从串行端口读取给出拆分字符串 [英] Reading from serial port gives split up string

查看:89
本文介绍了从串行端口读取给出拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSSC类从Arduino Uno接收字符串.Arduino通过COM3连接到我的计算机.在Arduino上的setup void中,它将向Java程序发送一个字符串,表明Arduino准备读取串行数据.发生的是,当Java程序读取串行端口时,它将Arduino的字符串拆分为多行并带有空格.我想这是Java程序在接收到数据时正在打印数据,而不是等待完整的字符串.我该怎么做,以便程序从Arduino读取字符串并将其保存为字符串,然后将其打印到控制台.

I am using the JSSC class to receive a string from an Arduino Uno. The Arduino is connected to my computer via COM3. In the setup void on the Arduino, it sends a string to the java program saying that the Arduino is ready to read serial data. What happens is when the java program reads the serial port, it splits up the string from the Arduino onto multiple lines with spaces. I imagine it is that java program is printing the data when its received instead of waiting for the full string. How could I make it so that the program reads the string from the Arduino and saves it to a string and then prints it to console.

Java:

package jtac;

import jssc.*;

public class JTAC {

    public static SerialPort serialPort = new SerialPort("COM3");
    public static PortReader portreader = new PortReader(serialPort);
    public static boolean ready = false;

    public static void main(String[] args) {
        try {
            serialPort.openPort();//Open serial port
            //Thread.sleep(2000);
            serialPort.setParams(SerialPort.BAUDRATE_9600, 
                                 SerialPort.DATABITS_8,
                                 SerialPort.STOPBITS_1,
                                 SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
            serialPort.addEventListener(portreader, SerialPort.MASK_RXCHAR);
            while(!ready) {} //Wait for Arduino to fire up
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }   

    }

}
class PortReader implements SerialPortEventListener {

    SerialPort serialPort;
    public PortReader(SerialPort serialPort) {
        this.serialPort = serialPort;
    }

    @Override
    public void serialEvent(SerialPortEvent event) {
        if (event.isRXCHAR() && event.getEventValue() > 0) {
            try {
                String receivedData = serialPort.readString(event.getEventValue());
                System.out.println(receivedData);
                if(receivedData == "Arduino Ready") JTAC.ready = true;

            } catch (SerialPortException ex) {
                System.out.println("Error in receiving string from COM-port: " + ex);
            }
        }
    }
}

Arduino:

String input;

void setup() {
  Serial.begin(9600);
  Serial.print("Arduino Ready");
}

void loop() {

}

这是控制台的输出:

      Ard
uino
 Rea
dy

无法从控制台输出中复制Ard前面的空格.有什么办法可以让我一次完成所有工作吗?将来,我可能还需要再次从Arduino接收数据.谢谢.

The spaces in front of Ard couldn't be copied from the console output. Is there a way I could make this all on one line? I might need to receive data from the Arduino again in the future as well. Thanks.

推荐答案

使用"#Arduino Ready $"之类的开始和结束标记

Use Start and End Markers like "#Arduino Ready$"

其中#" 表示开始,而"$" 表示您感兴趣的字符串的结尾.然后您可以使用它们来处理接收到的字节并根据标记进行存储.

Where "#" denotes starting and "$" denotes ending of string you interested. then you can use them to process the received bytes and store them based on the markers.

在论坛中查看此很棒的教程.

我的代码可能是完全错误的[我是Java中的一个完整的菜鸟],但是我认为您可以理解整体思想.

I might be completely wrong with the code [i'm a complete noob in java], but I think you can understand the overall idea.

串行事件方法应如下所示

Serial Event Method should be look like this

if (event.isRXCHAR() && event.getEventValue() > 0) {
    try {
        char receivedData = serialPort.read(event.getEventValue());
        if (recvData) === '#' {
            this.readString = 1
        }else if(recvData === '$'){
            this.readString = 0
        }

        if(this.readString == 1){
            this.recvStringData.append(receivedData)
            System.out.println(this.recvStringData);
        }

        if(this.recvStringData == "Arduino Ready") JTAC.ready = true;

    } catch (SerialPortException ex) {
        System.out.println("Error in receiving string from COM-port: " + ex);
    }
}

然后将是Arduino代码

And Arduino code will be

void setup(){
   Serial.begin(9600);
   while(!Serial);
}

void loop(){
    Serial.println("#Hi There$");
}

这篇关于从串行端口读取给出拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆