简体   繁体   English

Arduino,Python,Pyserial和套接字

[英]Arduino, python, pyserial and socket

I am trying to write a simple webserver on an Arduino to test a few things, but I couldn't find my Arduino with Ethernet on it. 我正在尝试在Arduino上编写一个简单的Web服务器来测试一些东西,但是找不到带有以太网的Arduino。

"No worries" I thought, "I'll just write a socket server in python that acts as a proxy for the serial connection". “不用担心”,我想,“我只是用python编写一个套接字服务器,充当串行连接的代理”。

import socket
import serial
host = ''
port = 8001
buffSize= 1024
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serverSocket.bind((host, port))
serverSocket.listen(1)
ser = serial.Serial('COM3', 115200, timeout=None, dsrdtr =False,rtscts =False,xonxoff =False)
print "Listening..."
send = ""
while 1:
    conn, remoteAddr = serverSocket.accept()
    print "Connection...."
    data = conn.recv(buffSize)
    print "Recieved"
    ser.write("%s\n"%data)
    print "Sent"
    print "Attempting to get reply"
    while ser.inWaiting()>0:
            conn.send( ser.read())
    conn.close()
serverSocket.close()

However, whatever I try, it seems that the connection made by the browser resets randomly and I'd get multiple rows of data. 但是,无论我尝试什么,似乎浏览器建立的连接都是随机重置的,并且我会得到多行数据。 And the script resets the Arduino each time it connects or disconnects from the serial port. 每次连接或断开串口时,脚本都会重置Arduino。 I tried using RealTerm and I got a proper answer, but python and serialness is just a mess. 我尝试使用RealTerm并得到了正确的答案,但是python和串行性只是一团糟。

Can anyone help me? 谁能帮我?

Use the tcp_serial_redirect.py script in the PySerial documentation. 使用PySerial文档中的tcp_serial_redirect.py脚本。 Is all you need. 是你所需要的全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM