Java SerialPort 通信,接收器上的输入字节错误 [英] Java SerialPort communication, wrong input bytes on receiver

查看:34
本文介绍了Java SerialPort 通信,接收器上的输入字节错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向接收器发送字节(使用 java)时,一些接收到的字节值会混乱地更改为 0x3F.(0x0 一直变到 0x3F)

When I`m sending bytes to receiver (using java), some of received byte values chaotically changing to 0x3F. (0x0 all the time changing to 0x3F)

java 源代码:

serialPort = new SerialPort(portName);                       
  serialPort.openPort();
  serialPort.setParams(
    SerialPort.BAUDRATE_9600,
    SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE
  );
  serialPort.writeBytes(new byte[]{0,3,(byte)240,1,(byte)242});

当我使用 arduino 进行数据传输时,一切正常.

When I`m using arduino for data trasmission, everything works fine.

Arduino 源代码:

Arduino src:

byte b[5]={0,3,240,1,242};
void setup() { Serial.begin(9600);}
void loop() {
  for(int i=0;i<5;i++){
    Serial.write(b[i]);
      delay(5);
  }
  delay(1000);
}

当我从 java 向 arduino 发送数据时,所有字节看起来也很好.

When I'm sending data from java to arduino, all bytes also looks fine.

当我也用 VB 或 C# 向接收器发送数据时 - 一切都很好.

When I'm sending data to receiver with VB or C# also - everything is fine.

当我使用 Thread.sleep(5) 逐字节发送数据时 - 没有成功.

When I'm sending data byte-by-byte with Thread.sleep(5) - no success.

我做错了什么?

我正在使用 rxtx ||jssc.

I`m using rxtx || jssc.

当前的解决方法 - 通过 arduino 将数据发送到接收器.但它缓慢而奇怪.

Current workaround - send data to receiver throught arduino. But its slow and wierd.

更新:

监控端口活动后:成功数据包有几个 WAIT_ON_MASK 函数(上、下)然后在一个函数中 IRP_MJ_READ 得到所有具有正确字节的数据包.

After monitoring port activity: Success packet has couple WAIT_ON_MASK functions (up,down) Then in one function IRP_MJ_READ got all packet with proper bytes.

当我尝试用我的 java 代码发送数据时,有很多 SERIAL_GET_COMMSTATUS(向上/向下)函数,然后是 IRP_MJ_READ,前 3 个字节转换为 0x3F,然后再次 WAIT_ON_MASK &COMMSTATUS,然后逐字节保留数据.

When I`m trying to send data with my java code, there is a lot SERIAL_GET_COMMSTATUS (up/down) functions, then IRP_MJ_READ with first 3 bytes converted to 0x3F, then again WAIT_ON_MASK & COMMSTATUS, and then remaing data byte-by-byte.

附言接收器是一辆旧车 bc.

P.s. Receiver is an old car bc.

推荐答案

在我的情况下SerialPort.PARITY_EVEN(使用jssc)解决问题

In my case SerialPort.PARITY_EVEN (using jssc) solve the problem

这篇关于Java SerialPort 通信,接收器上的输入字节错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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