arduino serial.write只发送一半的数据

zzwlnbp8  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(400)

我试着把arduino pro的几条短裤转换成java程序。问题是,我告诉arduino发送的数据中只有一半是实际发送的。

设置

阿杜伊诺素描

我的arduino素描如下:

// 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]);
}

草图通过多路复用器读取15个flex传感器的值,并将这些值作为字节写入串行存储器。使用serial.print不是一个选项,因为将值写入ascii字节需要相当长的时间,而且要发送的字节也更多。

java程序

在java方面,我使用java简单的串行连接来获取ardiuno的串行输出,然后将字节解析回shorts。

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草图,使其只发送10个值并相应地修改java程序时,它将输出10个值,后跟10个零。这同样适用于arduino等发送的4个值。
为什么会这样?我是否在串行上发送了太多的数据,所以读取时只发送0?我的java程序有问题吗?这可能是java简单串行通信库中的错误吗?

编辑

错误在java simpleserialconnection中的某个地方,在更改为ardulink以与arduino通信之后,它几乎完美地工作了。我正在为jssc提交一份错误报告,案子结束了。

m0rkklqb

m0rkklqb1#

把你的问题分成几部分:
用可靠的终端程序捕获arduino的串行通信。
使用不同的程序来模拟所需的输出,并查看java代码是否正确捕获了它。
限制输出以查看问题是否消失(将指向某个缓冲区溢出)
用ascii通信代替二进制,使分析更容易。
换句话说:确保它运行,然后调整性能。

相关问题