0

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

  • 1
    This has nothing to do with the named pipe. The error is happening when reading from the serial port. – Barmar Aug 11 '22 at 01:26
  • Does this answer your question? [Python SerialException: Device reports readiness to read but returned no data (device disconnected?)](https://stackoverflow.com/questions/28343941/python-serialexception-device-reports-readiness-to-read-but-returned-no-data-d) – wovano Aug 11 '22 at 07:12
  • Yes. It seems to be a different problem when I see that it is input correctly when debugging. – lodis sign Aug 11 '22 at 07:36
  • Could you try to run the following code (4 lines) instead of `sender.py`? Line 1: `import serial`, Line 2: `NodeMCU = serial.Serial('/dev/ttyACM0', 115200)`, Line 3: `time.sleep(3)`, Line 4: `while True: print(repr(NodeMCU.readline()))` If this gives the same problem, your question has nothing to do with named pipes (as already suggested) and you should update the question accordingly. NB: the `repr()` is to see exactly what's received over the serial line. The output is slightly different: it's quoted and non-printable characters are escaped. It's a very useful trick. – wovano Aug 11 '22 at 08:05
  • thank you! It was the same error. Title updated. – lodis sign Aug 11 '22 at 08:59
  • Good to hear. You should also remove send.py and receiver.py from your question, replace it by the 4 (or 5 if indented correctly) lines of code that are necessary to reproduce this problem. And update the output accordingly. This will make the question much more focused and likely to get answered. And try to fit the title in one line. That makes it more readable and easier to find. – wovano Aug 11 '22 at 09:18

0 Answers0