python通过TCP套接字发送/接收十六进制数据 [英] python send/receive hex data via TCP socket

查看:143
本文介绍了python通过TCP套接字发送/接收十六进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以太网访问控制设备,据说可以通过 TCP 进行通信.
如何通过输入 HEX 数据发送小包,因为这是我从他们的手册中获得的内容(每个命令后发送和接收的通信数据包的标准格式)
您能否展示一些示例代码或链接以开始使用....

<前>来自终端的标准返回包大小(字节)BS (0x08) : ASCII 字符 1STX (0x02) : ASCII 字符 1LENGTH : 从 BS 到 ETX 4 的长度TID : 系统唯一 I.D.1结果 1DATA : 返回参数 NCHECKSUM : 从 BS 到 DATA 1 的字节总和ETX (0x03) : ASCII 字符 1

<前>到终端的标准命令包大小(字节)ACK (0x06) : ASCII 字符 1STX (0x02) : ASCII 字符 1LENGTH : 从 ACK 到 ETX 4 的长度TID : 系统唯一 I.D.(例如:1)1命令 1访问密钥(可选) 6DATA : 命令参数 NCHECKSUM :从 ACK 到 DATA 1 的字节总和ETX (0x03) : ASCII 字符 1该数据包从 ACK 开始.在这个数据包中,多字节值必须从 MSB 开始.例如,如果长度为 10,则 LENGTH 为 0x00 0x00 0x00 0x0a.

解决方案

我会使用 struct.pack 准备要发送的字节串,从您要发送的数据.一定要以 > 开始打包格式,这意味着你想要大端排序和标准尺寸,因为它们记录得如此清楚!

所以(我不知道访问密钥的可选"是什么意思,我假设这意味着如果您没有访问密钥,该字段可以是全零字节),如果数据"已经是一个字节串和命令"一个小的无符号整数,例如……:

def stringfor(command, data, accesskey=''*6, tid=1):长度 = 16 + len(数据)prefix = struct.pack('>BBIBB6s', 6, 2, length, tid, command, accesskey)校验和 = sum(ord(c) for c in prefix) &0xFF返回前缀 + chr(校验和) + chr(3)

I have a ethenet access control device that is said to be able to communicate via TCP.
How can i send a pachet by entering the HEX data, since this is what i have from their manual (a standard format for the communication packets sent and received after each command)
Can you please show some example code or links to get started....

standard return packet from the terminal
                               Size (bytes) 
BS (0x08) : ASCII Character         1
STX (0x02) : ASCII Character        1 
LENGTH : length from BS to ETX      4 
TID : system unique I.D.            1 
RESULT                              1 
DATA : returned parameter           N 
CHECKSUM : byte sum from BS to DATA 1 
ETX (0x03) : ASCII Character        1 

Standard command packet to the terminal  
                               Size (bytes) 
ACK (0x06) : ASCII Character         1 
STX (0x02) : ASCII Character         1 
LENGTH : length from ACK to ETX      4 
TID : system unique I.D. (ex: 1)     1 
COMMAND                              1 
Access Key(Optional)                 6 
DATA : command parameter             N 
CHECKSUM : byte sum from ACK to DATA 1 
ETX (0x03) : ASCII Character         1 

This packet starts from ACK. 
In this packet, multiple byte value must be started from MSB. 
For example, if length was 10, LENGTH is 0x00 0x00 0x00 0x0a. 

解决方案

I'd use struct.pack to prepare the string of bytes to send, from the data you want to send. Be sure to start the packing format with > to mean you want big-endian ordering and standard sizes, since they document that so clearly!

So (I don't know what the "optional" means for the access key, I'll assume it means that the field can be all-zero bytes if you have no access key), if "data" is already a string of bytes and "command" a small unsigned integer for example, something like...:

def stringfor(command, data, accesskey=''*6, tid=1):
  length = 16 + len(data)
  prefix = struct.pack('>BBIBB6s', 6, 2, length, tid, command, accesskey)
  checksum = sum(ord(c) for c in prefix) &0xFF
  return prefix + chr(checksum) + chr(3)

这篇关于python通过TCP套接字发送/接收十六进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆