Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino与Java的接口问题_Java_C_Serial Port_Arduino - Fatal编程技术网

Arduino与Java的接口问题

Arduino与Java的接口问题,java,c,serial-port,arduino,Java,C,Serial Port,Arduino,我遵循了测试流程,但在测试中遇到了一些问题。目前,当我尝试手动输入时,我的Arduino程序运行良好 Arduino上的C代码段: char line[20]; int line_pos = 0; char action[10]; unsigned long duration; boolean data_ready = false; void setup() { Serial.begin(9600); delay(1500); Serial.println("Ready!"); }

我遵循了测试流程,但在测试中遇到了一些问题。目前,当我尝试手动输入时,我的Arduino程序运行良好

Arduino上的C代码段:

char line[20];
int line_pos = 0;
char action[10];
unsigned long duration;
boolean data_ready = false;

void setup()
{
  Serial.begin(9600);
  delay(1500);
  Serial.println("Ready!");
}

void loop()
{
char state;
if(Serial.available()>0)
{
    delay(20);
    state = Serial.read();

    if(state == '!')
    {
      data_ready = true;
      line[line_pos] = state;
      line_pos = 0;
    }
    else
    {
      line[line_pos] = state;
      line_pos = line_pos + 1;
    }

    if(data_ready == true)
    {
        split(line);
        Serial.print(action);
        delay(20);
        Serial.print("!");
        delay(20);
        Serial.print(duration);
        delay(20);
        Serial.print("!");

        Serial.println("Data has been split");

        if(strcmp(action, "left") == 0)
        {
            Serial.println("Received left");
        }
        else if(strcmp(action, "right") == 0)
        {
            Serial.println("Received rightt");
        }
        else if(strcmp(action, "straight") == 0)
        {
            Serial.println("Received straight");
        }

        memset(line, 0, 20);
        data_ready = false;
    }
    }
}  

void split(char input[20])
{
char *param, *ptr;

param = strtok_r(input, "#", &ptr);
strncpy(action, param, sizeof(action));
action[sizeof(action)-1] = '\0';

param = strtok_r(NULL, "!", &ptr);
duration = strtoul(param, &param, 10);
}
当我输入“left#123!”时,我在COM中得到如下输出,结果与预期一致。这里没有问题:

准备好了! 左边123!数据已被拆分 收到左键

下面是我的Java代码:

import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.util.Enumeration;

public class SerialTest implements SerialPortEventListener {
    SerialPort serialPort;
        /** The port we're normally going to use. */
    private static final String PORT_NAMES[] = { 
            "/dev/tty.usbserial-A9007UX1", // Mac OS X
            "/dev/ttyUSB0", // Linux
        "COM3", // Windows
        };
/** Buffered input stream from the port */
private InputStream input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;

private String display = "";

public void initialize() {
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    // iterate through, looking for the port
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
        for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
                portId = currPortId;
                break;
            }
        }
    }

    if (portId == null) {
        System.out.println("Could not find COM port.");
        return;
    }

    try {
        // open serial port, and use class name for the appName.
        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                TIME_OUT);

        // set port parameters
        serialPort.setSerialPortParams(DATA_RATE,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        // open the streams
        input = serialPort.getInputStream();
        output = serialPort.getOutputStream();

        // add event listeners
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        System.err.println(e.toString());
    }
}

/**
 * This should be called when you stop using the port.
 * This will prevent port locking on platforms like Linux.
 */
public synchronized void close() {
    if (serialPort != null) {
        serialPort.removeEventListener();
        serialPort.close();
    }
}

/**
 * Handle an event on the serial port. Read the data and print it.
 */
public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            int available = input.available();
            byte chunk[] = new byte[available];
            input.read(chunk, 0, available);

            System.out.println(display);

            display += (new String(chunk)).trim();
            if(display.contains("!"))
            {
                display = display.substring(0, display.indexOf("!"));

                if(display.equals("Ready"))
                {
                    String reply = "left#123!";
                    byte reply_byte[] = new byte[reply.length()];
                    reply_byte = reply.getBytes("UTF-16LE");
                    output.write(reply_byte);
                }

                display = "";
            }

        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    // Ignore all the other eventTypes, but you should consider the other ones.
}

public static void main(String[] args) throws Exception {
    SerialTest main = new SerialTest();
    main.initialize();
    System.out.println("Started");
}
}
以下是Java代码在控制台中打印出来的内容:

Rea L 0 资料 数据仓库 达塔哈斯比 Datahasbe sp 数据已被拆分


串行输入似乎不是以流畅的方式读取,而是以重复的部分读取。有人能帮我诊断问题并给我一个解决方案吗?

这里至少有两件事需要注意,但这是基于我对Java以外的语言的经验,因此它可能不会以完全相同的方式运行

首先,事件看起来像是在几个字符后触发的,也许你的Arduino发送速度不够快,无法同时显示所有字符。这会导致Java代码重复输出,因为您正在打印
display
,然后将新的
块添加到其中。如果你找不到感叹号,它永远不会被清除。您最好打印
chunk
,以便准确查看收到的内容

第二种情况是,您可能只接收新数据的事件,因此,如果在触发事件和调用
input.read()
之间出现新数据,则不会触发另一个事件。最好在事件处理程序中有一个循环,在仍然有可用数据时继续,例如:

public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            int available = input.available();
            while (available > 0)
            {
                byte chunk[] = new byte[available];
                input.read(chunk, 0, available);

                System.out.println(new String(chunk));

                // Perform your packet processing here

                // See if there is any more data that came in while we were
                // processing the event
                available = input.available();
            }

        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    // Ignore all the other eventTypes, but you should consider the other ones.
}

谢谢在处理事件时,如何准确地检查是否有新数据传入?我如何知道这些新数据是否不是重复的旧数据?@ask:您使用循环检查新数据,调用
input.available()
,并进行处理,直到其读数为零。它不会重复旧数据,因为当您调用
input.read()
时,将从输入中提取您读取的字节数,以便下次不会读取它们。它正常工作了吗。但是使用了if语句,而不是while循环。每当一个字节到达时,就会调用serialEvent函数。