我目前正在使用arduino nano为ws2812b LED(argb)构建一个控制器。我想用一个通过usb连接到arduino的windows计算机上的java应用程序来控制颜色。我编写了以下java代码来向arduino发送基本数据:
public class Main {
SerialPort sp;
public Main() {
openConnection("COM3");
byte[] data =
{0b00000001, 0b00000000, 0b01000000, 0b00000000, 0b01000000};
try {
sp.getOutputStream().write(data);
sp.getOutputStream().flush();
Thread.sleep(2000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
closeConnection();
} // End of constructor
public static void main(String[] args) {
new Main();
} // End of main( String [] args )
@Override
public void actionPerformed(ActionEvent e) {
} // End of actionPerformed( ActionEvent e )
private void openConnection(String port) {
if (port == null)
throw new IllegalArgumentException("Port can't be null.");
sp = SerialPort.getCommPort(port);
sp.setComPortParameters(9600, 8, 1, 0);
sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);
if (sp.openPort())
System.out.println("Port open.");
else
System.out.println("Port could not be opened.");
} // End of openConnection( String port )
private void closeConnection() {
if (sp.closePort())
System.out.println("Port closed.");
else
System.out.println("Port could not be closed.");
} // End of closeConnection()
} // End of class
如果我运行这个,数据会被发送到arduino(rx闪烁,奇怪的是l也闪烁),但是serial.available()总是返回0。以下是arduino代码的相关部分:
void setup() {
for (int i = 0; i < 8; i++) {
ARGB[i].begin();
}
showAll();
Serial.begin(9600);
}
//ARGB_X.setPixelColor(NUMBER_OF_LED, RED, GREEN, BLUE);
void loop() {
if (Serial.available() > 0) {
ARGB[0].setPixelColor(0, 10, 0, 0);
ARGB[0].show();
}
}
有人知道我做错了什么吗?如有任何建议,我们将不胜感激。
暂无答案!
目前还没有任何答案,快来回答吧!