我有一个Python脚本,当在终端中运行时,将启动CherryPy Web服务器。在脚本中,我通过导入serial
使用pyserial,然后打开端口/dev/ttyAMA0,我可以发送任何串行命令。
@cherrypy.expose
def login (self, **data):
passcode = data.get("passcode", None)
print "logging in using passcode %s"%passcode ,type(passcode)
import serial
import time
#open connection
serialport=serial.Serial ("/dev/ttyAMA0", 9600, timeout=0.5)
#write in user sign in code
serialport.write("\x03LI%s\x0D"%passcode)
#get reply
reply=serialport.readlines(1)
print reply, type(reply)
然而,由于有以太网端口并且我可以使用Netcat向类似的设备发送串行命令,那么我该如何让此脚本通过以太网端口发送命令而不是串行端口?我需要更改什么?
很抱歉,但我真的不知道该怎么做。我已经在Google上搜索过了,但找不到答案。:(
socat
是你的好朋友 ;) - hek2mglnetcat
不是完全相同的工具。你可以使用 Python 连接到一个本地 TCP socket,但需要通过 socat 将字节流重定向到串口设备上。Python甚至无法识别此操作。当然,也会有真正的Python解决方案。请注意,我不是Python专家,但我们在工作中曾经使用过“socat方式”完成此操作 :) 这就是为什么我留下了这个评论。 - hek2mgl