我一直在使用pyserial gitHub repo中的tcp_serial_redirect.py示例。 我想将接收到的数据记录在liteSQL数据库中,但是因为任何将数据复制到全局(或类)变量的尝试都失败,并且进程在复制点停止时失败。超出我想要做的事情:
class SerialToNet(serial.threaded.Protocol):
"""serial->socket"""
def __init__(self):
self.socket = None
def __call__(self):
return self
serBuf = ""
def buffer_data(self, data):
# Printing works
print("Recieved data {}".format(data))
# Manipulating `data` works
data += data.upper()
# Does not work
#serBuf += data.replace(b'\x00', b'')
#partial_lines = serBuf.split(b'\r\n')
#serBuf = partial_lines[-1]
#return partial_lines
# or
# addLineToLiteSQL(partial_lines)
def data_received(self, data):
# buffer the data:
self.buffer_data(data)
if self.socket is not None:
self.socket.sendall(data)
我原本希望我可以连接data
并在它是一个完整的行时处理它。 SerialToNet
用作:
ser_to_net = SerialToNet()
serial_worker = serial.threaded.ReaderThread(ser, ser_to_net)
serial_worker.start()
我确信它应该是显而易见的。用t& e敲打我的脑袋太久了8)