我是IoT平台的新手。我需要阅读使用Python的串行端口RS232以9600波特率(设备名称:Sunrom血压传感器)读取血压和心率并输出。请帮助我提供任何有用的参考链接和有用的材料。
答案 0 :(得分:0)
三个步骤的快速指南:
1)在Rpi上的UART上禁用内核消息:运行sudo raspi-config
,在菜单上选择选项5(接口选项),然后选择P6(串行),然后选择否。有关此{{3}的更多详细信息}。
2)将传感器连接到RPi的UART:根据here,传感器上的UART工作在3.3V电平,因此将其直接连接到Pi的UART应该是安全的。进行以下连接:
|Sensor | RPi connector |
|=============================|
| +5V | 2 or 4 (+5V) |
| GND | 6 or 9 (GND) |
| TX-OUT| 10 (UART0 RXD) |
3)启动您的Pi和传感器,并运行以下脚本(使用Python 3.x),改编自this link,仅删除打算写入端口的部分(您无需编写任何东西,您的传感器都会不断发送数据)并解码接收到的原始数据以正确显示它:
import serial, time
#initialization and open the port
#possible timeout values:
# 1. None: wait forever, block call
# 2. 0: non-blocking mode, return immediately
# 3. x, x is bigger than 0, float allowed, timeout block call
ser = serial.Serial()
ser.port = "/dev/ttyS0"
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = None #block read
ser.timeout = 1 #non-block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
try:
ser.open()
except Exception as e:
print("error open serial port: " + str(e))
exit()
if ser.isOpen():
try:
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output
#and discard all that is in buffer
numOfLines = 0
while True:
response = ser.readline().decode()
print("Blood Pressure [mmHg] (Systolic, Diastolic, Pulse): " + response)
numOfLines = numOfLines + 1
time.sleep(1)
if (numOfLines >= 5):
break
ser.close()
except Exception as e1:
print("error communicating...: " + str(e1))
else:
print("cannot open serial port ")
免责声明:以上内容均未经过测试(连同传感器一起,我确实对代码进行了测试),但它应该立即起作用,或者至少为您提供入门指南。