help
Dave Farrance
df at see.replyto.invalid
Fri Jan 15 10:02:12 EST 2016
sam Rogers <samlro2 at yahoo.com> wrote:
> I have downloaded python 2.7 with no problem. It works. I am trying to get pyserial to work. I have tried many different solutions. I am not sure if it works or not. How can I be sure? I am using windows 7. I did not see any help at python.org. Can you help?
>PS my goal is make this adafruit project work.
>https://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector/overview
Before using Python, have you verified that the IDE talks on the serial
port OK -- and that you can upload the simple blink sketch from its
provided examples and that you can change the blink rate of the LED?
Having done that, can you establish 2 way communication with Python?
What happens if you try the simple test script below? You will need to
change the "dev" line to whichever port you're using.
// Arduino sketch to echo serial port strings
char buf[81]; // max 80 chars + null
int siz = 0; // size of input string
void setup(){
Serial.begin(9600);
Serial.setTimeout(100);
}
void loop(){
siz = Serial.readBytesUntil('\n',buf,80); // get input string
if (siz == 0) return; // if timeout, loop again
Serial.print("I heard: "); // reply with some text and
buf[siz] = 0; // must add null terminator
Serial.println(buf); // return original string
}
#!/usr/bin/env python2
import serial,time
# LINUX PORT -- change to "COM1" or whatever for Windows
dev = "/dev/ttyACM0"
ser = serial.Serial(dev, 9600, timeout=1)
time.sleep(2) # Arduino starts in ~1.6s
while True:
mystr = raw_input("Enter text max 80 chars: ")
if mystr == '': break # terminate prog with <enter>
ser.flushInput() # flush input just in case
ser.write(mystr + '\n') # send newline-terminated string
txt = ser.readline(80) # read newline-terminated string
print len(txt),"chars: ",txt, # \r\n included so add final comma
ser.close() # close port
More information about the Python-list
mailing list