I am echoing an AT command to a modem interface (/dev/ttyUSB3) and the command runs successfully.For instance the below command works and has an output of OK:echo -e ATZ\r > /dev/ttyUSB3I can see this by having another terminal with cat /dev/ttyUSB3 active.How do I, instead of using cat (eg use one command to send a command and get output back), redirect the output OK to a variable or file so that I can process data returned from the modem?
redirect output from interface?
bash;shell;io redirection
You should use the serial device much like a normal file. The only difference is that it needs some ioctl()s to do the speed and control line setup.So don't use os.system(echo ... but f = open('/dev/ttyUSB3', 'rw') and then f.write() and f.read().In theory you could use ioctl() to set the speed and so on, but at that stage it's simply easier to use pySerial than to do all of the parameter marshalling yourself. ser = serial.Serial(port='/dev/ttyUSB3', baudrate=9600, timeout=1, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS) with ser.write() and ser.read().Note that you should use udev to set a unique name for the serial port, rather than hard-coding /dev/ttyUSB3. Here's how to do that for a single USB/RS-232 adapter and here's how to do that for a multiport USB/RS-232 adapter. 