简体   繁体   中英

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.

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.

    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:

eg: Host machine send 0X80 0X01 0X00 0X33 0XB2 (hexadecimal),return Start plate address Status1 Status2 Status3 Status4 Status5 Status6 Status7

Command Check cod 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.

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 .
    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 .
    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. Read pySerial 3.0 documentation
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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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