2013-03-07 63 views
1

我有一個應用程序通過串行端口(使用pyserial)將數據發送到接收時回覆的外部模塊。我有一個線程監視傳入的數據,當有數據時,通過發送函數發送一個信號。在插槽中,我然後分析收到的數據包與簡化的hdlc協議。它工作正常,但唯一的問題是,如果幀包含零(0x00),該插槽接收到的字符串會被截斷。所以我假設emit函數將字符串傳遞給'0'。這是信號和插槽的代碼。從PySide插槽接收到的字符串不完整

def ComPortThread(self): 
    """Thread that handles the incoming traffic. Does the basic input 
     transformation (newlines) and generates an event""" 
    while self.alive.isSet():    #loop while alive event is true 
     text = self.serial.read(1)   #read one, with timeout 
     if text:       #check if not timeout 
      n = self.serial.inWaiting()  #look if there is more to read 
      if n: 
       text = text + self.serial.read(n) #get it 
      self.incomingData.event.emit(text) 

@QtCore.Slot(str) 
def processIncoming(self, dataIn): 
    """Handle input from the serial port.""" 
    for byte in dataIn: 
     self.hexData.append(int(binascii.hexlify(byte),16)) 
    .... 

例如,如果我打印ComPortThread變量「文本」的內容,我可以得到:

7e000a0300030005

,如果我做同樣的「數據輸入」 ,我得到:

7E

我讀過QByteArray會保持'0',但我沒有成功使用它(雖然我不知道我是否正確使用它)。

回答

0

嗯,沒事找過QtSlot decorator形式:

PyQt4.QtCore.pyqtSlot(types[, name][, result]) 
Decorate a Python method to create a Qt slot. 

Parameters: 
types – the types that define the C++ signature of the slot. Each type may be a Python type object or a string that is the name of a C++ type. 
name – the name of the slot that will be seen by C++. If omitted the name of the Python method being decorated will be used. This may only be given as a keyword argument. 
result – the type of the result and may be a Python type object or a string that specifies a C++ type. This may only be given as a keyword argument. 

而且它看起來像自pyserial的讀返回bytes python type

read(size=1)¶ 
Parameters: 
size – Number of bytes to read. 
Returns:  
Bytes read from the port. 
Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read. 

Changed in version 2.5: Returns an instance of bytes when available (Python 2.6 and newer) and str otherwise. 

雖然2.5版和Python 2.6的注意。考慮到這一點我想看看確保你最多提到的,並嘗試兩個版本:

@QtCore.Slot(bytes) 
def processIncoming(self, dataIn): 
    """Handle input from the serial port.""" 
    for byte in dataIn: 
     self.hexData.append(int(binascii.hexlify(byte),16)) 
    .... 

,看看是否適合您。

+0

感謝您的建議,但我得到了同樣的結果。現在,在調用emit()函數之前,我通過使用「.encode(」hex「)」應用了一個大胖補丁。然後我在插槽內解碼。這對我來說可行,但感覺就像用錘子插入螺絲。 – jfmorin 2013-03-08 13:14:14

+0

@jfmorin Python 2.6或更高版本的2.5 pyserial或更高版本正確嗎? – cwgem 2013-03-08 13:15:16

+0

是的,我在python 2.6上使用Python 2.7。 – jfmorin 2013-03-08 13:50:43