Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用串行端口将整数从Ardunio发送到Java程序,并在Java中将两字节数组转换为整数_Java_Arduino - Fatal编程技术网

使用串行端口将整数从Ardunio发送到Java程序,并在Java中将两字节数组转换为整数

使用串行端口将整数从Ardunio发送到Java程序,并在Java中将两字节数组转换为整数,java,arduino,Java,Arduino,我想将整数值从Arduino发送到Java应用程序。为此,我使用串行端口。在Arduino的处理程序中,我将以下内容打印到串行端口: Serial.print(accel, DEC) accel是一个有符号的int。在我的Java代码中,我实现了一个SerialPortEventListener,并从inputstream输入读取一个字节数组 public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEven

我想将整数值从Arduino发送到Java应用程序。为此,我使用串行端口。在Arduino的处理程序中,我将以下内容打印到串行端口:

Serial.print(accel, DEC)
accel
是一个有符号的int。在我的Java代码中,我实现了一个
SerialPortEventListener
,并从inputstream输入读取一个字节数组

public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            int available = input.available();
            byte[] chunk = new byte[available];
            input.read(chunk);
            System.out.println (new String(chunk));
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}
我需要将这个字节数组转换成整数,但我不知道如何做。Arduino教程说Arduino以两个字节(16位)存储签名整数。我尝试将两个字节的数据读入数组块,然后用下面的代码将这两个字节解码成整数

public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            int available = input.available();
            int n = 2;
            byte[] chunk = new byte[n];
            input.read(chunk);
            int value = 0;
            value |= chunk[0] & 0xFF;
            value <<= 8;
            value |= chunk[1] & 0xFF;
            System.out.println(value);
         } catch (Exception e) {
             System.err.println(e.toString());
         }
    } 
}

如何将来自Arduino的字节通过串行端口解码为整数?

您可以使用
ByteBuffer
将字节转换为int,代码如下所示:

ByteBuffer bb = ByteBuffer.wrap(chunk);
System.out.println(bb.getInt());
union{
  int i;
  byte b[2];
}u;
u.i = accel;
Serial.write(u.b, 2);
int n = 2;
byte[] chunk = new byte[n];
input.read(chunk);
short value = 0;
// get 2 bytes, unsigned 0..255
int low = chunk[0] & 0xff;
int high = chunk[1] & 0xff;
value =  (short) (high << 8 | low) ;
System.out.println(value);

编辑:我应该已经识别出ascii数字:-)

为什么不:制定一个面向行的协议;使用java BufferedReader和.readLine()。 使用Integer.valueOf()解析字符串。请注意,换行符用作 框架字符

它描述了大多数GPS设备和各种非常重要的设备是如何使用的。
无论如何都不是最理想的,但你可以这样做…

结果是Arduino上的Serial.print()函数将值转换为字符串,并将字符串发送到串行端口,这就是我的位移位不起作用的原因。一种解决方案是将java代码中的值收集为字符串,然后将字符串转换为int

但是,为了避免将值从字符串转换为int,可以使用serial.write()函数将int发送到串行端口。为此,在Arduino草图中,定义一个int和一个大小相同的字节数组的并集。然后创建一个union实例,设置整数,并使用Serial.write()指定匹配的字节数组。在Java程序中收集字节数组并进行位移位以获得整数

我的Arduino草图编码如下:

ByteBuffer bb = ByteBuffer.wrap(chunk);
System.out.println(bb.getInt());
union{
  int i;
  byte b[2];
}u;
u.i = accel;
Serial.write(u.b, 2);
int n = 2;
byte[] chunk = new byte[n];
input.read(chunk);
short value = 0;
// get 2 bytes, unsigned 0..255
int low = chunk[0] & 0xff;
int high = chunk[1] & 0xff;
value =  (short) (high << 8 | low) ;
System.out.println(value);
我用于读取字节数组并将其转换为int的java代码如下所示:

ByteBuffer bb = ByteBuffer.wrap(chunk);
System.out.println(bb.getInt());
union{
  int i;
  byte b[2];
}u;
u.i = accel;
Serial.write(u.b, 2);
int n = 2;
byte[] chunk = new byte[n];
input.read(chunk);
short value = 0;
// get 2 bytes, unsigned 0..255
int low = chunk[0] & 0xff;
int high = chunk[1] & 0xff;
value =  (short) (high << 8 | low) ;
System.out.println(value);
int n=2;
byte[]chunk=新字节[n];
输入读取(块);
短值=0;
//获取2个字节,无符号0..255
int low=块[0]&0xff;
int high=块[1]&0xff;

value=(short)(high我的问题与此类似。我使用Arduino Serial.write发送一组已知的int值(2字节/int)。Arduino Serial monitor正确解释了这些值。但是java程序将所有数字都搞错了(零除外)。显然字节顺序是相反的。我认为字节顺序也是arduino输出的其他数据类型的问题。jSerialComm用于读取串行端口。以下是java代码:

// declarations
byte[] byter = new byte[16];
ByteBuffer bufr = ByteBuffer.wrap(byter);
short[] shortr = new short[8];

// code
comPort.readBytes(byter, 16);
for(int i=0; i<8; i++){
    shortr[i] = Short.reverseBytes(bufr.getShort(2*i));
}
//声明
byte[]byter=新字节[16];
ByteBuffer bufr=ByteBuffer.wrap(byter);
short[]shortr=新的short[8];
//代码
comPort.readBytes(byter,16);

对于(int i=0;iHi mprabhat,感谢您的帮助。当尝试使用ByteBuffer时,我得到了一个java.nio.BufferUnderflowException。为什么会发生这种情况?我读到了有关ByteBuffer的文章。getInt返回BufferUnderflowException的原因是getInt在给定索引处读取四个字节,但块只包含两个字节。因此我尝试使用Byt使用getShort进行eBuffer并再次获得错误的值。这些值不是在-512到-516之间,而是在:3338 11573 12594 3338 11573 12595等处。嘿,很抱歉刚才看到了您的评论。我打印了块[0]和块[1]得到了这些重复的值:45534954131010455349521310我不知道这是否也有帮助,但下面是代码'code'int n=2;byte[]chunk=newbyte[n];input.read(chunk);System.out.println(chunk[0]+,“+chunk[1])“code”,这是打印结果:45、53、49、53每次更改
value
后,请尝试打印出它的值。Ha:-)现在看起来很愚蠢的人,我应该注意到十六进制转储实际上是ascii数字。是的,为什么不呢。制作一个面向行的协议,使用标记-值对:比如TEMP:191\n手动调试至少很简单…我实际上找到了一种将int写入串行端口的方法。我将发布我的解决方案。