Search code examples
pythonpyserial

Don't get any data from a weight indicator using pyserial?


I am using pyserial to fatch data from a ad-4407 weight indicator.But my program return "?" or " " all the time.But when i use another software like serial port monitor or rscom then i get data from the weight indicator.My port configuration and command are as follows

import serial
import codecs
import binascii

def weightFatcher():
    serial_port = serial.Serial()
    serial_port.port = "COM1"
    serial_port.baudrate = 2400
    serial_port.parity = serial.PARITY_EVEN
    serial_port.stopbits = serial.STOPBITS_ONE
    serial_port.bytesize = 7
    serial_port.timeout = 10
    serial_port.xonxoff = False
    serial_port.dsrdtr = False
    serial_port.rtscts = False
    serial_port.open()

    command = "RG\r\n"
    res = bytes(command, 'ascii')
    serial_port.write(res)
    data = serial_port.readall()
    print(data)
    serial_port.close()
weightFatcher()

Solution

  • You issue is probably that you do not wait long enough after sending out your request. Try the following:

    command = "RG\r\n"
    res = bytes(command, 'ascii')
    serial_port.write(res)
    
    time.sleep(1)
    
    bytesToRead = serial_port.inWaiting()
    data = serial_port.read(bytesToRead)
    
    print(data)
    serial_port.close()
    

    You can play around with the waiting time, or have the read part looped until the bytes read match the expected data length.