Reputation: 148
I have connected my RPi3 Tx and Rx pins together. I use the following code:
import serial
from time import sleep
ser = serial.Serial(
port='/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=10
)
while True:
ser.write(0)
sleep(1)
incoming = ser.read(ser.inWaiting())
print(incoming)
This prints empty b''
. I can do this: '0'.encode()
and then I get b'0'
.
I need to send packets of data to a sensor: bytearray({0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79})
. The issue is that in loopback I get incomplete package, and it is out of order. It seems that after the first '0' that gets sent it terminates, or something else happens. What is the correct way to send commands like this? I also tried ser.read(9)
since I expect 9 bytes, but it still cuts it off.
Upvotes: 1
Views: 674
Reputation: 12255
The problem lies with
bytearray({0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79})
where the {...,}
syntax is being used to construct a set. This is an unordered list of unique items. Hence the multiple zero bytes become just one, and the order is arbitrary. Instead, use a list:
data = bytearray((0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79))
ser.write(data)
Upvotes: 1