1

受信時に応答する外部モジュールにシリアルポート経由で (pyserial を使用して) データを送信するアプリケーションがあります。着信データを監視するスレッドがあり、存在する場合は、発行関数を介して信号を送信します。スロットでは、簡略化された hdlc プロトコルに対して受信したパケットを分析します。正常に動作していますが、唯一の問題は、フレームにゼロ (0x00) が含まれている場合、スロットによって受信された文字列が切り捨てられることです。したがって、出力関数は文字列を「0」まで渡すと想定しています。シグナルとスロットのコードは次のとおりです。

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))
    ....

たとえば、変数 "text" の内容を ComPortThread に出力すると、次のようになります。

7e000a0300030005

「dataIn」についても同じことを行うと、次のようになります。

7e

QByteArray が「0」を保持することを読みましたが、それを使用することに成功していません (ただし、正しく使用したかどうかはわかりません)。

4

1 に答える 1

0

うーん、QtSlot デコレータを見ると、フォームは次のようになります。

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 の read はバイトの python タイプを返すため、次のようになります。

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))
    ....

そして、それがうまくいくかどうかを確認してください。

于 2013-03-07T21:18:54.863 に答える