串口通讯读取值并且转换

package com.example.demo.dao;
import com.fazecast.jSerialComm.SerialPort;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Component
public class GetData {
    public int[] ReadConventionalData() throws InterruptedException {
        SerialPort port = SerialPort.getCommPort("COM9");
        port.openPort();
        port.setComPortParameters(115200, 8, 1, 0);

        int[] array = new int[3];
        StringBuilder sb = new StringBuilder();

        try {
            while (true) {
                if (port.bytesAvailable() > 0) {
                    byte[] buffer = new byte[port.bytesAvailable()];
                    port.readBytes(buffer, buffer.length);
                    sb.append(new String(buffer));

                    // Ensure we have a complete set of data
                    String data = sb.toString();
                    if (data.contains("X:") && data.contains("Y:") && data.contains("Z:")) {
                        String[] parts = data.split("[XYZ]+");
                        if (parts.length >= 4) {
                            try {
                                int xValue = Integer.parseInt(parts[1].replaceAll("[^0-9-]", "").trim());
                                int yValue = Integer.parseInt(parts[2].replaceAll("[^0-9-]", "").trim());

                                // Extract zValue safely
                                String zString = parts[3].substring(0, Math.min(5, parts[3].length())).replaceAll("[^0-9-]", "").trim();
                                int zValue = Integer.parseInt(zString);

                                array[0] = xValue;
                                array[1] = yValue;
                                array[2] = zValue;

                                System.out.println("X: " + xValue + ", Y: " + yValue + ", Z: " + zValue);

                                // Remove processed data
                                sb.delete(0, sb.indexOf("Z:") + 2);

                                port.closePort();
                                return array;
                            } catch (NumberFormatException | StringIndexOutOfBoundsException e) {
                                System.err.println("Error parsing data: " + e.getMessage());
                                // Handle the exception if needed
                                sb.delete(0, sb.indexOf("Z:") + 2); // Remove faulty data
                            }
                        }
                    }
                }
                Thread.sleep(100); // Adjust sleep time as necessary
            }
        } finally {
            port.closePort(); // Ensure port is closed in all cases
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值