how can i connect and send commands to a serial port while another program is using it?

817 views Asked by At

I am using a telit he910g card. it is connected to my PC directly using a miniPCI slot. I am using it for 3G internet connection and A-GPS/GPS services.

My system is running linux mint 17.1, the 3G connection is handled using the network manager APP and works great. The 3G connection is started and handled using a module that is part of a program I am writing. The code I am using in order to connect to the serial port is this:

def _connect_to_device(self):
    """ Connect to a serial port """

    try:
        self._device = serial.Serial(self._filename, baudrate=self._baud_rate)
    except StandardError, e:
        raise StandardError("Couldn't connect to GPS device. Error: %s" % str(e))

When I use the python program alone it works great. But when I try and use it while the 3G is on i cant connect to the serial device. The wierd thing is that if I try to connect to it using a program like "minicom" while 3G is turned on it DOES work.

So my question is: how can I make both run and work together? since now they are mutually exclusive.

thanks to all who help. :)

2

There are 2 answers

1
meuh On

Glad you found a way round your problem. Just for completeness:

Normally, serial ports can be opened by multiple processes. If one of them does ioctl(,TIOCEXCL) on the open file then further opens will return EBUSY until everyone closes the device. Only root can get past this and open the device at all times.

If root opens the device and does an ioctl(,TIOCNXCL), then other processes can open the device too.

In python, TIOCNXCL isnt defined anywhere, but you can do the ioctl (eg on stdin) with:

import fcntl
TIOCEXCL = 0x540c # from /usr/lib64/perl5/asm-generic/ioctls.ph
TIOCNXCL = 0x540d
print fcntl.ioctl(0, TIOCNXCL)
0
elor arieli On

Ok, so it is solved.

the issue was that the telit module has 2 ports /dev/ttyACM0 (high speed) and /dev/ttyACM3 (lower speed). I tried to connect to the high speed one, but apparently the 3G uses that one and it causes contentions. So moving to use the lower speed port in my script solved the issue.