I'm struggling with my first steps with Python and PySerial. I have one Arduino which reads impulses from an electricity power meter, passes the counts over to another Arduino over an RF link and then sends the data over to an Raspberry Pi. The Arduino to Arduino communication works well. However the last step of passing this to the RPi fails.
The MeterValue from the Arduino side is an unsigned Long (32 bits) and therefore split in 4 pieces of 8-bit transmissions plus a frame start and frame end:
[0x01] Frame start
[0xMeterValueByte0]
[0xMeterValueByte1]
[0xMeterValueByte2]
[0xMeterValueByte3]
[0x04] Frame end
This is my Arduino code continuously sent to the RPi in debug purpose :
void loop() {
//static test loop
Serial.write(0x01); //start frame
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x02);
Serial.write(0x04); //end frame
delay(3000);
}
This is my Python code:
Import serial
ser = serial.Serial('/dev/ttyACM0',9600)
print("Connected to: " + ser.portstr)
inputbuffer=[]
inputbuffer = [0 for i in range(10)]
while True:
if ser.inWaiting() > 0:
inputbuffer = ser.read(1)
print "Byte received"
print "Inputbuffer[0]: " + hex(ord(inputbuffer[0]))
j = inputbuffer[0]
print "j= " + hex(ord(j))
This check of the "j" parameter somehow fails:
if (j == 0x1): #look for start of frame
print "Start found\n"
print "Filling buffer\n"
while ser.inWaiting() < 5: #hold here until 5 bytes arrived
pass #proceed here
inputbuffer +=ser.read(5) #read 5 bytes
if inputbuffer[5] == 4: #check that the message has frame end
RXval = inputbuffer[1]
RXval |= (inputbuffer[2] << 8)
RXval |= (inputbuffer[3] << 16)
RXval |= (inputbuffer[4] << 24)
print "RXval: ", RXval
else:
print "RX error, no frame end\n"
inputbuffer = []
This is the Python output:
Connected to: /dev/ttyACM0
Byte received
Inputbuffer[0]: 0x1
j= 0x1
Byte received
Inputbuffer[0]: 0x0
j= 0x0
Byte received
Inputbuffer[0]: 0x0
j= 0x0
Byte received
Inputbuffer[0]: 0x2
j= 0x2
Byte received
Inputbuffer[0]: 0x4
j= 0x4
And then it repeats. Obviously the Python code doesn't realize j == 1. How should I handle the checking of the received bytes?