python通过TCP套接字发送/接收十六进制数据
我有一个以太网访问控制设备,据说可以通过 TCP 进行通信。
如何通过输入十六进制数据来发送数据包,因为这是我从他们的手册中获得的内容(每个命令后发送和接收的通信数据包的标准格式)
您能否显示一些示例代码或链接以开始使用......
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将十六进制数据编码为字符串即可:
Just encode the hex data in a string:
我会使用 struct.pack 来根据要发送的数据准备要发送的字节字符串。请务必以
>
开始打包格式,这意味着您需要大端排序和标准大小,因为它们记录得非常清楚!所以(我不知道“可选”对于访问密钥意味着什么,我假设这意味着如果没有访问密钥,该字段可以是全零字节),如果“数据”已经是一个字符串字节和“命令”一个小的无符号整数,例如......:
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...: