Pyton – Stefano Allegri https://www.allegristefano.eu la cooperazione è alla base di una buona progettazione Sat, 21 Mar 2020 15:42:19 +0000 it-IT hourly 1 https://wordpress.org/?v=5.3.15 Seriale over TCP/IP con modulo Telit GM-863-PY https://www.allegristefano.eu/2020/03/21/seriale-over-tcp-ip-con-modulo-telit-gm-863-py/ https://www.allegristefano.eu/2020/03/21/seriale-over-tcp-ip-con-modulo-telit-gm-863-py/#respond Sat, 21 Mar 2020 15:36:45 +0000 https://www.allegristefano.eu/?p=41 Il dispositivo consente di connettere una linea di comunicazione seriale RS232 remota al PC sfruttando la connettività internet del PC e la connessione GPRS del modulo GM-863-PY. Il modem GM-863 è programmato in Pyton con lo script disponibile al termine dell’articolo. Dato l’indirizzo IP della SIM presente nel dispositivo remoto il PC si connette via socket […]

L'articolo Seriale over TCP/IP con modulo Telit GM-863-PY proviene da Stefano Allegri.

]]>
Il dispositivo consente di connettere una linea di comunicazione seriale RS232 remota al PC sfruttando la connettività internet del PC e la connessione GPRS del modulo GM-863-PY.

Il modem GM-863 è programmato in Pyton con lo script disponibile al termine dell’articolo.

Dato l’indirizzo IP della SIM presente nel dispositivo remoto il PC si connette via socket alla porta 1500 utilizzando un client via socket come ad esempio HyperTerminal.

Il PC può quindi inviare comandi e riceve risposte direttamente allo strumento connesso allla seriale RS232 del modulo Telit GM-863.

Qualore la SIM dati non disponga di un indirizzo IP statico si può inplementare nel modulo la trasmissione dell’indirizzo IP, ottenuto al momento dell’attivazione del contesto GPRS, verso un server che lo associ ad un host name, come ad esempio www.noip.com

E’ altresì possibile creare un file di configurazione da inserire nel modulo per fare in modo da poter configurare i principali parametri come APN, numero della porta sulla quale aprire il server locale ecc…

Di seguito l’esempio di conice Pyton da compilare ed inserire nel modulo Telit:

# Allegri Stefano
#
# www.allegristefano.eu
# All rights reserved.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS“AS IS” AND ANY EXPRESS OR IMPLIED
#WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
#DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#POSSIBILITY OF SUCH DAMAGE.
#

“””
SKT.py
gennaio 2014

1) Script configuration (IP address, local ports, APN).
2) Socket configuration and GPRS context activation
3) Start Server mode configuring firewall and socket listen (skt1)
4) GPRS connection; every characted sent on the serial port of one module are received on the remote other port in trasparent way.
5) Re-start the socket when the DCD low status is detected.
“””

import MOD
import SER
import MDM

# ########################################################### #
# this values must be configured with valid ones..
# the ones here are only for demo.
# this is the network name to register to in manual mode
NETWORK     = “I WIND”  #  “I WIND”   “I TIM”   “vodafone IT”
PORT        = “1500”
APN         = “myinternet.wind”
#
# ########################################################### #

BAUD = “115200”

# ################# timeout definitions #######################
TIMEOUT_REG = 300
TIMEOUT_PIN = 100
TIMEOUT_MIN = 10
TIMEOUT_CMD = TIMEOUT_MIN * 10

print ‘SKT.py\r\n© 2014 Allegri Stefano www.allegristefano.eu\r’

MOD.sleep(40)
a = SER.set_speed(BAUD,’8N1′)



#get network registration status
res = MDM.send(‘AT+COPS?\r’,0)
res = MDM.receive(50)
if( res.find(‘,’) == -1):
print ‘No network\r’
else:
print res

# only for simulated environment
# answer = raw_input(‘Do you want to register? Answer Y or N -> ‘)
# if (answer == “Y”):
# network = raw_input(‘network->’)
if 1:
network = NETWORK
res = MDM.send(‘AT+COPS=1,0,”‘,0)
res = MDM.send(network,0)
res = MDM.send(‘”\r’,0)
res = MDM.receive(TIMEOUT_REG)
res = MDM.send(‘AT+COPS?\r’,0)
res = MDM.receive(50)
if( res.find(‘,’) == -1):
print ‘No network\r’
else:
print res
MOD.sleep(50)
# Here start the GPRS session

s=”
while (s.find(‘OK’)==-1) :
a = SER.send(‘\rDEBUG_INFO : FIREWALL OFF\r’)# debug info
a = MDM.send(‘AT#FRWL=2\r’,10)
s = MDM.receive(50)
a = SER.send(s)  # debug info                      
MOD.sleep(10)

#Configura il contesto GPRS
s=”
while (s.find(‘OK’)==-1) :    
a = MDM.send(‘AT+CGDCONT=1,”IP”,”‘,0)
a = MDM.send(APN,0)            # insert APN of the operator in use (e.g.TIM)  ‘IBOX.TIM.IT’
a = MDM.send(‘”\r’,0)
s = MDM.receive(20)    
MOD.sleep(10)

#attiva il firewall con permessi d’accesso da tutti gl’indirizzi IP
s=”
while (s.find(‘OK’)==-1) :      # set firewall
a = SER.send(‘\rDEBUG_INFO : FIREWALL ON\r’)# debug info
a = MDM.send(‘AT#FRWL=1,”1.2.3.4″,”0.0.0.0″\r’, 10)
s = MDM.receive(50)
a = SER.send(s)     # debug info
MOD.sleep(10)

#attiva il contesto GPRS
MOD.sleep(50)    
s=”

while (s.find(‘#SGACT’)==-1) :
a = SER.send(‘\rDEBUG_INFO : GPRS context\r’)# debug info
a = MDM.send(‘AT#SGACT=1,1\r’, 10)     # GPRS context activation
s = MDM.receive(50)
a = SER.send(s)        # debug info
MOD.sleep(10)

strIP = s[10:s.find(‘\r’,5)] # ritorna l’indirizzo IP assegnato dalla rete GPRS…
a = SER.send(‘\rIndirizzo IP :’) # debug info    
a = SER.send(strIP)         # debug info    
a = SER.send(‘\r’)         # debug info    

#attende l’attivazione del contesto GPRS
while (s.find(‘OK’)==-1) :     # activate the socket listen
s = MDM.receive(10)
MOD.sleep(3)
a = SER.send(‘\rDEBUG_INFO : GPRS attivo\r’)# debug info

# Invia a server greentest l’indirizzo IP acquisito dalla rete…
s=”
a = SER.send(‘\rDEBUG_INFO : invia a server greentest indirizzo IP del modulo\r’)   # debug info
a = MDM.send(‘AT#SKTD=0,50000,”greentest.dyndns-ip.com”,0,0\r’, 10)                 # trasmissione dell’indirizzo IP
while (s.find(‘CONNECT’)==-1) :
s = MDM.receive(50)
a = SER.send(s)        # debug info
MOD.sleep(10)

#invia la stringa contenente i dati dell’ IMEI e dell’ IP
a = MDM.send(‘Modulo Telit ‘+ strIP +’\r\n’, 10)        # il modulo trasmette il proprio indirizzo IP
MOD.sleep(10)
a = MDM.send(‘+++’, 10)                                 # il modulo chide la trasmissione

while (s.find(‘NO CARRIER’)==-1) :
s = MDM.receive(50)
a = SER.send(s)        # debug info
MOD.sleep(10)

flagSCK = 1
while (flagSCK):
s=”
while (s.find(‘OK’)==-1) :     # activate the socket listen
a = SER.send(‘\rDEBUG_INFO : SKT LISTEN\r’)# debug info
a = MDM.send(‘AT#SL=1,1,’ + PORT + ‘,0\r’, 10)
s = MDM.receive(50)
a = SER.send(s)     # debug info
MOD.sleep(10)


s=”
while (s.find(‘CONNECT’)==-1) :     # wait for incoming connection indicated
s = MDM.receive(20)            # by SRING to answer and open the socket
if (s.find(‘SRING’)!=-1) :
a = SER.send(‘\rDEBUG_INFO : SKT ANSW\r’)# debug info
a = MDM.send(‘AT#SA=1\r’, 10)
s = MDM.receive(50)
a = SER.send(s)     # debug info
else:  
MOD.sleep(10)

n = 0
b = “”
while MDM.getDCD() == 1: # testa se la connessione è sempre attiva
b = SER.read()
s=””
res = MDM.send(b, 10)
s = MDM.read()
res = SER.send(s)
MOD.sleep(2) # 50
SER.set_speed(BAUD,’8N1′)    # this instruction reinitialize SER if got garbled

s=”
while (s.find(‘NO CARRIER’)==-1) :         # attende fino a che non trova NO CARRIRE – il client remoto ha terminato la connessione
s = MDM.receive(20)            # by SRING to answer and open the socket
a = SER.send(‘\rDEBUG_INFO : NO CARRIER\r’)# debug info

L'articolo Seriale over TCP/IP con modulo Telit GM-863-PY proviene da Stefano Allegri.

]]>
https://www.allegristefano.eu/2020/03/21/seriale-over-tcp-ip-con-modulo-telit-gm-863-py/feed/ 0