Read serial output, remove nul and save to file

0

In Cygwin, I am trying to read serial port input, filter it to remove nul characters, and save the output to a file. Something like the following, which results in an empty logfile:

cat /dev/ttyS1 | tr -d '\000' >myfile.log

This shows stdout on the terminal:

cat /dev/ttyS1 | tr -d '\000'

I tried using stdbuf -oL -eL, per this post, to no avail.

Bonus points if you can get it to work with grep filtering stdout on the terminal (i.e., log everything to file, but only see filtered output on the terminal).

cat /dev/ttyS1 | tr -d '\000' | tee myfile.log | egrep --line-buffered "WARN|ERROR"

Note: The serial port I am using is an FTDI USB serial adapter.

mrtumnus

Posted 2018-05-18T19:44:12.257

Reputation: 1

Does /dev/ttyS1 exist ? – matzeri – 2018-05-19T11:26:20.570

I used /dev/ttyS1 as an example. The serial port I'm using on this particular machine is different and does exist (I can read input using cat /dev/ttyS1). – mrtumnus – 2018-05-21T15:17:51.717

If cat /dev/ttyS1works and cat /dev/ttyS1 | tr -d '\000' produces nothing it seems your serial port is only producing null – matzeri – 2018-05-21T17:08:51.863

@matzeri No, it is producing regular text with nulls inserted before "\r\n" on each line. I think it might be due to the source of the serial data is written in C, which uses null terminators for strings. But lets assume I don't have control over that. How do I remove the nulls and save to file? – mrtumnus – 2018-05-23T12:31:31.417

Does work if you split the action in two : cat /dev/ttyS1 > tempfile and cat tempfile | tr -d '\000' ? – matzeri – 2018-05-23T19:41:14.327

Yes, that is my current approach that works. It's puzzling that doing it all together in a single pipe-chain does something different. – mrtumnus – 2018-05-25T13:10:37.683

Very strange. Testing with cat /dev/random | tee -a file1 | tr -d '\000' > file2 file1 has null bytes but file2, as expected none. od -b file2 | colrm 1 7 | grep 000 is empty – matzeri – 2018-05-25T14:59:24.323

Using /dev/random in cygwin produces the filtered output correctly, as I would expect. However, /dev/ttyS1 still results in a 0-byte file after the tr call. – mrtumnus – 2018-05-29T18:03:27.000

No answers