我正在制作与收银机通信的程序,当我在上面测试打印机(打印从 0 到 100 的数字)时,我注意到每次我得到这个字节(十六进制的 0f)时,我的线程要么阻塞要么关闭。我的项目由2个程序组成,一个用于与收银机和tcp通信,另一个只是坐在tcp端口(稍后将添加DB)所以这是4线程程序:
#!/usr/bin/env python
import thread
import crcmod
import Queue
import serial
import socket
import time
#portovi
TCP_IP = '127.0.0.1'
TCP_PORT=5007
v =0
lockSerial = thread.allocate_lock()
lockTcp = thread.allocate_lock()
lockPrn = thread.allocate_lock()
serialBuff = []
tcpBuff = []
prnBuff = []
prnReady=1
stx = chr(0x02)
etx = chr(0x03)
ack = chr(0x06)
nack = chr(0x15)
prnCmd = ('PB','PD','PF','P'+chr(0xc2))
crc8 = crcmod.mkCrcFun(0x131, 0x00)
ser = serial.Serial( 0 , baudrate=19200, bytesize=8, parity='N', stopbits=2, timeout=None, xonxoff=0, rtscts=0, writeTimeout=None, dsrdtr=None)
ser.open()
ser.sendBreak()
sl = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sl.bind((TCP_IP, TCP_PORT))
sl.listen(1)
connl, addr = sl.accept()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT+1))
s.listen(1)
conn, addr = s.accept()
def toHex(s):
lst = []
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
hv = '0'+hv
lst.append(hv)
return reduce(lambda x,y:x+y, lst)
#hex2bit
def hex2bit(string):
novi = bin(int(string, 16))[2:]
if len(novi)<8:
pom=8-len(novi)
while pom!=0:
novi = str(0)+novi
pom=pom-1
return novi
def serialRecv():
messageSerRec = ''
global prnReady
while 1:
byte = ser.read()#shuts down here
if not toHex(byte)=='0f':#even if I change it it shuts down
if byte == ack:
print 'ACK'
elif byte == nack:
print 'NACK'
else:
if byte == stx:
messageSerRec = ''
elif byte == etx:
messageSerRecFin = messageSerRec[:-2]
chSum = int ('0x%s'%messageSerRec[-2:],0)
if chSum == crc8(messageSerRecFin):
lockSerial.acquire()
serialBuff.append(messageSerRecFin)
print "Serial Recv: ", messageSerRecFin
lockSerial.release()
ser.write(ack)
prnReady = 1
else: ser.write(nack)
else: messageSerRec = messageSerRec+byte
time.sleep(0.01)
def tcpSend():
while 1:
if serialBuff:
lockSerial.acquire()
conn.send(serialBuff[0])
print "Tcp Send: ", serialBuff[0]
serialBuff.remove(serialBuff[0])
lockSerial.release()
time.sleep(0.01)
def tcpRecv():
pom=""
while 1:
messageTcpRec = connl.recv(25)
i=0
if len(messageTcpRec)>0:
while i<len(messageTcpRec):
if not messageTcpRec[i]==';':
pom=pom+messageTcpRec[i]
else:
prnBuff.append(pom)
pom=""
i=i+1
print prnBuff
#if not messageTcpRec=="":
# print "Tcp Recv: ", messageTcpRec
# if messageTcpRec[:2] in prnCmd:
# lockPrn.acquire()
# prnBuff.append(messageTcpRec)
# lockPrn.release()
# elif tcpBuff:
# lockTcp.acquire()
# tcpBuff.append(messageTcpRec)
# lockTcp.release()
time.sleep(0.01)
def serialSend():
global prnReady
while 1:
#print "prnRdy=", prnReady
if tcpBuff:
print "tcpBuff:", tcpBuff[0]
lockTcp.acquire()
ser.write(stx+tcpBuff[0]+hex(crc8(tcpBuff[0]))[-2:].upper()+etx)
print "Serial Send: ", (tcpBuff[0])
tcpBuff.remove(tcpBuff[0])
lockTcp.release()
if prnBuff:
if prnReady == 1:
lockPrn.acquire()
ser.write(stx+prnBuff[0]+hex(crc8(prnBuff[0]))[-2:].upper()+etx)
print "Serial Send(prn): ", (prnBuff[0])
prnBuff.remove(prnBuff[0])
lockPrn.release()
prnReady = 0
time.sleep(0.01)
thread.start_new_thread(serialRecv,())
thread.start_new_thread(tcpSend,())
thread.start_new_thread(tcpRecv,())
thread.start_new_thread(serialSend,())
while 1:pass
s.close()
因此,线程 serialRecv 在收到该字节时关闭,我在过去 2-3 天一直试图解决这个问题,但找不到解决方案。