Sample code for serial (RS-232) connection between computer and Libela Elsi G 325 retail scale/weight
import serial | |
""" | |
Tehtnica: Libela Elsi G 325 (https://www.libela-elsi.si/en/retail-scales/retail-scales-without-printer/) | |
Protokol: TISA-4 (0) | |
https://pypi.org/project/pyserial/ | |
https://pyserial.readthedocs.io/en/latest/shortintro.html | |
Podatki za povezavo s tehtnice: 9600 8 N 1 | |
baudrate = 9600 | |
bytesize = 8 | |
partiy = N | |
stopbits = 1 | |
""" | |
PORT = '/dev/cu.UC-232AC' | |
def checksum(bytes_string): | |
""" | |
https://stackoverflow.com/questions/26517869/creating-xor-checksum-of-all-bytes-in-hex-string-in-python | |
""" | |
cs = 0 | |
for el in bytes_string: | |
cs ^= el | |
return bytes([cs]) | |
def fixed_5_bytes_price(price_as_string): | |
price = round(float(price_as_string), 2) | |
decimals = str(round(price % 1, 2))[2:4].encode() | |
integers = str(int(price))[0:3].encode() | |
return integers.rjust(3, b'0') + decimals.ljust(2, b'0') | |
def prepare_outgoing_data(): | |
start = b'98' | |
price = input("Vpiši ceno izdelka (npr '4.2' za 4,2 €/kg): ") | |
price = fixed_5_bytes_price(price) # '4.2' -> b'00420' | |
cs = checksum(start + price) | |
return start + price + cs + b'\x0D\x0A' | |
def display_incoming_data(in_data): | |
data_str = in_data.decode() | |
s = "Masa v mirovanju: {}".format("DA" if data_str[2] == "0" else "NE") | |
w = "Izmerjena masa: {:.3f} kg".format(int(data_str[3:8]) / 1000) | |
e = "Znesek OK: {}".format("DA" if data_str[8] == "0" else "NE") | |
i = "Znesek: {:.2f} €".format(int(data_str[9:15]) / 100) | |
print(s, w, e, i, sep = "\n") | |
# init | |
pos_to_weight = prepare_outgoing_data() | |
# print(pos_to_weight) | |
# open serial port | |
ser = serial.Serial(PORT, timeout = 1, write_timeout = 1) | |
# print data on the just created and opened serial device | |
# print(ser) | |
# send data | |
ser.write(pos_to_weight) | |
response = ser.read_until(b'\r\n') | |
#print(response) | |
display_incoming_data(response) | |
# close the connection | |
ser.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment