I connected the RS-232 output of an old weighing indicator directly to the Pi 4 GPIO, and weird characters are coming through
Translated from Türkçe by AI ·
Hi, I’m a software developer and the hardware side is a bit foreign to me, so tell me if I’m talking nonsense.At a customer’s warehouse entrance there is an old scale indicator with a DB9 RS-232 output on the back. According to the manual, it prints the weight line by line on every weighing. My goal is to read this with a Raspberry Pi 4, send it to Hermetry as telemetry, and then show the daily tonnage on a dashboard.What I did: I connected pins 2, 3 and 5 of the DB9 with jumper wires to the Pi’s GPIO14 (TX), GPIO15 (RX) and GND pins. I enabled serial from raspi-config. In Python I read it like this:
import serial
s = serial.Serial('/dev/serial0', 9600, timeout=2)
while True:
print(s.readline())
The output looks like this:
b'ùæ þ'
b'ø '
b'àþ'
There is nothing meaningful in it. Also, every now and then a terminal shows something like my own login screen, and I didn’t understand that either. I’m also not sure whether I’ll burn out the indicator or the Pi, since I measured something like -9 V on the indicator output with a multimeter.
Selam, ben yazılımcıyım, donanım tarafı biraz yabancı, o yüzden saçmalıyorsam söyleyin.
Bir müşterinin depo girişinde eski bir kantar göstergesi var, arkasında DB9 RS-232 çıkışı var. Kılavuza göre her tartımda satır satır ağırlık basıyor. Amacım bunu Raspberry Pi 4 ile okuyup Hermetry’ye telemetri olarak göndermek, sonra dashboard’da günlük tonaj göstermek.
Yaptığım: DB9’un 2, 3 ve 5 numaralı pinlerini jumper kabloyla Pi’nin GPIO14 (TX), GPIO15 (RX) ve GND pinine bağladım. raspi-config’den serial’i açtım. Python’da şöyle okuyorum:
import serial
s = serial.Serial('/dev/serial0', 9600, timeout=2)
while True:
print(s.readline())
Çıktı şöyle:
b'ùæ þ'
b'ø '
b'àþ'
Hiç anlamlı bir şey yok. Bir de arada bir terminalde kendi login ekranım gibi bir şey çıkıyor, onu da anlamadım. Göstergeyi ve Pi’yi yakar mıyım diye de içim rahat değil açıkçası, gösterge çıkışında multimetreyle -9 V gibi bir şey ölçtüm.
Translated from Türkçe by AI ·
Is the baud rate correct? It could be 4800 or something like that in the indicator’s menu.
Baud doğru mu? Göstergenin menüsünde 4800 falan olabilir.
Translated from Türkçe by AI ·
I checked the menu, it says 9600 8N1. The manual says so too.
Menüden baktım, 9600 8N1 yazıyor. Kılavuz da öyle diyor.
Translated from Türkçe by AI ·
It’s not the baud rate, it’s the voltage. If you measured -9 V, the answer is already there.An RS-232 signal swings roughly ±3 to ±15 V and its logic is inverted: idle is negative, “1” is negative, “0” is positive. The Pi’s UART, on the other hand, is 3.3 V TTL, 0 to 3.3 V, with normal logic. Connecting them directly gets both the level and the polarity wrong, and that garbage in the output is exactly what you’d expect. On top of that, applying a negative voltage and around 9 V to GPIO15 can kill the pin. Disconnect it right away; it’s luck if the Pi is still intact.I’ve done the same job in two places and used two approaches:1. MAX3232 module (MAX3232, not MAX232; it runs at 3.3 V). Feed VCC from the Pi’s 3.3 V, TTL side to GPIO14/15, RS-232 side to the DB9. 2. The cleaner option: a USB-RS232 adapter (PL2303 or FTDI chip). It shows up as /dev/ttyUSB0 and you don’t have to deal with GPIO at all.About the login screen issue: the Pi’s serial console is open on /dev/serial0. In raspi-config > Interface > Serial Port, answer No to “login shell over serial” and Yes to “serial port hardware”.And about TX/RX crossing: we don’t know whether the scale is DCE or DTE. If no data arrives, swap pins 2 and 3.Sending the data to Hermetry after that part is simple: using paho with an access token, you publish to v1/telemetry:
import re, json, serial
import paho.mqtt.client as mqtt
c = mqtt.Client(client_id="kantar-1")
c.username_pw_set(TOKEN, "")
c.tls_set()
c.connect(BROKER_HOST, 8883, keepalive=60)
c.loop_start()
s = serial.Serial('/dev/ttyUSB0', 9600, timeout=2)
while True:
line = s.readline().decode('ascii', 'ignore')
m = re.search(r'(-?d+.d+)s*kg', line)
if m:
c.publish("v1/telemetry", json.dumps({"weight_kg": float(m.group(1))}), qos=1)
Baud değil, voltaj sorunu. -9 V ölçtüysen zaten cevap orada.
RS-232 sinyali yaklaşık ±3 ile ±15 V arası ve mantığı ters: boşta negatif, “1” negatif, “0” pozitif. Pi’nin UART’ı ise 3.3 V TTL, 0 ile 3.3 V arası ve düz mantık. Doğrudan bağlayınca hem seviye hem polarite yanlış, çıktıdaki çöp tam olarak bu. Üstelik GPIO15’e negatif ve 9 V civarı gerilim vermek pini öldürebilir. Hemen sök, Pi’nin hâlâ sağlam olması şans.
Bende iki depoda aynı iş var, iki yol kullandım:
1. MAX3232 modülü (MAX232 değil, 3232; 3.3 V ile çalışan). VCC’yi Pi’nin 3.3 V’una ver, TTL tarafı GPIO14/15, RS-232 tarafı DB9’a. 2. Daha temizi: USB-RS232 çevirici (PL2303 ya da FTDI çipli). /dev/ttyUSB0 olarak çıkıyor, GPIO ile hiç uğraşmıyorsun.
Login ekranı meselesi: Pi seri konsolu /dev/serial0 üzerinden açık. raspi-config > Interface > Serial Port’ta “login shell over serial” sorusuna Hayır, “serial port hardware” sorusuna Evet de.
Bir de TX/RX çaprazlaması: gösterge DCE mi DTE mi bilmiyoruz. Veri gelmezse 2 ile 3’ü yer değiştir.
Okuduktan sonra Hermetry’ye gönderme kısmı basit, paho ile access token’la v1/telemetry‘ye yayınlıyorsun:
import re, json, serial
import paho.mqtt.client as mqtt
c = mqtt.Client(client_id="kantar-1")
c.username_pw_set(TOKEN, "")
c.tls_set()
c.connect(BROKER_HOST, 8883, keepalive=60)
c.loop_start()
s = serial.Serial('/dev/ttyUSB0', 9600, timeout=2)
while True:
line = s.readline().decode('ascii', 'ignore')
m = re.search(r'(-?d+.d+)s*kg', line)
if m:
c.publish("v1/telemetry", json.dumps({"weight_kg": float(m.group(1))}), qos=1)
Translated from Türkçe by AI ·
Converting it to a number with a regex is important. If the indicator prints text like " 12.45 kg" and you send it as is, it stays as text in Hermetry, is not plotted on the chart, and no hourly average or total comes out. In one project we sent text for two weeks, and when we noticed, the historical data was useless.
Regex’le sayıya çevirmesi önemli. Gösterge " 12.45 kg" gibi metin basıyorsa ve onu olduğu gibi gönderirsen Hermetry’de metin olarak kalır, grafikte çizilmez, saatlik ortalama ya da toplam da çıkmaz. Biz bir projede iki hafta metin göndermişiz, fark ettiğimizde geçmiş veri işe yaramıyordu.
Translated from Türkçe by AI ·
Solved, thanks.I couldn’t find a USB-RS232 converter, so I got a MAX3232 module from the electronics guy. I connected it, still got garbage, then as Esra said I crossed pins 2 and 3, and the lines started coming in properly. I also turned off the serial console, the login screen disappeared. GPIO15 also came out fine, I got lucky.Thanks to Tuğba’s note, weight_kg is going out as a number from the very start, and it’s being plotted as a graph on the dashboard.
Çözüldü, sağ olun.
USB-RS232 çevirici bulamadım, elektronikçiden MAX3232 modülü aldım. Bağladım, yine çöp geldi, sonra Esra’nın dediği gibi 2 ile 3’ü çaprazladım, satırlar düzgün gelmeye başladı. Seri konsolu da kapattım, login ekranı kayboldu. GPIO15 da sağlam çıktı, şanslıymışım.
Tuğba’nın notu sayesinde ilk günden weight_kg sayı olarak gidiyor, dashboard’da grafik çiziliyor.
- You must be logged in to reply to this topic.
Join the conversation
Sign in to ask and answer, like helpful posts and mark the reply that solved your problem. Every contribution earns points and levels.

