擅长:python、mysql、java
<p>我找不到任何解决睡眠问题的方法。只有在至少允许1.5秒的情况下,arduino才会正确地接收到字节-大概是为了串行连接初始化。在</p>
<p>作为一种解决方法,我必须在启动时打开串行端口,然后通过套接字服务器接受和写入数据。这一招如期而至。问题似乎出在串行端口的打开上。在</p>
<pre><code>import os, os.path
import time
import serial
import socket
import re
def sendData(msg):
if not ser.is_open:
ser.open()
ser.write(msg)
ser.flush()
out = ''
time.sleep(0.5)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != '':
print ">>" + out
else:
print ">> nothing!"
time.sleep(0.5)
port='/dev/ttyUSB1',
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 9000))
serversocket.listen(1) # become a server socket, maximum 5 connections
ser = serial.Serial(
port=port,
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
)
while True:
connection, address = serversocket.accept()
buf = connection.recv(64)
if len(buf) > 0:
print buf
sendData(buf)
</code></pre>