RFID using Python Socket ( Low Level Commands)

868 views Asked by At

SAAT-520Wireless-Communication-Programming-Development-ProtocolI've been trying to send Lowlevel commands to my RFID to get device information. I'm using wireshark to tap the connection and it seams to be fine as packets seem to go and come from my PC to RFID device and vice versa.

But I'm unable to see any response or output on my program. On the device note I have response command, I'm not sure should I and/or where I can use command response.

Import socket

TCP_IP = '192.168.0.238'
TCP_PORT = 7086
BUFFER_SIZE = 20
MESSAGE = '0x55, 0x00~0xFF, 0x01, 0x00, 0x00~0xFF'

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect((TCP_IP, TCP_PORT))
except:
    print ('failed' + TCP_IP, 'down')
s.sendall(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()
print 'received', repr(data)

I don't understand why there is no response from my program. No error nor program is successful. Just process never ends.

Also please find the notes for commands in the attachment.

System Information Query Command (0x01H)

img1

Command Response

img2

1

There are 1 answers

3
SteveJ On

(From your question)

s.sendall(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()

You are sending data via send, immediately asking for the response data, then closing the socket. Enough time will not have passed for you to get a response. For testing, try placing a time delay between.

import time
s.sendall(MESSAGE)
time.sleep(5)
data = s.recv(BUFFER_SIZE)
print(data)
s.close()

You can also wait for the incoming data;

import time
s.sendall(MESSAGE)
while data is None:
    data = s.recv(BUFFER_SIZE)
    print "No data, waiting...."
    time.sleep(0.5)

print "Data arrived"
incoming = data.decode('utf-8')
print incoming
# Process Data

I did personally have an issue if my buffer size was too small -- I had to catch the timeout exception to deal with it. Perhaps try increasing your buffer size.

Below is something close to the actual code I use for my application; It python 3.x, and you will have to condition it a bit -- but it works for me.

            try:
                data = s.recv(buf_len)
                if data is not None and len(data) > 0:
                    while True:
                        new_data = s.recv(buf_len)
                        if new_data is None or len(new_data) == 0:
                            break
                        data = data + new_data

            except socket.timeout:
                if data is not None and self.callback is not None:
                    logger.debug(f"Received packet: {data}")   

incoming = data.decode('utf-8')
print incoming
# Process Data