JAVA 串口编程(二)
来源:互联网 发布:淘宝高仿鞋哪些店铺 编辑:程序博客网 时间:2024/05/29 18:08
三、实例
(1)打开、关闭串口
首先使用CommPortIdentifier中的方法,获取可用的端口,并且选择一个端口打开作为通信端口。
A:枚举可用端口
1 void listPortChoices() 2 {3 CommPortIdentifier portId;4 Enumeration en = CommPortIdentifier.getPortIdentifiers();5 while (en.hasMoreElements()) 6 {7 portId = (CommPortIdentifier) en.nextElement();8 if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) 9 System.out.println(portId.getName());10 }11 portChoice.select(parameters.getPortName());12 }13 14
B:打开指定的端口
1 CommPortIdentifier portId;2 try 3 {4 //生成CommPortIdentifier类的实例5 portId = CommPortIdentifier.getPortIdentifier("COM4");6 } 7 catch (NoSuchPortException e)8 {9 e.printStackTrace();10 }11 try 12 {13 //打开串口COM4,设置超时时限为3000毫秒14 serialPort = (SerialPort) portId.open("testApp", 3000);15 } 16 catch (PortInUseException e)17 {18 e.printStackTrace();19 }20 21
C:关闭端口
使用完的端口,必须记得将其关闭,否则其它的程序将无法使用该端口,CommPortIdentifier类只提供了开启端口的方法,而要关闭端口,则要调用CommPort类的close()方法。
serialPort.close()
(2)设置串口参数
1 try {2 try {3 serialPort.setSerialPortParams(9600, // 波特率4 SerialPort.DATABITS_8,// 数据位数5 SerialPort.STOPBITS_1, // 停止位6 SerialPort.PARITY_NONE);// 奇偶位7 } 8 catch (UnsupportedCommOperationException e)9 {10 e.printStackTrace();11 }12
(3)串口的读、写
A:向串口写数据
1 OutputStream outputStream;2 try3 {4 outputStream = serialPort.getOutputStream();5 } 6 catch (IOException e)7 {8 e.printStackTrace();9 }10 bt = new byte[] { (byte) 0x55, (byte) 0xAA, (byte) 0xF1 };11 try12 {13 outputStream.write(bt);14 outputStream.flush();15 outputStream.close();16 }17 catch (IOException e)18 {19 e.printStackTrace();20 }21
B:读取串口中的数据
读操作,需继承SerialPortEventListener接口。为SerialPort添加监听Listener。实现该接口的serialEvent (SerialPortEvent event)方法。
1 public class SerialRead implements SerialPortEventListener{2 3 InputStream inputStream;4 byte[] rBuffer = new byte[38];5 SerialRead(SerialPort serialPort)6 {7 try {8 serialPort.addEventListener((SerialPortEventListener) this);9 this.serialPort.notifyOnDataAvailable(true);10 11 } catch (TooManyListenersException e) {12 }13 14 try {15 if (serialPort != null)16 inputStream = serialPort.getInputStream();17 } catch (IOException e) {18 }19 }20 21 public void serialEvent(SerialPortEvent event) 22 {23 if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE)24 { 25 int count = 0;26 try 27 {28 while ((ch = inputStream.read()) != -1) 29 { 30 bt[count++]=(byte)ch;31 }32 }33 catch (IOException e) 34 {35 e.printStackTrace();36 }37 } 38
}
阅读全文
0 0
- JAVA 串口编程(二)
- JAVA 串口编程(二)
- JAVA 串口编程(二)
- Win32 串口编程(二)
- Win32 串口编程(二)
- Win32 串口编程(二)
- Win32 串口编程(二)
- java串口编程(起步)
- JAVA 串口编程 (一)
- JAVA 串口编程 (三)
- JAVA 串口编程 (一)
- JAVA 串口编程 (三)
- JAVA 串口编程 (三)
- JAVA 串口编程 (一)
- WinCE串口编程API函数(二)
- SerialPort 串口编程知识整理(二)
- UART0串口编程系列(二)
- Linux下的串口编程(二)
- hdu2446--二分搜索+打表
- gcc与BOM
- SoftMax和代价函数
- 基于分布式的抢红包服务器
- Failed to resolve: 之一
- JAVA 串口编程(二)
- 数据滤波处理
- 有关A^B(hdoj2035)
- 临界区锁 InitializeCriticalSection()
- 带有xavier初始化、dropout的多层神经网络
- Linux 系统管理之进程管理:目的、方法、命令
- GreenDao数据库
- Nginx的session一致性问题——memcached缓存session共享
- tiny6410的yaffs2文件系统的制作及其安装(1)