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
  • 113
  • 6
  • Does `/dev/ttyS1` exist ? – matzeri May 19 '18 at 11:26
  • 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 May 21 '18 at 15:17
  • If `cat /dev/ttyS1`works and `cat /dev/ttyS1 | tr -d '\000'` produces nothing it seems your serial port is only producing null – matzeri May 21 '18 at 17:08
  • @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 May 23 '18 at 12:31
  • Does work if you split the action in two : `cat /dev/ttyS1 > tempfile` and `cat tempfile | tr -d '\000' ` ? – matzeri May 23 '18 at 19:41
  • 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 May 25 '18 at 13:10
  • 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 May 25 '18 at 14:59
  • 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 May 29 '18 at 18:03

0 Answers0