我有两台服务器(用python编写),它们在Pi上处理串行通信的方式几乎相同。然而,一个工作,另一个没有,我似乎无法找到问题。我已连接逻辑分析仪,第一台服务器在串行通信时正确触发rx / tx,但第二台服务器不会触发任何东西。
第一个(工作)服务器 - 减少只显示序列:
import socket
import sys
import RPi.GPIO as GPIO
from serial import Serial
#HOST = ' ' # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privleged port
ser = 0
#accepts a command as a string, parameters separated by white space
def processData( data ):
print ( "cmd : " + data).strip()
parseData = data.split(" ")
cmdLength = len(parseData)
cmd = parseData[0]
if cmd == "digitalWritePin":
pin = parseData[1]
state = parseData[2]
#GPIO.setup(pin, GPIO.OUT) # SHOULD HAVE ALREADY BEEN DONE W/ A CONFIG!!!
if state == '1':
GPIO.output(int(pin), True)
elif state == "0":
GPIO.output(int(pin), False)
elif cmd == "serialConfig":
baudRate = int(parseData[1])
timeOut = int(parseData[2])
global ser
ser = Serial('/dev/ttyAMA0', baudRate, timeout=timeOut)
elif cmd == "serialWrite":
serialcmd = parseData[1]
writeBuff = data.split("serialWrite")
#print writeBuff[1].strip(" ")
ser.write(writeBuff[1].strip(" "))
elif cmd == "serialReadLine":
print "serial read:"
response = ser.readline()
print response
conn.sendall(response)
print "read done"
return
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
s.bind((HOST,PORT))
print 'Socket bind complete'
s.listen(10) #parameter: backlog, controls number of connections that are 'queued'
print 'Socket now listening'
#Function f or handling connections. this will be used to create threads
def clientthread(conn):
#sending message to connected client
try:
while True:
data = conn.recv(1024)
if not data:
break
processData( data )
#out of the loop
conn.close()
except socket.error , msg:
print 'Recv failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run
#second is the tuple of arguments to the function
start_new_thread(clientthread,(conn,))
s.close
最重要的部分是:
ser = Serial('/dev/ttyAMA0', baudRate, timeout=timeOut)
和el / if块的串行配置/读/写区域
和第二个服务器(不工作):
import socket
import sys
from time import sleep
from serial import Serial
from thread import *
import binascii
#HOST = ' ' # Symbolic name meaning all available interfaces
HOST = '10.1.10.28'
PORT = 8889 # Arbitrary non-privleged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
s.bind((HOST,PORT))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
s.listen(10) #parameter: backlog, controls number of connections that are 'queued'
print 'Socket now listening'
ser = Serial('/dev/ttyAMA0', baudrate = 115200, timeout= 10)
#ser.open #--- uncommenting this does not make a difference
#Function f or handling connections. this will be used to create threads
def clientthread(conn):
#infinite loop so the function does not terminate and thread does not end
try:
while True:
print "step 1"
first = conn.recv(1)
print "step 2"
if not first:
break
hextFirst = hex( ord(first) )
print hextFirst
if hextFirst == '0xff':
print "step 3"
#ser.write(hextFirst) #send 0xff (converted)
ser.write(first) #send 0xff (orignal)
length = conn.recv(1) #get length
hextLength = hex( ord(length) ) #convert length
intlength = ord(length)
print "hextLength: " + hextLength
print "step 4"
#ser.write(hextLength) #send length (converted)
ser.write(length) #send length (original)
cmd = 0
if ord(length) == 0:
cmd = conn.recv(13)
else:
cmd = conn.recv(ord(length)-2)
hextCmd = binascii.b2a_hex(cmd)
print cmd
print "hextCmd: " + hextCmd
#ser.write(hextCmd) #send cmd (converted)
ser.write(cmd) #send cmd (original)
#sleep(1)
response = ser.read(1) #get response
#hextResponse = hex(ord(response))
print "serial resp: " + response
conn.sendall(response) #send response to LV
print "step 5"
print "step 6"
sleep(10)
#out of the loop
conn.close()
except socket.error , msg:
print 'Recv failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
try:
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run
#second is the tuple of arguments to the function
start_new_thread(clientthread,(conn,))
s.close
except KeyboardInterrupt:
ser.close
s.close
print "Exiting: Keyboard Interrupt"
我意识到有很多代码可以通过,但你可以忽略大部分代码,我只是想知道串行配置/写入出了什么问题。就像我最初说的那样,问题来自逻辑分析仪没有在第二台服务器上看到任何串行通信(进出),而第一台服务器工作得很好
答案 0 :(得分:0)
尝试通过编辑/boot/cmdline.txt
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 ..
将其更改为
dwc_otg.lpm_enable=0 console=tty1 .....
并从/ etc / inittab
中删除此行 T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
还要确保以超级用户sudo