[英]Python TCP Socket Programming
I am having some difficulties getting my program to communicate with the two Digi modems that I have. 我的程序无法与我拥有的两个Digi调制解调器进行通讯时遇到了一些困难。
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
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) 应该可以解决您的问题(在python3中,您需要发送字节而不是字符串)
TX = b"\x7E\x00\x0C\x01\x00\xA5"
s.send(TX)
print( repr(s.recv(BUFFER)) )
If you use Python3x then string is not the same type as for Python 2.x, you must cast it to bytes (encode it). 如果使用Python3x,则string与Python 2.x的字符串类型不同,必须将其强制转换为字节(将其编码)。
TX = "\x7E\x00\x0C\x01\x00\xA5"
s.send(bytes(TX, 'latin-1'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.