Найти - Пользователи
Полная версия: Отправка бинарного пакета через сокет
Начало » Network » Отправка бинарного пакета через сокет
1
_alexs_
Пытаюсь написать небольшой скрипт для обмена данными с устройством. Есть вот такое описание протокола

First when module connects to server, module sends its IMEI. IMEI is sent the same way as encoding barcode. First comes short identifying number of bytes written and then goes IMEI as text (bytes). For example IMEI 123456789012345 would be sent as 000F313233343536373839303132333435 After receiving IMEI, server should determine if it would accept data from this module. If yes server will reply to module 01 if not 00. Note that confirmation should be sent as binary packet.

Then module starts to send first AVL data packet. After server receives packet and parses it, server must report to module number of data received as integer (four bytes). If sent data number and reported by server doesn’t match module resends sent data.

Не подскажете, как отправить бинарный пакет с нужным ответом? Пробовал отправлять как b'\x00\x01' и как b'01' но ответ от устройства не получаю. Полный код ниже

 # -*- coding: utf-8 -*-
import socketserver
HOST = "localhost"
PORT = 9999
class MyHandler(socketserver.BaseRequestHandler):
    def handle(self):
        self.data = self.request.recv(1024).strip()
        self.data = buff
        print("Received from client '{}':".format(self.client_address[0]))
        print(self.data)
        if self.data[2:] == b'351513052186415':
            print("Valid client")
            self.request.sendall(b'\x00\x01')
       # parse AVL data packet and confirm
        if self.data[:4] == b'\x00\x00\x00\x00':
            print("Found data packet")
            d = self.data[9:10]
            print("Confirming that data recieved")
            self.request.sendall(d)
if __name__ == "__main__":
    server = socketserver.TCPServer((HOST, PORT), MyHandler)
    print("TCP server started. Press 'Ctrl+C' to stop.")
    server.serve_forever()
hitrec322
Такая же проблема…
py.user.next
_alexs_
Пробовал отправлять как b'\x00\x01' и как b'01' но ответ от устройства не получаю.
Надо послать либо b'\x00' либо b'\x01'.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB