Raspberry Pi:使用Pi4J从UART读取数据

问题描述 投票:0回答:1

[遵循示例“使用Pi4J的串行通信示例”时,我无法从Raspberry Pi的UART(/ dev / ttyAMA0)读取数据]

http://pi4j.com/example/serial.html

    // create an instance of the serial communications class
    final Serial serial = SerialFactory.createInstance();

    SerialConfig config = new SerialConfig();

    // set default serial settings (device, baud rate, flow control, etc)
    //
    // by default, use the DEFAULT com port on the Raspberry Pi (exposed on GPIO header)
    // NOTE: this utility method will determine the default serial port for the
    //       detected platform and board/model.  For all Raspberry Pi models
    //       except the 3B, it will return "/dev/ttyAMA0".  For Raspberry Pi
    //       model 3B may return "/dev/ttyS0" or "/dev/ttyAMA0" depending on
    //       environment configuration.
    config.device(SerialPort.getDefaultPort());
    config.baud(Baud._38400);
    config.dataBits(DataBits._8);
    config.parity(Parity.NONE);
    config.stopBits(StopBits._1);
    config.flowControl(FlowControl.NONE);

    // open the default serial device/port with the configuration settings
    serial.open(config);

回调方法是:

    // create and register the serial data listener
    serial.addListener(new SerialDataEventListener()
    {
        @Override
        public void dataReceived(SerialDataEvent event)
        {
            byte[] receivedBytes = event.getBytes();
            ....

问题是:我确定每2秒就会有192字节“由我的其他硬件”发送到UART。但是我的回调方法不会收到192字节,但有时会收到16、97、141,...等。

添加中

event.discardData();

读取事件后至少使我无法接收0字节...

这种情况发生在我在GWT应用程序的服务器端读取UART时。

当使用launchpi在Raspberry Pi上直接调试时>

http://tsvetan-stoyanov.github.io/launchpi/

然后,我还接收到一些长度小于192字节的数据块,但是在经过10 ... 20秒的“调整时间”后,我主要是

一次收到了192字节的数据,这是我所期望的(有时 192个字节分为144个字节和48个字节,但是我可以处理)。

我用于在launchpi和GWT服务器端进行调试的代码当然是相同的。对于我的发送方和作为接收方的Raspi,波特率是相同的。

使用GWT时有什么区别,或者我是否通过插入其他开始/停止字节以某种方式同步了UART?

我需要在发送时在一个块中可靠的192字节。想法和提示(当然还有解决方案:-)非常受欢迎...最好的问候!

当遵循示例“使用Pi4J的串行通信示例” http://pi4j.com/example/serial.html //创建一个...时,我无法从Raspberry Pi的UART(/ dev / ttyAMA0)读取数据。

java gwt raspberry-pi2 uart
1个回答
0
投票

已解决(请参见上文)。只是要关闭的评论。

© www.soinside.com 2019 - 2024. All rights reserved.