Python 如何使用socat通过TCP/IP传输脚本中的串行命令?
我有一个web应用程序,它会不断地发布一些查询,并通过Raspberry Pi上的串行端口从设备获得回复。像这样:Python 如何使用socat通过TCP/IP传输脚本中的串行命令?,python,tcp,serial-port,raspberry-pi,ethernet,Python,Tcp,Serial Port,Raspberry Pi,Ethernet,我有一个web应用程序,它会不断地发布一些查询,并通过Raspberry Pi上的串行端口从设备获得回复。像这样: @cherrypy.expose def login (self, **data): passcode = data.get("passcode", None) print "logging in using passcode %s"%passcode ,type(passcode) import serial import time
@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)
reply=serialport.readlines(1)
print reply, type(reply)
#check if log in success
if not "00" in reply[0]:
if "FE" in reply[0]:
state="engineer"
else:
state="user"
else:
state="none"
print state
time.sleep(1)
return state
目前,我正在使用从Raspberry Pi到设备(gnd、rxd、txd)的直接3线(gnd、rxd、txd)。Raspberry Pi的串行端口为/dev/ttyam0,表示该设备的IP为10.0.0.55。我可以一次发送一个netcat命令,如下所示:
nc 10.0.0.55 1001
^CLI1234^M //this is an example of the serial command sent to the device from Raspberry Pi terminal
我应该如何使用
**socat**
?我不想更改脚本,只想操纵socat(或其他)中的一些配置,将命令转发到设备的IP。我想去掉连接到R-Pi UART的3根电线。我几乎找不到符合这一要求的socat示例(或者可能是我不知道应该使用什么关键字)。请启发我:)非常感谢 与您的nc
命令相当的socat
将是socat STDIN TCP:10.0.0.55:1001
,但我非常困惑这与访问串行端口目录的CherryPy应用程序有什么关系。该应用程序似乎在任何地方都没有对端口1001上的TCP的任何引用!是的,目前还没有,所以我想知道怎么做对于nc,我是说我可以用这种方式连接它,但不能用cherrypy或任何python脚本,只需一行接一行的命令?我的理解是,您有一些设备和RPI通过串行端口连接在一起。你有一个简单的应用程序,在RPI上运行,从串口读/写。Socat只是几乎任何类型的两个文件描述符之间的桥梁。如果您只想通过TCP连接到RPI并通过该TCP连接转发串行连接数据,则socat将起作用。您可以使用以下命令:。另外,我不明白您希望如何摆脱串行线。@ChrisKoston谢谢您的评论。你说得对,这就是我想要实现的目标。因为如果可以通过以太网连接,连接串行线似乎是多余的?所以我想我可以把它去掉。因此,为了澄清,我可以运行python脚本(它将发送串行命令),并进行一些配置以将命令转发到以太网端口?您的设置是这样的:DevicePiclient???