0

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/ttyUSB3

I 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?

1

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.

  • thank you, that is perfect. I should have mentioned python in my OP. If I do as you suggest re naming ports, it would think any usb0-9 was a modem? I will have a few other usb interfaces that are not, so is it about finding a unique parameter in that type of interface? – Paul Jan 27 '15 at 11:03
0
read modem_output < /dev/ttyUSB3
  • I may have phrased it badly. How would I do this in one command in one terminal? So i would have echo -e "ATZ\r" > /dev/ttyUSB3 but also some way to redirect the results? Is that possible? Or do I misunderstand your answer? – Paul Jan 26 '15 at 15:56
  • @Paul I don't know what you mean by "redirect". Something must read the data from the modem. In your example that was done by cat. But if the shell reads the data itself then there is no need for redirection. There is input redirection in this case, though. – Hauke Laging Jan 26 '15 at 16:04
  • By redirect I mean store in a variable/file. I want the data stored after it is read from the modem so that I can manipulate it. I am using python to make the call os.system("echo -e..."). Now what confuses me is do i have to use this read command concurrently in python to running the os.system call or how would I know which data is associated with which command? – Paul Jan 26 '15 at 16:43
  • Sorry if this is confusing, I am struggling getting my thoughts across. – Paul Jan 26 '15 at 16:43
  • 1
    @Paul You open the file /dev/ttyUSB3 in Python and read from it. Python questions should not be asked here but on Stackoverflow. – Hauke Laging Jan 30 '15 at 2:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.