Environment: ubuntu18.04, python3.7, Arduino IDE(Linux 64bit), vsCode.
2loadcells_namedPipe.ino
#include <Arduino.h>
#include "HX711.h"
const int LOADCELL_DOUT_PIN = 4, LOADCELL_SCK_PIN = 5;
const int LOADCELL_DOUT_PIN2 = 12, LOADCELL_SCK_PIN2 = 13;
HX711 scale, scale2;
void setup() {
Serial.begin(115200); // 9600이면 1초에 900bytes 정도 전송 가능.
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale2.begin(LOADCELL_DOUT_PIN2, LOADCELL_SCK_PIN2);
scale.set_scale();
scale2.set_scale();
// Serial.println("Tare. remove any weights");
// delay(500);
scale.tare();
scale2.tare();
Serial.println("Tare done");
}
void loop() {
if (scale.is_ready() && scale2.is_ready()){
long reading = scale.get_units(10);
long reading2= scale2.get_units(10);
Serial.println((String) reading + ", " + reading2); // (String) 없애면 알아볼 수 없는 값 나옴.
}
else{
Serial.println("hx711 connect error");
}
delay(100); // 시간이 길어지면 receiver에서 출력되는 값이 더 많아짐.
// 시리얼통신이기 때문에 직렬로 입력되는 data가 너무 빨리 들어가면 named pipe가 출력할 때
// 과부하가 걸려서 자동 종료되는 원리?
}
sender.py
import serial
import time
import os.path
# NodeMCU=serial.Serial(port="/dev/ttyACM0", baudrate=115200)
NodeMCU = serial.Serial('/dev/ttyACM0', 115200)
time.sleep(3) # 초기화 완료될 시간 약간 기다리기.
FIFO_FILENAME = './fifo-test'
if not os.path.exists(FIFO_FILENAME):
os.mkfifo(FIFO_FILENAME)
if os.path.exists(FIFO_FILENAME):
fp_fifo = open(FIFO_FILENAME, "w")
data = ''
fp_fifo.write(data)
while True:
c = NodeMCU.readline() # readlines()로 하면 입력이 끝났을 때까지 한 번에 읽어와서 무한루프로 input data 읽어오는 경우 출력 안 됨.
data = c.decode()
fp_fifo.write(data)
receiver.py
import os.path
FIFO_FILENAME = './fifo-test'
if os.path.exists(FIFO_FILENAME):
fp_fifo = open(FIFO_FILENAME, "r")
i = 0
while True:
with open(FIFO_FILENAME, 'r') as fifo:
data = fifo.read()
line = data.split('\n') # 없애고 바로 print(data)하면 시리얼모니터에서 보이는 모습 그대로 출력 됨.
for str in line:
i = i+1
print(str + "%4d" % i)
Output(receiver's terminal window)
-48, -236 1
-48, -72 2
-39, -221 3
-13, -271 4
-42, -186 5
-40, -31 6
-47, 21 7
-21, -247 8
-6, -134 9
-1, -20 10
-5, -110 11
-59, -32 12
-35, -32 13
-4, -62 14
13, -21 15
16
Error message:
somin@laptop:~/Desktop/2loadcells_namedPipe$ python sender.py
Traceback (most recent call last):
File "sender.py", line 19, in <module>
c = NodeMCU.readline() # readlines()로 하면 입력이 끝났을 때까지 한 번에 읽어와서 무한루프로 input data 읽어오는 경우 출력 안 됨.
File "/home/somin/.local/lib/python3.7/site-packages/serial/serialposix.py", line 596, in read
'device reports readiness to read but returned no data '
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
Expected:
The sender continues to run without being terminated, and the receiver continues to output input data.
Of existing question:
Python SerialException: Device reports readiness to read but returned no data (device disconnected?)
: It seems to be a different problem when I see that it is input correctly when debugging.
results of several trials:
But when I debugged it, several datas came in(what I want!)
Create and remove fifo file every time data is read