Reputation: 31
Im working on a project regarding LIFI and we wanted to test the communication by sending text message through USB port(windows OS) to light which blinks(0s n 1s) n receiver feeds it back to PC USB port(linux OS). Coding has been done in python3 language and it works for single character alphabets n multi character numerals but I receiver dont receive same numerals or alphabets for some characters. It might be related to encoding n decoding.
Transmission
import serial
ser=serial.Serial()
ser.port="/dev/ttyUSB0"
ser.baudrate=9600
ser.open()
while 1:
print("what to send--")
ab=input()
if ser.isOpen():
a=ser.write(ab.encode()) #Can also use (b'str')- converts to bytes
print("Sent")
print(a)
else:
print("Couldnt Open")
ser.close()
Receiver
import serial
ser=serial.Serial("/dev/ttyUSB2", 9600)
if ser.isOpen():
print(ser.name + " is open \n Receiving= ")
while True:
out= ser.read()
print(out.decode())
else:
print("Couldnt open")
ser.close()
Im reading character by character in an infinite loop. I receive some characters correctly like x,z,o.p but for 'b' i get 's', 'c' i get 'r'. For numerals 2,3,6,7 are coming correctly but 1,,4,5,8,9,0 comes as !,6,7,>,!,>. For a line of alphabets I get UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c.
Upvotes: 2
Views: 352
Reputation: 31
I got it working after changing to Baudrate=38400 given in the manual, but theoretically it should be working for all baudrates.
Upvotes: 1