Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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
OutputStream未在Java中写入_Java_Vb.net_Arduino_Serial Port - Fatal编程技术网

OutputStream未在Java中写入

OutputStream未在Java中写入,java,vb.net,arduino,serial-port,Java,Vb.net,Arduino,Serial Port,我试图将字符串值写入我的arduino端口,但它成功写入,但当我发送命令时,机器不工作。如果我用我的arduino软件来测试这台机器,它工作得很好。我能知道为什么我不能在我的arduino端口上写东西吗 这是我的java代码: public static void comArduino(){ portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) {

我试图将字符串值写入我的arduino端口,但它成功写入,但当我发送命令时,机器不工作。如果我用我的arduino软件来测试这台机器,它工作得很好。我能知道为什么我不能在我的arduino端口上写东西吗

这是我的java代码:

public static void comArduino(){
    portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (portId.getName().equals("COM6")) {
                try {
                    serialPort = (SerialPort)
                    portId.open("Arduino", 2000);
                } catch (PortInUseException e) {
                    System.out.println("Error1 is "+ e);
                }
                try {
                    outputStream = serialPort.getOutputStream();
                } catch (IOException e) {
                    System.out.println("Error2 is "+ e);
                }
                try {
                    serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                } catch (UnsupportedCommOperationException e) {
                    System.out.println("Error3 is "+ e);
                }

                try{
                    Integer number = 1;
                    outputStream.write(number);
                    outputStream.flush();
                    System.out.println(number);

                }catch(IOException e){
                    System.out.println("Error 3: " + e);
                } 
           }

        }
    }
    serialPort.close();
}
这是我的vb代码:

 Private Sub Send_Arduinocmd()
    Dim data1 As String = "1"
    ComArduino.Write(data1)
    MsgBox("sent " + data1 + " to arduino")
End Sub

我想用java做同样的事情。谁能帮我一下我犯了什么错误。谢谢。

您是刷新还是关闭您的
输出流?你在Java中有错误吗?没有,我没有刷新我的OutputStream在使用flush之后,它仍然是一样的,arduinoSorry没有响应。我对Arduino的事一无所知。帮助你会很难。我编辑了你的帖子,添加了
arduino
标签。希望有更了解这一点的人会看到它。我写的方法和vb中的相同吗?这是因为在vb中工作得很好,而在java中它不工作,您是否刷新或关闭
输出流
?你在Java中有错误吗?没有,我没有刷新我的OutputStream在使用flush之后,它仍然是一样的,arduinoSorry没有响应。我对Arduino的事一无所知。帮助你会很难。我编辑了你的帖子,添加了
arduino
标签。希望有更了解这一点的人会看到它。我写的方法和vb中的相同吗?这是因为在vb中工作很好,而在java中则不工作