I would like to store in database one data that another raspberry pi send to me :
The problem is that during the sending, some data get lost. So I send 20 times the same data to be sure that my rapsberry receives correctly the data. However I want to store only one time my data
Here my sending code :
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=.5)
for x in range(0, 20)
ser.flushInput()
ser.flushOutput()
ser.write(' Hello world ! \r\n')
ser.flush()
Here my listening code :
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=.5)
data_fresh = " "
data_old = " "
while True:
if ser.inWaiting() > 0:
bytesToRead = ser.inWaiting()
data_fresh = ser.readline()
print(--- new data ---)
print(bytesToRead)
if data_fresh != data_old:
data_old = data_fresh
print(data_fresh)
# Store in db
here one result :
--- new data ---
8
H▒Hello world ! // The same bug that in my result
--- new data ---
8
Hello world !
--- new data ---
1
--- new data ---
2
--- new data ---
3
--- new data ---
4
--- new data ---
5
--- new data ---
6
--- new data ---
7
--- new data ---
1
...
As you see new data is called but I recieved nothing (bytesToRead > 0 which is weird) ... Moreover the first data that the listerner receives is corrupted.
I'm not famaliar with serial method.
Anyone know how I can solve th problem of buffer which not sending correclty my string and how to store only one data in db ?
0.3 delay on @9600 bps
. @onedkr – dsgdfg