Here is my test code:
Code: Select all
#!/usr/bin/env python
#
print ("Starting program")
import serial
#import MAX3100 as COM
import time
# import string
#
count = 1
myin = ""
test=serial.Serial("/dev/ttyAMA0",9600,timeout=5)
test.close()
test.open()
teststring = "ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz\r\n"
#
try:
while True:
print (count, teststring)
test.write(teststring)
print ("Waiting 5 second")
#time.sleep(10)
myin = test.readline()
print("recieved :"+myin)
count = count + 1
#
except KeyboardInterrupt:
pass # do cleanup here
#
test.close()
So what is wrong and how do I fix it? I tried installing pyserial-2.6 but this did not help. I removed python3 and reinstalled it but got the same error. Again, this same code works under python2.pi@raspberrypi ~/te1860-4 $ sudo py serialtest.py
Starting program
1 ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz
Traceback (most recent call last):
File "serialtest.py", line 19, in <module>
test.write(teststring)
File "/usr/local/lib/python3.2/dist-packages/serial/serialposix.py", line 475, in write
n = os.write(self.fd, d)
TypeError: 'str' does not support the buffer interface