扭曲的SerialPort dataReceived()提供的数据支离破碎 [英] Twisted serialport dataReceived() provides fragmented data

查看:340
本文介绍了扭曲的SerialPort dataReceived()提供的数据支离破碎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我tyring实现一个python程序,使用双绞线,沟通难熬的蓝牙设备。下面是我实现了一个样本code:

I am tyring to implement a python program, using Twisted, to communicate witha bluetooth device. The following is a sample code of what I have implemented:

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

当我运行程序,有时我得到回应和其他时间我没有收到任何东西。和大部分的时间长的响应支离破碎的表现为接下来的消息的一部分。我必须通过超级终端,以确保我得到通过蓝牙设备适当的响应。因此,这个问题必须与我的code。

When I run the program, sometimes I get a response and other times I do not receive anything. And most of the times long responses are fragmented appear as part of the next message. I have through the hyperterminal to make sure that I get the appropriate response from by bluetooth device. So, the problem has to be with my code.

有什么,我在我的code做错了吗?

Is there something that I doing wrong in my code?

其他修改/修正

当我更换dataReceived()函数由stringReceived()以上code,程序永远不会进入此功能。

When I replace dataReceived() function in the above code by stringReceived(), the program never enters this function.

我也试过上述程序与LineReceiver协议,如下:

I also tried to above program with the LineReceiver protocol, as the following:

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功能分散的数据。

I have the same problem, as before, with fragmented data from the dataReceived function.

推荐答案

有关我的大部分蓝牙的工作中,我使用的8位整数,所以我会建议使用 Int8StringReceiver 。在 LineReceiver 协议等待'\\ r \\ n'(和蓝牙无线电我用它默认为底线序列返回'\\ r')等等底线序列的错配将prevent从不断进入code。

For most of my bluetooth work, I've used 8-bit integers, so I would recommend using Int8StringReceiver. The LineReceiver protocol waits for an endline sequence which defaults to '\r\n' (and the bluetooth radios I use return '\r') so a mismatch on endline sequence would prevent the code from ever entering.

您是否尝试过使用非扭曲库调试?我强烈建议特别是扭曲的生产环境,但 PySerial 是轮询串行数据的好方法。 (的easy_install pyserial 应该做的伎俩。)试试这个code:

Have you tried to use a non-Twisted library for debugging? I highly recommend Twisted especially for production environments, but PySerial is a great way to poll serial data. (easy_install pyserial should do the trick.) Try this code:

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

请务必使用超时= 0 ,因为这将使你的无阻塞。
这将允许你检查什么样的数据是由蓝牙无线输出。

Make sure to use timeout=0 since that will make your read non-blocking. This will allow you to examine exactly what kind of data is being outputted by the bluetooth radio.

最后,这取决于你使用的是什么样的蓝牙无线电,Windows可能会决定移动 COM20 各地,特别是如果你使用的是USB连接的收音机。

Finally, depending on what kind of bluetooth radio you are using, Windows might decide to move COM20 around, especially if you are using a USB-connected radio.

这篇关于扭曲的SerialPort dataReceived()提供的数据支离破碎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆