1

Bluetoothデバイスと通信するために、Twistedを使用してpythonプログラムを実装しようとしています。以下は、私が実装したサンプルコードです。

from twisted.internet import protocol, reactor
from twisted.internet.serialport import SerialPort
from twisted.protocols import basic

class DeviceBluetooth(basic.Int16StringReceiver):

    def connectionMade(self):
        print 'Connection made!'
        self.sendString('[01] help\n')

    def dataReceived(self, data):
        print"Response: {0}".format(data)

        print "-----"
        print "choose message to send: "
        print "1. Stim on"
        print "2. Stim off"
        print "3. Stim status"
        print "4. Help"
        # user input
        ch = input("Choose command :: ")
        if int(ch) == 1:
            self.sendString('[02] stim on\n')
        elif int(ch) == 2:
            self.sendString('[03] stim off\n')
        elif int(ch) == 3:
            self.sendString('[04] stim ?\n')
        elif int(ch) == 4:
            self.sendString('[05] help\n')
        else:
            reactor.stop()

SerialPort(DeviceBluetooth(), 'COM20', reactor, baudrate=115200)
reactor.run()

プログラムを実行すると、応答が返されることもあれば、何も受信されないこともあります。ほとんどの場合、長い応答は断片化されており、次のメッセージの一部として表示されます。ハイパーターミナルを介して、Bluetooth デバイスから適切な応答が得られることを確認します。したがって、問題は私のコードにあるはずです。

私のコードで間違っていることがありますか?


追加修正・訂正

上記のコードの dataReceived() 関数を stringReceived() に置き換えると、プログラムはこの関数に入りません。

また、次のように、LineReceiver プロトコルを使用して上記のプログラムを作成しようとしました。

from twisted.internet import protocol, reactor
from twisted.internet.serialport import SerialPort
from twisted.protocols import basic

class DeviceBluetooth(basic.LineReceiver):

    def connectionMade(self):
        print 'Connection made!'
        self.sendLine('[01] help')

    def dataReceived(self, data):
        print"Response: {0}".format(data)

        print "-----"
        print "choose message to send: "
        print "1. Stim on"
        print "2. Stim off"
        print "3. Stim status"
        print "4. Help"
        # user input
        ch = input("Choose command :: ")
        if int(ch) == 1:
            self.sendLine('[02] stim on')
        elif int(ch) == 2:
            self.sendLine('[03] stim off')
        elif int(ch) == 3:
            self.sendLine('[04] stim ?')
        elif int(ch) == 4:
            self.sendLine('[05] help')
        else:
            reactor.stop()

SerialPort(DeviceBluetooth(), 'COM20', reactor, baudrate=115200)
reactor.run()

dataReceived 関数からの断片化されたデータで、以前と同じ問題があります。

4

2 に答える 2

1

Int16StringReceiver2 バイト (16 ビット) の長さのプレフィックスを使用してメッセージ フレーミングを実装するプロトコル サブクラス。dataReceivedただし、そのフレーミングを実装するメソッドはオーバーライドされます。これによりフレーミングが無効になり、たまたま接続から読み取られたバイトが、たまたま読み取られたサイズで配信されます。

をサブクラス化するときは、代わりInt16StringReceiverにオーバーライドすることを意図しています。stringReceived

于 2013-08-29T15:59:17.067 に答える
1

私の Bluetooth 作業のほとんどで、8 ビット整数を使用してきたので、Int8StringReceiver. プロトコルはLineReceiver、デフォルトのエンドライン シーケンス'\r\n'(および私が使用する Bluetooth ラジオは return '\r') を待機するため、エンドライン シーケンスの不一致により、コードが入力されなくなります。

デバッグに Twisted 以外のライブラリを使用しようとしましたか? 特に本番環境では Twisted を強くお勧めしますが、PySerialはシリアル データをポーリングする優れた方法です。(easy_install pyserialトリックを行う必要があります。)次のコードを試してください:

import serial
s = serial.Serial('COM20', 115200, timeout=0)
data = s.read(1024)
print repr(data)

これにより、ノンブロッキングtimeout=0が行われるため、必ず使用してください。readこれにより、Bluetooth 無線によって出力されているデータの種類を正確に調べることができます。

最後に、使用している bluetooth 無線の種類によってはCOM20、特に USB 接続の無線を使用している場合、Windows が移動することを決定する場合があります。

于 2013-08-29T17:02:11.173 に答える