使用java将ASCII写入串行端口

使用java将ASCII写入串行端口,java,serialization,Java,Serialization,我正在尝试将ASCII代码1写入串行端口。但是现在,我面临着将其写入串行端口的问题。我可以知道如何将ASCII写入串行端口吗 这是我的java代码: String number = "1"; byte arduinoo[] = number.getBytes(); for(int i=0; i<arduinoo.length; i++){ outputStream.write(arduinoo[i]); System.out.println(arduinoo[i]

我正在尝试将ASCII代码1写入串行端口。但是现在,我面临着将其写入串行端口的问题。我可以知道如何将ASCII写入串行端口吗

这是我的java代码:

 String number = "1";
 byte arduinoo[] = number.getBytes();
 for(int i=0; i<arduinoo.length; i++){
     outputStream.write(arduinoo[i]);
     System.out.println(arduinoo[i]);
     outputStream.flush();
 }
String number=“1”;
字节arduinoo[]=number.getBytes();

对于(int i=0;i我认为您正在尝试向arduinoo发送一个字节值0x1

但是简单地尝试发送字符串并不容易…因为如果从字符串中获取字节,则(通常)会获取存储在字节[]中的每个字符的“ASCII码”

    String strA = "1";
    byte[] dataA = strA.getBytes();

    for (int i = 0; i < dataA.length; i ++){
        int asInt = dataA[i];
        System.out.println("it's not what was expected..."+asInt);
    }

    String strB = "1";
    byte[] dataB = new byte[]{(byte)Integer.parseInt(strB)}; //parsing the String literal into an Integer

    for (int i = 0; i < dataB.length; i ++){
        int asInt = dataB[i];
        System.out.println("this is what was expected..."+asInt);
    }
因此,无论何时您想要发送字节explixit,然后直接创建它们

byte[] data = new byte[]{1,2,3};

你说你有问题,但从未解释过问题所在。你是否遇到异常?输出错误?你需要为你的问题添加更多信息。我正在向串行端口写入数据,结果显示成功,但我的串行端口没有读取我的信号。实际上没有问题,只是我觉得我正在编写的方法可能是错误的.
byte[] data = new byte[]{1,2,3};