首页 文章

Arduino Serial.write只发送一半的数据

提问于
浏览
0

我正在尝试将几个短片从Arduino Pro转移到Java程序 . 问题是,我告诉Arduino发送的数据中只有一半是实际发送的 .

设置

Arduino Sketch

我的Arduino Sketch看起来像这样:

// Mux control pins
#define s0 8
#define s1 9
#define s2 10
#define s3 11

// Mux in "SIG" pin
#define SIG_pin 3

byte lineEnd = 10;  // ASCII-NL

void setup() {  
/*
 * Mux und Flex
 */
// Define Digital Pin 8,9,10 and 11 as Output
// The most right Bit is the lowest! The highest 2 Bits are not accessible!
// --> B[-][-][13][12][11][10][9][8]
DDRB = B00001111;

// Write LOW to Pin 8,9,10 and 11
PORTB = B00000000;

Serial.begin(SERIAL_PORT_SPEED);

delay(500);
}

void loop()
{
printMux(0);
printMux(1);
printMux(2);
printMux(3);
printMux(4);

printMux(5);
printMux(6);
printMux(7);
printMux(8);
printMux(9);

printMux(10);
printMux(11);
printMux(12);
printMux(13);
printMux(14);

Serial.write('\n');

delay(17);
}

void printMux(byte channel){
// Write the channel to Pin 8,9,10 and 11
// I'm using direct Port Manipulation to speed the whole thing up
PORTB = channel;

unsigned char bytes[2];
int val = analogRead(SIG_pin);

bytes[0] = (unsigned int) val >> 8;
bytes[1] = val;

Serial.write(bytes[0]);
Serial.write(bytes[1]);
}

Sketch通过多路复用器读取15个Flex传感器的值,并将这些值作为字节写入Serial . 使用Serial.print不是一个选项,因为将值写为ASCII字节并且需要发送更多字节需要更长的时间 .

Java程序

在Java方面,我使用Java Simple Serial Connection获取Ardiuno的串行输出,然后将字节解析为short .

import java.util.ArrayDeque;

import jssc.SerialPort;
import jssc.SerialPortException;

public class SerialTest {

final static int ASCII_NL = 10;
final static int ASCII_COMMA = 42;
final static String BLUETOOTH_PORT = "COM10";
final static String USB_PORT = "COM3";
final static boolean USE_BLUETOOTH = false;

public static void main(String[] args) {
    final SerialPort sp = new SerialPort(USE_BLUETOOTH ? BLUETOOTH_PORT
            : USB_PORT);
    final ArrayDeque<Byte> byteVector = new ArrayDeque<>(48);

    try {
        sp.openPort();
        sp.setParams(SerialPort.BAUDRATE_115200, SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        Runnable r = new Runnable() {
            @Override
            public void run() {

                while (true) {
                    try {
                        byte[] bytesRead = sp.readBytes(60);
                        if (bytesRead != null) {
                            for (byte b : bytesRead) {

                                if (b == ASCII_NL) {
                                    // We have to wait for a whole line
                                    // as a short is a 2 byte value
                                    // 15 shorts produce 30 bytes
                                    if (byteVector.size() == 30) {
                                        byte[] buffer = new byte[byteVector
                                                .size()];
                                        for (int i = 0; i < byteVector
                                                .size(); i++) {
                                            buffer[i] = byteVector.poll();
                                        }
                                        extractValues(buffer);
                                    }
                                    byteVector.clear();
                                } else {
                                    byteVector.add(b);
                                }
                            }
                        }
                    } catch (SerialPortException e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(17);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread t = new Thread(r);
        t.start();
    } catch (SerialPortException e) {
        e.printStackTrace();
    }
}

static void extractValues(byte[] buffer) {
    for(byte b : buffer) {
        System.out.print(b + " ");
    }
    System.out.println();
    }
}

问题

至于测试目的,我只将收到的字节打印到控制台 . 我希望输出像这样:

1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32

那些是30个字节,所以我们收到了15个短裤 .

问题是输出看起来像这样:

1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 -32 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

只有一半的字节被正确传输,其余字节被发送为 0 .

进一步测试证实了这一当我改变Arduino Sketch只发送10个值并相应地修改Java程序时,它打印出10个值后跟10个零 . 对于Arduino等发送的4个值也是如此 .

为什么会这样?我是否通过串行发送过多数据,所以它在读取时只发送0?我的Java程序有问题吗?这可能是Java简单串行通信库中的错误吗?

编辑

错误位于Java SimpleSerialConnection中,在更改为Ardulink与Arduino进行通信后,它几乎完美无缺 . 我正在为JSSC提交一份错误报告,案件已经结束 .

1 回答

  • 0

    将问题分成几部分:

    • 使用可靠的终端程序捕获Arduino的串行通信 .

    • 使用其他程序模拟所需的输出,并查看Java代码是否正确捕获它 .

    • 限制输出以查看问题是否消失(指向某些缓冲区溢出)

    • 通过ascii通信替换二进制文件以使分析更容易 .

    换句话说:确保它运行,然后调整性能 .

相关问题