I made a code reading RPi by serial from pressure sensor. Somehow my code reading raw data from serial sensor but my while
loop does not work and I’m betting blank screen.
What I’m doing wrong? Whole code below:
import serial
import sys
import time
gauge = serial.Serial(
port = '/dev/serial0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = 1
)
while(True):
while(True):
input_string = list(gauge.read(2)) #watch for two magic bytes
if(input_string[0] == 7 and input_string[1] == 5):
input_string += list(gauge.read(9-2)) #append the rest of the data
if((sum(input_string[1:8]) % 256) == input_string[8]): #compute checksum
break
gauge_value = 10.0**((input_string[4] * 256.0 + input_string[5]) / 4000.0 - 12.5)
if(len(sys.argv) > 2):
print("mBar: {} Pa: {} Status: {} ({}) Error: {} ({})".format(gauge_value,gauge_value*100000.0,
input_string[2],bin(input_string[2]),
input_string[3],bin(input_string[3])))
else:
print("{},{}".format(time.time(), gauge_value))
Need to mention initial piece of code reading well and I’m getting raw data:
import serial
import time
ser = serial.Serial(
port='/dev/serial0',#Replace ttyUSB0 with ttyAMA0 for Pi1,Pi2,Pi0
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
while 1:
x=ser.readline()
print (x)
print raw data as below:
'n'
b'x8ex07x05@x08xbcI(!xfex15Ax00xecx1cx00x8dxbcx87x1cx1cxf4!x00n'
b'x1cx07x05x00x00xecx95x94xb5xf8x144x15xecx0ex00n'
b'tx07x05 x88n8H$xa8x1cx00xecx03x00xca{x07x15x90xb0xb5?n'
b'xf3Px10x00xebxb6x08x95xbc(x1cx1cxb4xfax00n'
So my guess is I made something wrong in while
loop.