用Java上位机给单片机写串口指令,简单可用代码如下:
import java.io.InputStream; import java.io.OutputStream; import javax.comm.*; public class test { public static void main(String args[]) throws Exception { CommPortIdentifier serialPortId = CommPortIdentifier.getPortIdentifier(“COM3”); SerialPort port = (SerialPort) serialPortId.open(“Read”, 30); //打开COM3串口,其中30是打开串口的超时时间 port.setSerialPortParams(9600, 8, 1, 0); //设置COM2的波特率,数据位,停止位,校验方式 //从串口中得到输入输出流了 byte[] buffer = {0x0F,0x01,0x00};//controlling command OutputStream out = port.getOutputStream(); out .write(buffer); out .flush(); InputStream in = port.getInputStream(); in.read(data); //data是一个byte[] } }
Reference
[1].http://blog.csdn.net/pigteacher/article/details/5619955
时间: 2024-10-12 13:41:32