rserial-portesp8266

R serial::read.serialConnection() returning empty value, even though there are values in buffer


I'm using R serial::read.serialConnection() to read serial output from an ESP8266 connected with USB to my computer. I just used the standard example and it works perfectly for years.

This is the code:

library(serial)
con <- serialConnection(port = "COM7"
                        ,mode = "115200,n,8,1"
                        ,newline = 1
                        ,translation = "crlf"
)
open(con)
read.serialConnection(con, n = 0)

Yesterday (26/06/2024) I runned this code again and unfornunily I had only an empty value as output. I checked the serial output within Arduino software and I also made a python script to read the serial port. In both cases everything worked, but I wonder why it didn't on R. I also noticed the same problem on a different computer, both systems were Windows 11.

As a paliative fix I openend the read.serialConnection function and I noticed that in line 26:

comStr <- paste("gets ${sdev_", con$var, "}", sep = "")

If I change it to:

comStr <- paste("read ${sdev_", con$var, "}", sep = "")

Then, this function returned to work. This comStr variable is used inside tcltk::.Tcl(), but to dig into "Low-level Tcl/Tk Interface" was to much for me.

Interristing, with the function serial::nBytesInQueue() I was able to check the buffer size and it increases while I reviced data in the COM port. But, running just tcltk::.Tcl("gets ${sdev_COM7}) I get an empty output, even with a buffer size higher than 0.

I have a prototype sensor for research purpose and I developed several R script for the users with low experience on R. I would be really happy if someone could help me solving this without rewriting the function serialread.serialConnection() to change line 26.

PS: I also tried to reinstall R and it's packages and the error persisted.


Solution

  • Try changing translation = "crlf" to translation = "auto". I had the same problem you described and solved with this change. You can find my setup here.