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
halfer
20.2k20 gold badges111 silver badges208 bronze badges
2 Answers 2
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)) )
answered Jun 29, 2015 at 19:10
Joran Beasley
114k13 gold badges168 silver badges187 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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'))
answered Jun 29, 2015 at 19:15
user 12321
2,9161 gold badge29 silver badges34 bronze badges
1 Comment
Joran Beasley
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)
lang-py