简体   繁体   English

使用python Raspberry Pi 3从串行设备检索数据时出现问题

[英]Problems retrrieving data from serial device with python Raspberry Pi 3

i have a solenoid control hardware that I am trying to use for an Access system, according to the manual of the device i am sending the command to the device and tri to get the status but i am only receiving blank lines. 我有一个电磁线圈控制硬件,我正在尝试将它用于Access系统,根据设备的手册,我正在向设备发送命令,并尝试获取状态,但我仅收到空白行。

the command to know the estatus of the ports is the following: 知道端口状态的命令如下:

  1. Read the lock status command(The feedback of the door switch status) Start plate address Lock address Command Check code (XOR) 0X80 0X01 – 0XF 0X00-32 0X33 XX The lock address is 0X00, return the status of all of the locks, if it's not 0, then it will back to this lock status. 读取锁状态命令(门开关状态的反馈)起始板地址锁地址命令校验码(XOR)0X80 0X01 – 0XF 0X00-32 0X33 XX锁地址为0X00,返回所有锁的状态,如果它不为0,则将返回到此锁定状态。

    eg: a. 例如: Return back to the status of lock 1 eg: Upper computer send 0X80 0X01 0X01 0X33 0XB2 (hexadecimal), return Command plate address Lock address Status Check cod 0X80 0X01 0X01 0X11 0X91 (Lock Open) 0X80 0X01 0X01 0X00 0X80 (Lock Close b. Return back to the status of all of the locks: 返回到锁1的状态,例如:上位机发送0X80 0X01 0X01 0X33 0XB2(十六进制),返回命令板地址锁地址状态检查代码0X80 0X01 0X01 0X11 0X91(锁打开)0X80 0X01 0X01 0X00 0X80(锁关闭b。返回到所有锁的状态:

eg: Host machine send 0X80 0X01 0X00 0X33 0XB2 (hexadecimal),return Start plate address Status1 Status2 Status3 Status4 Status5 Status6 Status7 例如:主机发送0X80 0X01 0X00 0X33 0XB2(十六进制),返回起始板地址Status1 Status2 Status3 Status4 Status5 Status6 Status7

Command Check cod 0X80 0X01 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF 0X33 0XXX 命令检查代码0X80 0X01 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF 0X33 0XXX

Status: From status 1 to Status 7, high to low, the corresponding lock is 1-50. 状态:从状态1到状态7,从高到低,相应的锁定为1-50。

This is my code: 这是我的代码:

#!/usr/bin/env python

# based on tutorials:
#   http://www.roman10.net/serial-port-communication-in-python/
#   http://www.brettdangerfield.com/post/raspberrypi_tempature_monitor_project/

import serial, time

SERIALPORT = "/dev/ttyUSB0"
BAUDRATE = 9600

ser = serial.Serial(SERIALPORT, BAUDRATE)

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 = 0             #non-block read

ser.timeout = 2              #timeout 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

ser.writeTimeout = 0     #timeout for write

print 'Starting Up Serial Monitor'

if ser.isOpen():

    try:
        ser.flushInput() #flush input buffer, discarding all its contents
        ser.flushOutput()#flush output buffer, aborting current output

        ser.write (serial.to_bytes([0X80,0X01,0X33,0XB2]))
        print("write data: 0X80,0X01,0X33,0XB2")
        time.sleep(0.5)
        numberOfLine = 0

        while True:

            response = ser.readline()
            print("read data: " + response)

            numberOfLine = numberOfLine + 1
            if (numberOfLine >= 8):
                break

        ser.close()

    except Exception, e:
        print "error communicating...: " + str(e)

else:
    print "cannot open serial port "

And this is my result: 这是我的结果:

read data: read data: read data: read data: read data: read data: read data: read data: 读取数据:读取数据:读取数据:读取数据:读取数据:读取数据:读取数据:读取数据:

Can you please help me what am i doing wrong? 你能帮我做错什么吗?

Comment ... document: link 评论 〜文件:链接

  1. Your command [0x80,0x01,0x33,0xB2] is wrong . 您的命令[0x80,0x01,0x33,0xB2] 错误
    use 采用

     ser.write ([0x80, 0x01, 0x01, 0x33, 0xB2]) 

    or 要么

     ser.write ([0x80, 0x01, 0x00, 0x33, 0xB2]) 
  2. Let's check if pySerial see your Serial Port and the Solenoid Control Hardware . 让我们检查pySerial看到您的串行端口和电磁控制硬件
    Execute the following and examine the output: 执行以下操作并检查输出:

     python -m serial.tools.list_ports 
  3. Remove ALL settings between 删除之间的所有设置

     ser.bytesize ... ser.writeTimeout 

    use defaults with only this code: 使用以下代码使用默认值

     ser = serial.Serial(SERIALPORT) 

Change to the following: 更改为以下内容:

    ser.write ([0x80, 0x01, 0x00, 0x33, 0xB2])

You already have binary Data, therefore no encoding required. 您已经有binary数据,因此不需要编码。 Read pySerial 3.0 documentation 阅读pySerial 3.0文档
serial.to_bytes(sequence)

I'm unsure about your command, your Question's description is somewhat unclear. 我不确定您的命令,您对问题的描述不清楚。
Do you have a public link to the Documentation of Solenoid Control Hardware . 您是否有电磁阀控制硬件文档的公共链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM