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

}

}

}

相关推荐
Kapaseker16 分钟前
Kotlin 精讲 — companion object
android·kotlin
studyForMokey1 小时前
【Android面试】打包 & 启动专题
android·面试·职场和发展
zh_xuan1 小时前
Android Jetpack 使用Room数据库
android·android jetpack·room
斯密码赛我是美女1 小时前
周报--2
android·数据库
zhangren024681 小时前
Laravel9.x核心特性全解析
android
耶叶2 小时前
Android 开发:基于Scaffold的电子邮件App
android·android-studio
三少爷的鞋2 小时前
是时候告别业务层 Manager 了:Android 架构升级到 UseCase + Repository
android
erniu2222222 小时前
android dex2oat 编译dex文件分析
android
恋猫de小郭2 小时前
Flutter 3.41.6 版本很重要,你大概率需要更新一下
android·前端·flutter
野生的码农10 小时前
放过自己,降低预期,及时行乐
android·ai编程