Java串口通信总结

来源:互联网 发布:网络安全法考试题库 编辑:程序博客网 时间:2024/05/16 08:50

最近在研究一个东西要用到串口的读写,通过读取串口COM2获取一个指令,然后根据该指令通过配置文件获取其他的信息再通过串口COM1进行做下一步的一系列操作。 
java读写串口要用到的是javax扩展类库javacomm,它是一系列的标准,该类库在sun的官网上只提供linux版本。由于我所应用的平台是win32,所以找了个win32的实现。在此过程中遇到了一系列的问题,后来终于解决问题并完成了通过读写串口进行通信的功能。在此对其java读写串口的情况做个简要说明:

首先下载javacomm包,然后将其解压,解压后需要做的就是配置串口通信相关东西,下面是自己使用的时候的配置:

1.把javax.comm.properties文件拷贝到Java运行时环境的lib目录中,即:
C:/Program Files/Java/jdk1.5.0_12/lib/javax.comm.properties
C:/Program Files/Java/jdk1.5.0_12/jre/lib/javax.comm.properties

2.把win32com.dll拷贝到C:/windows/system32/ 和 C:/Program Files/Java/jdk1.5.0_12/jre/bin/下

3.把comm.jar放到Java运行时环境的lib目录中,即C:/Program Files/Java/jdk1.5.0_12/lib/comm.jar

4.在"环境变量"的CLASSPATH中添加comm.jar,如:%JAVA_HOME%/lib/comm.jar;

然后通过COM的API来实现串口的读写,其实API相当简单,如下代码:

Java代码

.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}
隐藏行号 复制代码 Source
  1. CommPortIdentifier serialPortId = CommPortIdentifier.getPortIdentifier(“COM2”);   
  2. SerialPort  port = (SerialPort) serialPortId.open(“Read”, 30);//打开COM2串口,其中30参数是用来设置打开串口的超时时间。   
  3. port.setSerialPortParams(1200, 8, 1, 0);//设置COM2的波特率,数据位,停止位,校验方式等   
  4.   
  5. //从串口中得到输入输出流了   
  6. OutputStream out = port.getOutputStream();   
  7. out .write(buffer);   
  8. out .flush();   
  9.   
  10. InputStream in = port.getInputStream();   
  11. in.read(data);//data是一个byte[]

如果COM1通信要求的波特率是1200,而系统默认的波特率是9600,在系统的设备管理器中修改了COM1的波特率为1200,那么这个时候修改后程序不报错误,但是在串口里获取不到任何信息,也就是在设备管理器修改COM1的波特率是没有用的,必须通过port.setSerialPortParams(1200, 8, 1, 0)来设置其波特率。

原创粉丝点击