我试图用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);
}
}发布于 2016-08-10 10:48:00
我觉得你的密码是错的。
我就是这样做的。
https://www.arduino.cc/en/Serial/Write
Serial.write(val)Serial.write(str) Serial.write(buf,len)
val:作为单个字节发送的值str:作为一系列字节发送的字符串buf:作为一系列字节发送的数组len:缓冲区的长度
https://stackoverflow.com/questions/33019239
复制相似问题