How to pipe data from /dev/ttyUSB0 to a python script
<p>I can  <code>sudo cat /dev/ttyUSB0</code> and data is flowing in the console.</p>
<p>but I cant figure out how to do this in a python script, to consume the data.</p>
<p>I tried</p>
<pre><code>sudo python test.py &lt; /dev/ttyUSB0
sudo cat /dev/ttyUSB0 | python test.py
</code></pre>
<p>where test.py</p>
<pre><code>import sys

for line in sys.stdin:
    print line
</code></pre>
<p>It never prints anything, but if i do ls | python test.py it echos the content of ls fine?</p>
<p>What do i need to do in my test.py to read from /dev/ttyUSB0</p>
<h2>update for comments</h2>
<p>So when i reboot the pi and cat out without running anything other than what shown here:</p>
<pre><code>pi@raspberrypi ~ $ sudo gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock
pi@raspberrypi ~ $ sudo cat /dev/ttyUSB0
$GPGGA,111448.000,5543.1460,N,01229.2541,E,2,08,1.13,19.1,M,41.5,M,0000,0000*5A
$GPGSA,A,3,10,16,18,21,26,20,29,27,,,,,1.42,1.13,0.87*0D
$GPRMC,111448.000,A,5543.1460,N,01229.2541,E,0.37,331.77,270816,,,D*63
$GPVTG,331.77,T,,M,0.37,N,0.68,K,D*33
$GPGGA,111449.000,5543.1462,N,01229.2542,E,2,08,1.13,19.1,M,41.5,M,0000,0000*5A
$GPGSA,A,3,10,16,18,21,26,20,29,27,,,,,1.42,1.13,0.87*0D
$GPRMC,111449.000,A,5543.1462,N,01229.2542,E,0.40,336.35,270816,,,D*62
$GPVTG,336.35,T,,M,0.40,N,0.74,K,D*3F
$GPGGA,111450.000,5543.1463,N,01229.2541,E,2,08,1.13,19.1,M,41.5,M,0000,0000*50
$GPGSA,A,3,10,16,18,21,26,20,29,27,,,,,1.42,1.13,0.87*0D
$GPRMC,111450.000,A,5543.1463,N,01229.2541,E,0.18,42.67,270816,,,D*52
$GPVTG,42.67,T,,M,0.18,N,0.33,K,D*06
$GPGGA,111451.000,5543.1464,N,01229.2542,E,2,08,1.13,19.1,M,41.5,M,0000,0000*55
$GPGSA,A,3,10,16,18,21,26,20,29,27,,,,,1.42,1.13,0.87*0D
$GPGSV,4,1,14,21,66,084,17,16,65,264,18,26,56,199,24,27,41,281,20*74
$GPGSV,4,2,14,20,37,067,14,18,37,138,25,49,26,189,28,10,14,169,21*72
$GPGSV,4,3,14,07,13,335,,29,11,100,16,08,07,282,,13,05,050,*73
$GPGSV,4,4,14,05,04,025,,15,04,080,*72
$GPRMC,111451.000,A,5543.1464,N,01229.2542,E,0.30,7.93,270816,,,D*67
$GPVTG,7.93,T,,M,0.30,N,0.55,K,D*36
</code></pre>
<p>it all looks nice
then running the python script</p>
<pre><code>pi@raspberrypi ~ $ python test.py
Traceback (most recent call last):
  File &quot;test.py&quot;, line 96, in &lt;module&gt;
    run_program()
  File &quot;test.py&quot;, line 85, in run_program
    with serial.Serial('/dev/ttyUSB0',9600) as ser:
  File &quot;/usr/lib/python2.7/dist-packages/serial/serialutil.py&quot;, line 260, in __init__
    self.open()
  File &quot;/usr/lib/python2.7/dist-packages/serial/serialposix.py&quot;, line 276, in open
    raise SerialException(&quot;could not open port %s: %s&quot; % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyUSB0: [Errno 16] Device or resource busy: '/dev/ttyUSB0'
</code></pre>
<p>fixed by killing gpsd</p>
<pre><code>pi@raspberrypi ~ $ sudo killall gpsd
pi@raspberrypi ~ $ python test.py
</code></pre>
<p>on the server i now receive the data, looking good. (i push over socket instead of the print in answer).</p>
<pre><code>$GPGGA,111838.000,5543.1488,N,01229.2451,E,2,09,0.94,21.8,M,41.5,M,0000,0000*5A
$GPGSA,A,3,29,10,16,18,21,26,20,27,07,,,,1.60,0.94,1.30*09
$GPRMC,111838.000,A,5543.1488,N,01229.2451,E,0.55,102.49,270816,,,D*65
$GPVTG,102.49,T,,M,0.55,N,1.02,K,D*35
$GPGGA,111839.000,5543.1488,N,01229.2454,E,2,09,0.93,21.8,M,41.5,M,0000,0000*59
$GPGSA,A,3,29,10,16,18,21,26,20,27,07,,,,1.22,0.93,0.79*04
$GPRMC,111839.000,A,5543.1488,N,01229.2454,E,0.56,54.80,270816,,,D*55
$GPVTG,54.80,T,,M,0.56,N,1.05,K,D*06
$GPGGA,111840.000,5543.1489,N,01229.2456,E,2,09,0.93,21.8,M,41.5,M,0000,0000*54
$GPGSA,A,3,29,10,16,18,21,26,20,27,07,,,,1.22,0.93,0.79*04
$GPRMC,111840.000,A,5543.1489,N,01229.2456,E,0.43,12.04,270816,,,D*52
$GPVTG,12.04,T,,M,0.43,N,0.80,K,D*00
$GPGGA,111841.000,5543.1490,N,01229.2457,E,2,09,0.94,21.8,M,41.5,M,0000,0000*5B
$GPGSA,A,3,29,10,16,18,21,26,20,27,07,,,,1.60,0.94,1.30*09
$GPGSV,4,1,14,16,65,260,27,21,64,082,17,26,54,198,30,27,43,282,17*7B
$GPGSV,4,2,14,18,39,137,29,20,36,065,18,49,26,189,28,10,15,168,16*74
$GPGSV,4,3,14,07,13,334,16,29,10,101,15,08,08,283,,13,06,049,*73
$GPGSV,4,4,14,15,05,079,,05,02,025,*73
$GPRMC,111841.000,A,5543.1490,N,01229.2457,E,0.24,252.88,270816,,,D*69
$GPVTG,252.88,T,,M,0.24,N,0.44,K,D*3B
$GPGGA,111842.000,5543.1490,N,01229.2456,E,2,09,0.93,21.8,M,41.5,M,0000,0000*5E
$GPGSA,A,3,29,10,16,18,21,26,20,27,07,,,,1.22,0.93,0.79*04
</code></pre>
<p>After exiting the python script <code>sudo cat /dev/ttyUSB0</code> nothing happens anymore.
Is this because I did not close something correct in python?</p>