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

}

}

}

相关推荐
行墨10 分钟前
Jetpack Compose 深入浅出(二)——基础组件Text
android
雨白2 小时前
深入理解协程的运作机制 —— 调度、挂起与性能
android·kotlin
沐怡旸2 小时前
【Android】Android系统体系结构
android
namehu2 小时前
React Native 应用性能分析与优化不完全指南
android·react native·ios
xqlily3 小时前
Kotlin:现代编程语言的革新者
android·开发语言·kotlin
HelloBan3 小时前
如何正确去掉SeekBar的Thumb和按压效果
android
木易 士心3 小时前
Android EventBus 源码解析:设计模式、原理与实现
android
ClassOps3 小时前
源码阅读 LeakCanary
android
用户2018792831673 小时前
为啥现在 Android App 不用手动搞 MultiDex 了?
android
fouryears_234174 小时前
如何将Vue 项目转换为 Android App(使用Capacitor)
android·前端·vue.js