android 控制主板串口

import com.fazecast.jSerialComm.SerialPort;

import java.nio.charset.StandardCharsets;

public class SendAndReceiveFromAllPorts {

public static void main(String[] args) {

SerialPort[] ports = SerialPort.getCommPorts();

// 统一的发送指令(16 进制)

byte[] sendCommand = new byte[]{0xAA, 0xBB, 0xCC};

for (SerialPort port : ports) {

port.openPort();

port.setBaudRate(9600);

// 发送指令

port.writeBytes(sendCommand, sendCommand.length);

// 接收响应

byte[] receiveBuffer = new byte[1024];

int numRead = port.readBytes(receiveBuffer, receiveBuffer.length);

if (numRead > 0) {

String receivedData = new String(receiveBuffer, 0, numRead, StandardCharsets.UTF_8);

System.out.println("从端口 " + port.getSystemPortName() + " 接收到的数据: " + receivedData);

}

port.closePort();

}

}

}

相关推荐
志存高远6637 分钟前
(面试)Android各版本新特性
android
IT从业者张某某1 小时前
信奥赛-刷题笔记-队列篇-T3-P3662Why Did the Cow Cross the Road II S
android·笔记
未来之窗软件服务1 小时前
Cacti 未经身份验证SQL注入漏洞
android·数据库·sql·服务器安全
BXCQ_xuan1 小时前
handsome主题美化及优化:10.1.0最新版 - 2
android
圈圈编码1 小时前
MVVM框架
android·学习·kotlin
橙子199110163 小时前
在 Kotlin 中,什么是解构,如何使用?
android·开发语言·kotlin
androidwork4 小时前
Android 中使用通知(Kotlin 版)
android·kotlin
Digitally5 小时前
如何从 Android 设备打印短信(5 种方法)
android
casual_clover6 小时前
Android 中 打开文件选择器(ACTION_OPEN_DOCUMENT )
android
_龙小鱼_7 小时前
卡顿检测与 Choreographer 原理
android·kotlin