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.

  • 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/ttyS1works 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.