简体   繁体   English

Java将字节写入串行端口USB

[英]Java writing bytes to serial port USB

I have a code below that I am sending to a serial USB port... it was working, now its not! 我下面有一个代码,我正在发送到串行USB端口...它正在工作,但现在不行! the akber() function dies if the wrong string is sent... 如果发送了错误的字符串, akber()函数就会死掉...

if I send akber("1.0.0.0.1.5") - it works perfectly, 如果我发送akber("1.0.0.0.1.5") -效果很好,

if I send akber("23.0.128.0.0.5") - it does not work... 如果我发送akber("23.0.128.0.0.5") -它不起作用...

See code below 见下面的代码

    public static byte[] akber(final String input) {
        StringTokenizer tokens = new StringTokenizer(input, ".");
        int numberOfArrays = tokens.countTokens();
        byte[][] byteArrays;
        byteArrays = new byte[numberOfArrays][4];
        int i = 0;
        int space = 0;
        while (tokens.hasMoreTokens()) {
            int x = Integer.valueOf(tokens.nextToken());
            if (x<256) { space++; } else { space+=2; }  
            byteArrays[i] = BigInteger.valueOf(x).toByteArray();
            i++;
        }
        final byte[] output = new byte[space];
        copySmallArraysToBigArray(byteArrays, output);
        return output;
    }

    public static void copySmallArraysToBigArray(final byte[][] smallArrays, final byte[] bigArray) {
        int currentOffset = 0;
        for (final byte[] currentArray : smallArrays) {
            System.arraycopy(currentArray, 0, bigArray, currentOffset, currentArray.length);
            currentOffset += currentArray.length;
        }
}

called from function: 从函数调用:

serialPort.writeBytes(akber(data));

I would need it to work with any combination of numbers in the "data" string, so it converts them to the right type of bytes and writes to port... its not my code, and I don't quite understand it, but still need to fix it :-) 我需要它与“数据”字符串中的任何数字组合一起工作,因此它将其转换为正确的字节类型并写入端口...这不是我的代码,我不太理解,但是仍然需要修复它:-)

Change this line: 更改此行:

if (x<256) { space++; } else { space+=2; }  

to

if (x<128) { space++; } else { space+=2; }  

I ran your code, originally it throws an IndexOutOfBoundsException for 我运行了您的代码,最初它会引发IndexOutOfBoundsException

akber("1.0.128.0.0.5");

so check your code that it is not consuming exceptions somewhere, eg 因此,请检查您的代码是否在某处不消耗异常,例如

try {
     exceptionThrowingMethod();
}      
catch(Exception e) {
}

If the exceptionThrowingMethod throws an exception the code will continue as if the exception was not thrown (but the exceptionThrowingMethod didn't execute succesfully!) 如果exceptionThrowingMethod引发了异常,则代码将继续运行,就像未引发exceptionThrowingMethod一样(但是exceptionThrowingMethod没有成功执行!)

Actually although the above allowed the code to continue working, it did not solve the problem as byte values from the function above 128 were the negative equivalent or something, and sent the wrong values, so the USB hardware was receiving the incorrect characters and not working... by looking at other posts about "Java 128 bytes" on stackoverflow, worked out its something to with byte code above 128 being the same as its negative equivalent, so solved it with trial and error, very annoying - but changed that line to: 实际上,尽管上面的代码允许代码继续工作,但是它不能解决问题,因为来自函数128的字节值等于负数或类似的值,并且发送了错误的值,因此USB硬件接收到错误的字符且无法正常工作...通过查看关于stackoverflow上的“ Java 128字节”的其他文章,得出了与128字节以上的字节代码相同的结果,即负数相同,因此通过反复试验解决了它,非常烦人-但更改了这一行至:

    if (x<128) { space++; } else { space+=2;
                int x2 = (x-128)*2;
                x=x-(x*2);
                if (x<-128) { x=x+x2; }
                }

which seemed to work. 这似乎起作用。 So happy days, till I find another issue with it! 开心的日子,直到发现另一个问题! Might be a simple solution for people looking to convert values above 127 to bytes, than more standard Java solution I saw and didnt really understand, as am more used to scripting. 对于希望将127以上的值转换为字节的人们来说,这可能是一个简单的解决方案,而不是我所看到的,并没有真正理解的更标准的Java解决方案,因为它更习惯于编写脚本。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM