ฉันต้องการส่งและรับข้อมูลระหว่างพอร์ตอนุกรมสองพอร์ต ฉันใช้ชุดเครื่องมือ CANtact เป็นอุปกรณ์ซีเรียลของฉัน ฉันใช้รหัสต่อไปนี้
เขียน.py:
import serial
#init serial port and bound
# bound rate on two ports must be the same
ser = serial.Serial('/dev/ttyACM1', 9600)
print(ser.portstr)
#send data via serial port
serialcmd=("012345688902341")
ser.write(serialcmd.encode())
ser.close()
Listen.py
import serial
serBarCode = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
while True:
#read data from serial port
serBarCode = serBarCode.readline()
#if there is smth do smth
if len(serBarCode) >= 1:
print(dataBarCode.decode("utf-8"))
ฉันได้รับข้อผิดพลาดต่อไปนี้:
hp@HP-โน้ตบุ๊ก:~$ cd /home/hp/Desktop
hp@HP-โน้ตบุ๊ก:~/Desktop$ python3 write.py
/dev/ttyACM1
hp@HP-โน้ตบุ๊ก:~/Desktop$ python3 Listen.py
Traceback (การโทรล่าสุดครั้งล่าสุด):
ไฟล์ "listen.py" บรรทัด 7 นิ้ว
serBarCode = serBarCode.readline()
AttributeError: วัตถุ 'ไบต์' ไม่มีแอตทริบิวต์ 'readline'
hp@HP-โน้ตบุ๊ก:~/Desktop$