Nag*_*gyI 5 java serial-port arduino jssc
我正在测试我的Arduino UNO的串行传输速度.根据我的要求,我必须从主机PC向Arduino传输3KB/s.我编写了一个非常简单的程序,它回复了结果,Serial.available()然后在Arduino IDE的串行监视器中进行了测试.我已经开始发送字符,直到达到63字节的最大值.我对此感到非常惊讶,因为我在某处看到Arduino有一个128byte的串行缓冲区.
无论如何,我已经编写了一个非常简单的协议,它以48字节的数据包传输数据(实际上是49个字节,因为标题字符).主机发送一个d字符,然后发送48个字节的数据.为了测试传输的有效性,我发送一个从0到47的简单字节序列,在Arduino端进行验证.如果验证失败,UNO开始闪烁PIN13上的板载LED.一旦发送了字节,主机就会等待一个简单k字符的确认.一旦完成处理实际数据包,Arduino就会发送它.
主机程序测量传输的数据包的数量,并在1秒后显示它.波特率为9600时,PC成功传输~16包/秒(~800字节/秒),这是相当不错的.我试图通过将双方的波特率提高到57600来改善这一点; 但是,发送的数据包数量只会增加一点.我不知道问题是什么.也许我已经达到USB串口转换器的某种限制?
这是我的代码.
PC(Java,我使用jSSC进行串口通信)
package hu.inagy.tapduino.server;
import jssc.SerialPort;
import jssc.SerialPortException;
/**
* Test Arduino communication.
*/
public class App
{
private static void testComm(SerialPort port) throws SerialPortException {
long runningSeconds = 0;
long time = System.currentTimeMillis();
long numberOfPackets = 0;
boolean packetSent = false;
while (runningSeconds < 10) {
long currentTime = System.currentTimeMillis();
if (currentTime - time > 1000) {
runningSeconds++;
time = currentTime;
System.out.println(numberOfPackets + " packets/s");
numberOfPackets = 0;
}
if (!packetSent) {
packetSent = true;
port.writeByte((byte) 'd');
for (int i = 0; i < 48; i++) {
port.writeByte((byte) i);
}
} else {
byte[] received = port.readBytes();
if (received != null) {
if (received.length > 1) {
throw new IllegalStateException("One byte expected, instead got: " + received.length);
}
char cmd = (char) received[0];
if ('k' != cmd) {
throw new IllegalStateException("Expected response 'k', instead got: " + cmd);
}
packetSent = false;
numberOfPackets++;
}
}
}
}
public static void main(String[] args)
{
SerialPort port = new SerialPort("COM7");
try {
if (!port.openPort()) {
throw new IllegalStateException("Failed to open port.");
}
port.setParams(57600, 8, 1, 0);
} catch (SerialPortException e) {
throw new IllegalStateException("Exception while setting up port.", e);
}
try {
// Wait 1.5sec for Arduino to boot successfully.
Thread.sleep(1500);
} catch (InterruptedException e) {
throw new IllegalStateException("Interrupt while waiting?", e);
}
try {
testComm(port);
} catch (SerialPortException exc) {
throw new IllegalStateException("Failure while testing communication.", exc);
} finally {
try {
if (!port.closePort()) {
throw new IllegalStateException("Failed to close port.");
}
} catch (SerialPortException e) {
throw new IllegalStateException("Exception while closing port.", e);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Arduino的
void setup() {
pinMode(13, OUTPUT);
Serial.begin(57600);
}
boolean error = false;
void loop() {
if (error) {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
} else {
while (Serial.available()<49);
char cmd = Serial.read();
if ('d'!=cmd) {
error=true;
return;
}
for (int i=0; i<48; i++) {
int r = Serial.read();
if (r!=i) {
error=true;
return;
}
}
Serial.write('k');
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
NagyI,jSSC方法writeByte(字节b)是相同的writeBytes(new byte [] {b}).例如:
serialPort.writeByte((byte)0xFF);
serialPort.writeBytes(new byte[]{(byte)0xFF});
Run Code Online (Sandbox Code Playgroud)
这些字符串是等价的.writeByte()方法为要发送的每个字节创建一个新的字节数组.在Java中创建一个对象是一项昂贵的操作,这就是为什么,如果你需要高速数据传输,请使用像你的答案中那样准备好的字节数组.问候,阿列克谢.
| 归档时间: |
|
| 查看次数: |
7895 次 |
| 最近记录: |