1

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. 
2
  • and when i get the data sent with s.send, i get the response from s.recv, how can i display what comes back in HEX with "print" ? Commented Mar 14, 2010 at 16:22
  • you use struct.unpack (for the 8-byte start of the response packet) -- again, ensuring that you're getting the whole response is key as TCP is a stream protocol, NOT a datagram protocol, so might arbitrarily truncate things -- not likely, but why risk it? Just as you should use sendall instead of send to make sure the whole packed is transmitted, as I mentioned in reply to your comment on my answer. Commented Mar 14, 2010 at 17:23

2 Answers 2

6

Just encode the hex data in a string:

'\x34\x82\xf6'
Sign up to request clarification or add additional context in comments.

2 Comments

and if the hex data does not have an ASCII equivalent is this a problem ?
You're representing the raw bytes. str does not care whether or not it's ASCII; only things like print do.
4

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='\0'*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)

1 Comment

@Mike, sure, or s.sendall (probably better as you want to be 100% sure all the packet is sent -- but then the MTU of your connection is probably large enough that this makes no difference), see docs.python.org/library/… .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.