Domanda

I'm writing a simple python script to catch messages from gps trackers and write messages to server's db.

# -*- coding: utf-8 -*-
import socket, string
import MySQLdb

def write_message(x):
    db = MySQLdb.connect(host="localhost", user="root", passwd="****", db="****", charset='utf8')
    cursor = db.cursor()
    sql = '''INSERT INTO test(test_c) VALUES ('%(test_c)s') '''%{"test_c":x}
    cursor.execute(sql)
    db.commit()
    db.close()
    return x  

HOST = ""
PORT = 50007

srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.bind((HOST, PORT))
while 1:
    print u"Listen to " + str(PORT)
    srv.listen(1)             
    sock, addr = srv.accept()
    while 1:
        pal = sock.recv(2048)
        if not pal: 
            break
        print u"Recieved %s:%s:" % addr, pal
        lap = write_message(pal)
    sock.close()

To test script I use my home pc with the script

# -*- coding: utf-8 -*-
import socket

HOST = '****'   
PORT = 50007            
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
mes = u'a'
s.sendall(mes)
s.close()

Everything goes fine and server writes messages to db. With utf-8 they are full of unicode characters and it's ok. But when I use gps tracker I catch some strange messages like "xxSA░7d└╣" ('xx\x11\x01\x03SA\x907d\x84 \x10\x0b2\x01\x00\x01\\xa7\r\n') but the documentation says that messages contain ascii symbols only. I've found that there is "socket.SOCK_RAW" option and tried to use it, but got "socket.error: [Errno 93] Protocol not supported".

Do you know what is the solution?

The device is GT03b.

È stato utile?

Soluzione

The server commands for the GT03B are ASCII, but the rest of it (protocol numbers, checksums etc.) is just a bytestream. Your "strange message", for example, appears to be a normal LBS information package.

Refer to the GT03B protocol for more information.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top