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();

}

}

}

相关推荐
敲代码的鱼哇40 分钟前
发送短信/拨打电话/获取联系人能力 UTS 插件(cz-sms)
android·前端·ios·uni-app·安卓·harmonyos·鸿蒙
用户5052372099151 小时前
Android 13/14 通知权限与前台服务适配指南
android
用户5052372099151 小时前
Android 12 适配指南:SplashScreen API 与 PendingIntent 变更
android
用户5052372099151 小时前
一张表看懂 Android 8-15 所有适配要点
android
_祝你今天愉快2 小时前
Android 12 (AOSP) 添加自定义系统服务
android
程序员陆业聪4 小时前
AI编码提效实战:Skill、Rule与上下文工程
android
程序员陆业聪4 小时前
AI驱动需求梳理与Spec编写:让PRD自动变成技术方案
android
李艺为6 小时前
Android Studio使用switch匹配资源id时报需要常量表达式解决办法
android
YaBingSec8 小时前
玄机靶场-2024ccb初赛sc05 WP
android·运维·网络·笔记·安全·ssh
常利兵8 小时前
解锁Android嵌入式照片选择器,让你的App体验丝滑起飞
android