Reputation: 21
I am currently building a server for GPS tracking using Teltonika Codec 8 protocol. While I have read all the relevant documentation, I am facing an issue where I am unable to move to the next record beyond record 13. I have received the IMEI and AVL packet successfully, but the protocol seems to be stuck at record 13.
**Code: **
`def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
imei = conn.recv(1024)
print(imei)
try:
message = '\x01'
message = message.encode('utf-8')
conn.send(message)
connected = False
except socket.error as e:
print(f"Error sending reply. Maybe it's not our device {addr}: {e}")
connected = False
try:
data = conn.recv(1024)
recieved = binascii.hexlify(data)
print(data)
print(recieved)
decodethis(recieved)
record = getrecord(recieved)
message = "0000" + str(record).zfill(4)
print(record)
message = message.encode('utf-8')
conn.send(message)
conn.close()
connected = False
except socket.error as e:
print(f"Error receiving data from {addr}: {e}")
print("Error Occured.")
connected = False
break
conn.close()`
I have consulted the documentation and found that I need to send "00000013" to inform the protocol that I have received the 13th record. However, I am unsure how to proceed with this. Could someone please provide me with some guidance or sample code to help me move to the next record beyond record 13?
Followed these links: https://community.teltonika-gps.com/4965/how-to-read-data-from-teltonika-fmb001-with-a-python-script https://wiki.teltonika-networks.com/view/VCode#Codec_8
Devices: FMB110 FMB140
Thank you in advance for any assistance you can provide.
Upvotes: 0
Views: 1363
Reputation: 148
kinda late response, if you still need help with this, check this code:
def codec8UDP(data):
response = "0005%s01%s%s"%(
data["packetId"],
data["AVLPacketId"],
hex(data["TotalRecords"])[2:].zfill(2)
)
return response.upper()
def codec8TCP(data):
response = "000000%s"%(
hex(data["TotalRecords"])[2:].zfill(2)
)
return response
Both functions return the ACK message for TCP and UDP, you can adapt them to better fit your code as well.
I also suggest using this documentation for code implementation: https://wiki.teltonika-gps.com/view/Codec#Codec_8
I'm not sure what you mean by "...stuck at record 13", but if you are referring to the amount of records received in the same message, you just need to send back to device "0000000D".
Also, if you are going to use TCP connections, you don't need (and shouldn't) close connection for each data the device sends. You can leave the connection open. If you prefer to "close" connection, just use UDP.
By the way, I highly recommend you to use asyncio for your listener.
class TeltonikaTCPListener(asyncio.Protocol):
... stuff ...
class TeltonikaUDPListener():
... stuff ...
Class Server():
def __init__(self, kwargs**):
... stuff ...
def start():
self.loop = asyncio.new_event_loop()
listenUDP = self.loop.create_datagram_endpoint(
lambda: TeltonikaUDPListener(<your_vars>,
local_addr=(<IP>, <PORT>),
reuse_port=True) # ONLY WORK IN UNIX SYSTEM, FOR WINDOWS REMOVE reuse_port
listenTCP = self.loop.create_server(
lambda: TeltonikaTCPListener(<your_vars>,
host=<IP>, port=<PORT>,
start_serving=True
)
try:
self.transport_tcp = self.loop.run_until_complete(listenTCP)
self.transport, protocol = self.loop.run_until_complete(listenUDP)
self.loop.run_forever()
except (KeyboardInterrupt, Exception) as E:
self.logger.exception("CLOSING SERVER")
The code above gives you a brief start, you need to implement some main methods and routines. By following the links bellow you can implement it wihtout problems: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_datagram_endpoint
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_server
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_until_complete
Upvotes: 1
Reputation: 131
Do you ask what response send to device for inform it that 13 records are received? Probably You need to send 4 bytes 0x0000000D which means 13 decimal.
Upvotes: 1