1

我正在尝试使用 Java 简单串行连接器在我的计算机和 arduino uno 之间建立连接。我试图使用下面列出的代码来做到这一点。不知何故它不工作(连接到arduino引脚7的LED二极管在运行我的程序时没有打开,但是当我使用artuino软件的串行监视器时它会打开。)。有谁知道为什么?

Java项目代码:

    import jssc.SerialPort;
import jssc.SerialPortException;

public class Main {

    public static void main(String[] args) {
        //In the constructor pass the name of the port with which we work
        SerialPort serialPort = new SerialPort("COM3");
        try {
            //Open port
            serialPort.openPort();
            //We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
            serialPort.setParams(SerialPort.BAUDRATE_9600,
                                 SerialPort.DATABITS_8,
                                 SerialPort.STOPBITS_1,
                                 SerialPort.PARITY_NONE);
            //Writes data to port
            serialPort.writeBytes("Test".getBytes());
            //Closing the port
            serialPort.closePort();
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }
}`

Arduino代码:

void setup() {
  Serial.begin(9600); //Ustawienie prędkości transmisji
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
}

void loop() {
  if( Serial.available() > 0){
    digitalWrite(7, HIGH);

  }

}
4

1 回答 1

0

我认为,您的 Arduinocode 是错误的。

我这样做。

https://www.arduino.cc/en/Serial/Write

Serial.write(val)

Serial.write(str) Serial.write(buf, len)

val:作为单个字节发送的值 str:作为一系列字节发送的字符串 buf:作为一系列字节发送的数组 len:缓冲区的长度

于 2016-08-10T10:48:00.130 回答