Java 在Arduino中同时读取和写入串行数据
首先,我是Arduino的新手。我已经创建了一个Java应用程序,它通过串行通信成功地与Arduino通信。但我想做的是从陀螺仪传感器读取数据,用java监控这些数据,然后从java发送命令来控制马达。有没有一种方法可以在不发生数据冲突的情况下同时读取数据和发送命令? 我使用Arduino mega 2560板Java 在Arduino中同时读取和写入串行数据,java,arduino,serial-port,serial-communication,Java,Arduino,Serial Port,Serial Communication,首先,我是Arduino的新手。我已经创建了一个Java应用程序,它通过串行通信成功地与Arduino通信。但我想做的是从陀螺仪传感器读取数据,用java监控这些数据,然后从java发送命令来控制马达。有没有一种方法可以在不发生数据冲突的情况下同时读取数据和发送命令? 我使用Arduino mega 2560板 void loop( ){ while(Serial.available( ){ char command = Serial.read(); sw
void loop( ){
while(Serial.available( ){
char command = Serial.read();
switch(command){
case 'w':
// run motors
// lots of other cases
default:
//when there is no command received and the last data is send at leat 1s ago, send data
//I afraid data collide when there is incoming command during sending data
}
}
}
抱歉,我的英语不好。因为有两根电线将Arduino连接到电脑,所以不可能发生“碰撞”。当电脑发送到Arduino时,它使用Arduino RX线。当Arduino发送到PC时,它使用Arduino传输线。这就是交流。您只需使用两种模式即可。一个用于发送/接收命令,另一个用于发送/接收数据。当您有来自陀螺的数据要发送时,若串行可用,则发送数据并等待命令。在Java中,反向。