我发现这段代码用于使用串行端口进行通信的简单聊天,我想看看它是否有效。我用谷歌搜索了一段时间,发现 com0com 可以模拟 RS-232 连接,因为我没有电缆,但现在我很困惑,不知道如何实际完成这项工作
代码(在python中):
from serial import *
from threading import Thread
class Receiver(Thread):
def __init__(self, serialPort):
Thread.__init__(self)
self.serialPort = serialPort
def run(self):
text = ""
while (text != "exit\n"):
text = serialPort.readline()
print ("\n machine1: " + text)
self.serialPort.close()
class Sender(Thread):
def __init__(self, serialPort):
Thread.__init__(self)
self.serialPort = serialPort
def run(self):
text = ""
while(text != "exit\n"):
text = raw_input("Type your message>>") + "\n"
self.serialPort.write(text)
self.serialPort.close()
serialPort = Serial("\\\\.\\CNCA0")
send = Sender(serialPort)
receive = Receiver(serialPort)
send.start()
receive.start()
提前致谢。