通过JAVA与串口(RS232)通信实例

最近了解到的需求是需要需激光打刻机进行(RS232)串口通信, 
这里使用的是RXTX开源包实现的。 
之前并没有用java做过串口通信,而且这方面资料不是很多。 
项目实际应用中可能会采用VB开发(这个我就不会了) 

只不过用java尝试一下,记个笔记,希望可以对相关开发用些帮助。 

下面是实现代码 
Java代码   收藏代码
  1. package test;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.InputStreamReader;  
  6. import java.io.OutputStream;  
  7. import java.util.Date;  
  8. import java.util.Enumeration;  
  9. import java.util.TooManyListenersException;  
  10.   
  11. import gnu.io.CommPortIdentifier;  
  12. import gnu.io.PortInUseException;  
  13. import gnu.io.SerialPort;  
  14. import gnu.io.SerialPortEvent;  
  15. import gnu.io.SerialPortEventListener;  
  16. import gnu.io.UnsupportedCommOperationException;  
  17.   
  18. public class CommUtil implements SerialPortEventListener {  
  19.   
  20.     InputStream inputStream; // 从串口来的输入流  
  21.     OutputStream outputStream;// 向串口输出的流  
  22.     SerialPort serialPort; // 串口的引用  
  23.     CommPortIdentifier portId;  
  24.   
  25.     public CommUtil(Enumeration portList, String name) {  
  26.         while (portList.hasMoreElements()) {  
  27.             CommPortIdentifier temp = (CommPortIdentifier) portList.nextElement();  
  28.             if (temp.getPortType() == CommPortIdentifier.PORT_SERIAL) {// 判断如果端口类型是串口  
  29.                 if (temp.getName().equals(name)) { // 判断如果端口已经启动就连接  
  30.                     portId = temp;  
  31.                 }  
  32.             }  
  33.         }  
  34.         try {  
  35.             serialPort = (SerialPort) portId.open("My"+name, 2000);  
  36.         } catch (PortInUseException e) {  
  37.   
  38.         }  
  39.         try {  
  40.             inputStream = serialPort.getInputStream();  
  41.             outputStream = serialPort.getOutputStream();  
  42.         } catch (IOException e) {  
  43.         }  
  44.         try {  
  45.             serialPort.addEventListener(this); // 给当前串口天加一个监听器  
  46.         } catch (TooManyListenersException e) {  
  47.         }  
  48.         serialPort.notifyOnDataAvailable(true); // 当有数据时通知  
  49.         try {  
  50.             serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8, // 设置串口读写参数  
  51.                     SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);  
  52.         } catch (UnsupportedCommOperationException e) {  
  53.         }  
  54.     }  
  55.   
  56.     public void serialEvent(SerialPortEvent event) {  
  57.         switch (event.getEventType()) {  
  58.         case SerialPortEvent.BI:  
  59.         case SerialPortEvent.OE:  
  60.         case SerialPortEvent.FE:  
  61.         case SerialPortEvent.PE:  
  62.         case SerialPortEvent.CD:  
  63.         case SerialPortEvent.CTS:  
  64.         case SerialPortEvent.DSR:  
  65.         case SerialPortEvent.RI:  
  66.         case SerialPortEvent.OUTPUT_BUFFER_EMPTY:  
  67.             break;  
  68.           
  69.         case SerialPortEvent.DATA_AVAILABLE:// 当有可用数据时读取数据,并且给串口返回数据  
  70.             byte[] readBuffer = new byte[20];  
  71.   
  72.             try {  
  73.                 while (inputStream.available() > 0) {  
  74.                     System.out.println(inputStream.available());  
  75.                     int numBytes = inputStream.read(readBuffer);  
  76.                     System.out.println(numBytes);  
  77.                 }  
  78.                 System.out.println(new String(readBuffer).trim());  
  79.             } catch (IOException e) {  
  80.                 e.printStackTrace();  
  81.             }  
  82.             break;  
  83.         }  
  84.     }  
  85.     public void send(String content){  
  86.         try {  
  87.             outputStream.write(content.getBytes());  
  88.         } catch (IOException e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.     }  
  92.       
  93.     public void ClosePort() {  
  94.         if (serialPort != null) {  
  95.           serialPort.close();  
  96.         }  
  97.       }  
  98.   
  99.       
  100. }  


测试 
Java代码   收藏代码
  1. package test;  
  2.   
  3. import gnu.io.CommPortIdentifier;  
  4.   
  5. import java.util.Enumeration;  
  6.   
  7. public class Test {  
  8.   
  9.     public static void main(String[] args) throws InterruptedException {  
  10.         Enumeration portList = CommPortIdentifier.getPortIdentifiers(); //得到当前连接上的端口  
  11.           
  12.         CommUtil comm3 = new CommUtil(portList,"COM3");  
  13.         int i = 0;  
  14.         while(i<5)  
  15.         {  
  16.             Thread.sleep(3000);  
  17.             comm3.send("hello");  
  18.             i++;  
  19.         }  
  20.         comm3.ClosePort();  
  21.     }  
  22.   
  23. }  
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值