0

I am having some difficulties getting my program to communicate with the two Digi modems that I have.

def sockCon ():
global HOST
global PORT
global TX

TX = "\x7E\x00\x0C\x01\x00\xA5"

BUFFER = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
b = 1
while b == 1:        
    print ('T '+TX)
    s.send(TX.encode('latin-1'))        
    time.sleep(5)
    data = s.recv(BUFFER)
    print ('R '+decode(TX,'latin-1'), BUFFER)

Basically the problem is that when it sends it will either send completely wrong or it wont send at all and it will give me this error.

TypeError: 'str' does not support the buffer interface

2 Answers 2

1

you probably should not be doing that encode bit

TX = b"\x7E\x00\x0C\x01\x00\xA5"

should solve your problem (in python3 you need to send bytes not a string)

TX = b"\x7E\x00\x0C\x01\x00\xA5"
s.send(TX)
print( repr(s.recv(BUFFER)) )
Sign up to request clarification or add additional context in comments.

Comments

0

If you use Python3x then string is not the same type as for Python 2.x, you must cast it to bytes (encode it).

TX = "\x7E\x00\x0C\x01\x00\xA5"
s.send(bytes(TX, 'latin-1'))

1 Comment

I dont think he wants to actually encode as latin 1 ... I think he just wants to send it as bytes (although encoded might be the same) .. (yeah i just tested it encodes to exactly the same bytes with or without the latin1 bit :P)

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.