Python betiğim /dev/ttyUSB0 dosyasını açtığında Uno sıfırlanıyor
Yapay zekâ tarafından Español dilinden çevrildi ·
Biraz acemiyim, bariz bir şeyse kusura bakmayın. USB ile bir Raspberry Pi’ye takılı, CH340 çipli bir Arduino Uno klonum var. Uno, gaz sayacımın reed anahtarından gelen darbeleri sayıyor ve her 10 saniyede bir satır yazdırıyor; Pi üzerindeki bir Python betiği bu satırları okuyup Hermetry’ye gönderiyor.Sorun şu: Python betiği her başladığında (çökerse cron tarafından yeniden başlatılıyor) Uno sıfırlanıyor. Darbe sayısı 0’a dönüyor ve seri iletişimin ilk birkaç saniyesi kayboluyor. Hermetry’deki kartta gaz toplamında bu garip düşüşleri görüyorum.
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=2)
# Uno LED'i yanıp sönüyor, "boot" satırı geliyor, count=0
Bunun DTR hattından kaynaklandığını okudum. Pi’nin Arduino’yu sıfırlamasını durdurmak için ttyUSB0 üzerinde DTR’yi nasıl kapatabilirim?
Bit of a beginner so sorry if this is obvious. I have an Arduino Uno clone (CH340 chip) plugged into a Raspberry Pi with USB. The Uno counts pulses from my gas meter’s reed switch and prints a line every 10 s, and a Python script on the Pi reads the lines and sends them to Hermetry.
Problem: every time the Python script starts (and it gets restarted by cron if it crashes), the Uno resets. The pulse count goes back to 0 and the first few seconds of serial are lost. On the board in Hermetry I get these weird drops in the gas total.
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=2)
# Uno LED blinks, "boot" line comes in, count=0
I read it’s the DTR line. How do I turn DTR off on ttyUSB0 so the Pi stops resetting the Arduino?
Yapay zekâ tarafından English dilinden çevrildi ·
En kolay çözüm: betiği yeniden başlatma. Restart=always ayarlı bir systemd servisi olarak çalıştır, portu bir kez aç ve hiç kapatma.
Easiest fix: don’t restart the script. Run it as a systemd service with Restart=always, open the port once and never close it.
Yapay zekâ tarafından English dilinden çevrildi ·
DTR nedir? Bir su sayacı için de aynı türden bir kurulumum var
What is DTR? I have the same kind of setup for a water meter
Yapay zekâ tarafından English dilinden çevrildi ·
DTR, eski modem kontrol hatlarından biridir. Uno’da bu hat 100 nF’lık bir kapasitörden geçerek reset pinine bağlanır; böylece IDE, yüklemeden önce kartı resetleyebilir. Portu açan her program DTR’yi değiştirir ve kart resetlenir. Geçen yıl işte bir satır sayacında aynı sorunla uğraştım.Yazılım tarafında iki şeyi birlikte yapın:
stty -F /dev/ttyUSB0 -hupcl # don't drop DTR when the port closes
ser = serial.Serial()
ser.port = '/dev/ttyUSB0'
ser.baudrate = 9600
ser.timeout = 2
ser.dtr = False
ser.open()
Şunu da bilin: Linux’ta sürücü, önyüklemeden sonraki ilk açılışta DTR’yi bir an için yine yükseltebilir; yani bu yöntem resetleri azaltır ama her zaman tamamen önlemez.Kesin çözüm donanımsal: Uno’da RESET ile GND arasına, eksi GND’ye gelecek şekilde 10 µF’lık bir kapasitör koyun. Bu, reset darbesini bastırır. Ancak yeni firmware yüklerken bunu çıkarmanız (veya doğru anda resete elle basmanız) gerekir.Toplam değerdeki kayıplar için: sayımı her birkaç dakikada bir EEPROM’a da kaydedin ya da Uno’nun önyüklemeden değil, firmware yüklendiği andan itibaren çalışan toplamı göndermesini sağlayın. Böylece bir reset, toplamın tamamını değil, yalnızca birkaç darbeyi kaybettirir.
DTR is one of the old modem control lines. On the Uno it goes through a 100 nF capacitor to the reset pin, so the IDE can reset the board before an upload. Any program that opens the port toggles DTR, so the board resets. I fought this on a line counter at work last year.
Software side, two things together:
stty -F /dev/ttyUSB0 -hupcl # don't drop DTR when the port closes
ser = serial.Serial()
ser.port = '/dev/ttyUSB0'
ser.baudrate = 9600
ser.timeout = 2
ser.dtr = False
ser.open()
Be warned that on Linux the driver may still raise DTR for a moment on the very first open after boot, so this reduces resets, it doesn’t always kill them.
The sure fix is hardware: a 10 µF capacitor between RESET and GND on the Uno, minus to GND. That swamps the reset pulse. You then have to pull it out (or press reset by hand at the right moment) when you upload new firmware.
And for the drops in your total: also keep the count in EEPROM every few minutes, or have the Uno send a running total since it was flashed instead of since boot. A reset then costs you a few pulses, not the whole total.
Yapay zekâ tarafından English dilinden çevrildi ·
Toplam değer gönderilmesi konusunda +1. Saatlik tüketime ihtiyacın olursa, geçmiş API’si ve grafikler bunu sayaç değerinden hesaplayabilir.
+1 on sending a total. If you ever need consumption per hour, the history API and charts can work it out from the counter.
Yapay zekâ tarafından English dilinden çevrildi ·
Teşekkürler James. Yalnızca -hupcl yetmedi, yeniden başlatmadan sonraki ilk açılış yine resetledi, dediğin gibi. 10 µF kondansatörü taktım (kartın üzerine “yüklemek için çıkarın” yazan bir etiket koyarak, bunu zor yoldan zaten öğrenmiştim) ve resetler kesildi. Ayrıca scripti twilson’ın dediği gibi systemd’ye taşıdım. Artık sistem tamamen kararlı.
Thanks James. -hupcl alone wasn’t enough, the first open after a reboot still reset it, like you said. Put the 10 µF cap on (with a label on the board saying “remove to upload”, learned that the hard way already) and the resets are gone. Also moved the script to systemd like twilson said. Total is stable now.
- Bu konuyu yanıtlamak için giriş yapmış olmalısınız.
Sohbete katılın
Soru sormak ve yanıtlamak, yararlı gönderileri beğenmek ve sorununuzu çözen yanıtı işaretlemek için oturum açın. Her katkı puan ve seviye kazandırır.

