pyserial - Python not releasing serial port -
i open serial port, read parameters modem (omited here simplicity), close serial port , call sakis3g, script connect internet through serial or usb modem.
i can connect calling sakis3g console.
the code:
# -*- coding: iso-8859-1 -*- import serial import time subprocess import call port = serial.serial("/dev/ttyama0", baudrate=9600, timeout=0) port.open() at_ok=false #wait until modem answers ok while at_ok==false: port.write(bytes("at\n", 'iso-8859-1')) time.sleep(.5) r=port.read(1000) if len(r)>0: if r.decode("iso-8859-1").find("ok")>0: print ("connected modem") at_ok=true port.close() time.sleep(1) #just in case call(["sakis3g","connect","--pppd"])
the output:
root@raspberrypi:~# python3 /usr/test2.py connected modem port /dev/ttyama0 occupied 14057 python3. failed connect.
(i'm using raspberry) edit: used port.isopen() before call, , returns false.
this part of output of sakis3g when use --debug option:
[20574] [16:57:04] root already. proceeding. [20574] [16:57:04] device /dev/ttyama0 occupied 0 process(es). [20574] [16:57:04] pid(s) are: 20542 /------------------------------------------------------------------------------- [20574] [16:57:04] run command: \'/bin/ps -p 20542 -o pid,comm= | /usr/bin/tail -1\' /------------------------------------------------------------------------------- 20542 python3 \------------------------------------------------------------------------------- [20574] [16:57:04] command returned 0. \------------------------------------------------------------------------------- [20574] [16:57:04] wait 10 seconds in case port freed. [20574] [16:57:05] verbosing: 21% waiting /dev/ttyama0 released pid 20542 python3. [20574] [16:57:05] wait 10 seconds in case port /dev/ttyama0 freed.
edit2: think i've found problem (not solution): made python script folowing: - wait 10 seconds - open serial port - wait 10 seconds - close serial port - wait forever
using lsof|grep ttya
get:
before opening port:
root@raspberrypi:~# lsof|grep ttya root@raspberrypi:~#
when port open:
root@raspberrypi:~# lsof|grep ttya python3 530 root 3u chr 204,64 0t0 9 /dev/ttyama0 python3 530 root 4u chr 204,64 0t0 9 /dev/ttyama0 root@raspberrypi:~#
after port closes:
root@raspberrypi:~# lsof|grep ttya python3 530 root 3u chr 204,64 0t0 9 /dev/ttyama0 root@raspberrypi:~#
so pyserial or python keeping port occupied, not locked?