Reputation: 11
I am trying to connect an OHAUS Explorer EX124/AD balance to a Raspberry Pi 5, and alternatively a Windows 10 PC, both times using the RS232 connection on the balance. Both computers yield the same results. The RS232 connector is used with a USB adapter to plug into the computers, if that matters.
I don't believe that I have fallen into the common mistakes that I see on here, such as wrong baud rate, not encoding the messages, or omitting return characters \r\n
.
I am able to connect to the balance, and am able to read values off on the computer when using the automatic print function on the balance or by pressing the print button, using my function autoprint
. This works on both the Pi and the Windows PC.
However, if I try to send commands to the balance, they are always ignored. No errors are shown, but nothing happens (in the case of my function opendoor
) or blank messages are returned (in the case of my function ip
).
I got the serial commands from the manual, page EN-151, section 9.4.1. The commands that I am most interested in are IP, which makes it print (send the current balance reading to the computer) and WI 1 1 (open the automatic doors of the balance).
import serial
from time import sleep
import re
# Connecting to the balance - this works
try:
#ser = serial.Serial('COM3', # Works with Windows
ser = serial.Serial('/dev/ttyUSB0', # For use with Raspberry Pi - also works
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = 1)
except Exception as scaleserror:
print("Unable to connect to balance:\n", scaleserror) # I do not get this message
def autoprint(): # This works as expected when the balance is autoprinting, or the print button is pressed on the balance - as expected
ser.flushInput()
ser.flushOutput()
weight = 0
sleep(0.1)
while weight == 0:
x = ser.readline()
if str(x)[2] == " ": # Interpreting the output of the balance
weight = re.findall(r'\b\d+[.,]?\d+\b', str(x))
return weight
def opendoor(): # This doesn't do anything
sleep(0.1)
ser.flushInput()
ser.flushOutput()
ser.write('WI 0 1\r\n'.encode()) # Three variations, none work, nothing happens
ser.write('WI11\r\n'.encode())
ser.write(b'WI11\r\n')
def ip(): # This doesn't work - returns blank
sleep(0.1)
ser.flushInput()
ser.flushOutput()
ser.write('IP\r\n'.encode())
sleep(0.1)
returnMsg = ser.readline()
print(returnMsg) # Returned message is blank (b' ')
opendoor()
ip()
I realise that it is unlikely that anyone will be able to replicate this without the specific balance, but hopefully this code will show what the problem is! Hopefully this is a stupid mistake, since this is my first time using a serial connection with Python. Thanks in advance!
Upvotes: 0
Views: 267
Reputation: 11
After speaking more with the OHAUS technical support, suspicion was aimed at the RS232-USB cable. After swapping out the cable, it started working!
I didn't suspect the cable, since it worked in one direction (from the scales to the computer), but it didn't work in the other direction, from the computer to the scales!
Upvotes: 0