一. 实验目的及实验环境<1>实验环境Java eclipse下<2>实验目的了解串行通信的背景知识后,通过三线制制作一条串口通信线(PC-PC),并编程实现两台PC间通过RS-232C通信。
要求两台PC机能进行实时的字符通信,并了解工业自动化控制中的基本通信方式。
二.实验内容1、检查PC是否具有串行通信接口,并按其针脚类准备一条串口通信线缆。
2、串口包的安装,下载javacomm20-win32.zip并解压,将win32com.dll复制到<JDK>\bin目录下;将comm.jar复制到<JDK>\lib;把m.properties 也同样拷贝到<JDK>\lib目录下,再将上面提到的文件放到JRE相应的目录下就可以了。
三、方案设计1、将实验所需RS-232缆线准备好,并将JAVA串口包复制到相应地目录下。
2、查找有关串口通信的书籍以及在网上查找相应地串口通信代码。
3、用JAVA编程软件JCreator编写代码。
四.测试数据及运行结果图一主界面图二发送消息图三接收消息五.总结1、实验过程中遇到的问题及解决办法;串口包的安装配置比较难完成,最后在网上看各种博客和论坛,才将问题解决。
还有一些代码问题,最后找同学调试好了。
2、对设计及调试过程的心得体会。
通过本次串口实验,我对串口通信的知识了解的更透彻,这是在刚开始对串口通信知识不了解的情况下就编程而造成许多错误之后才得到的结果。
在网上查找资料的时候也接触到了不少其他的编程语言例如VB,delphi,C#等,这也让我对这些从没有学过的语言有所了解,我想这些知识对以后的实验工作都有帮助。
我也进一步发现了自己动手能力和自学能力都得到很多的进步,同时也对串口的发送与接收信息有了进一步的了解。
六.附录:源代码确认本机可以使用的串口:package test;import java.util.Enumeration;import java.util.HashMap;import mPortIdentifier;import m.SerialPort;public class GetSerialPorts {public void listPortChoices() {CommPortIdentifier portId;Enumeration en = CommPortIdentifier.getPortIdentifiers();// iterate through the ports.while (en.hasMoreElements()) {portId = (CommPortIdentifier) en.nextElement();if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { System.out.println(portId.getName());}}}public static void main(String[] args) {GetSerialPorts GSP = new GetSerialPorts();GSP.listPortChoices();}}打开串口,关闭串口:package test;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.HashMap;import mPortIdentifier;import m.PortInUseException;import m.SerialPort;import m.UnsupportedCommOperationException; public class GetSerialPorts {private CommPortIdentifier portId;private SerialPort testPort;private CommPortIdentifier myPort;private InputStream is;private OutputStream os;public void listPortChoices() {Enumeration en = CommPortIdentifier.getPortIdentifiers();// iterate through the ports.while (en.hasMoreElements()) {portId = (CommPortIdentifier) en.nextElement();if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { System.out.println(portId.getName());}myPort = portId;// 任意取一个串口,比如com1}}public boolean openPort() {try {testPort = (SerialPort) myPort.open("COM1", 500);// 注意这里必须换成一个真实的串口try {this.testPort.setSerialPortParams(38400, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);} catch (UnsupportedCommOperationException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {this.testPort.enableReceiveTimeout(30);} catch (UnsupportedCommOperationException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.testPort.setOutputBufferSize(1024);this.testPort.setInputBufferSize(1024);try {this.is = this.testPort.getInputStream();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {this.os = this.testPort.getOutputStream();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.testPort.notifyOnDataAvailable(true);this.testPort.notifyOnOutputEmpty(true);this.testPort.notifyOnBreakInterrupt(true);// this.printerPort.addEventListener(new PrintPortListener(is));System.out.println("打开com1机串口成功");return true;} catch (PortInUseException e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}}/****** TODO 关闭端口*****/public boolean closePort() {// TODO Auto-generated method stubtry {if (null != this.testPort) {is.close();os.close();this.testPort.close();}System.out.println("关闭COM1串口成功");return true;} catch (Exception e) {// TODO Auto-generated catch block// e.printStackTrace();System.out.println("关闭COM1串口失败");return false;}}public static void main(String[] args) {GetSerialPorts GSP = new GetSerialPorts();GSP.listPortChoices();GSP.openPort();}}读数据:/****** TODO 接收端口數******/public String readData(InputStream is) {// 读取缓冲区域byte[] readBuffer = new byte[4096];int readDataLength = 0;try {readDataLength = is.read(readBuffer);// for (byte b : readBuffer) {// System.out.print(b);// }// System.out.println();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}// 将真实数据保存到零时数组中byte[] readTemp = new byte[readDataLength];for (int i = 0; i < readDataLength; i++) {readTemp[i] = readBuffer[i];}// 将byte数组转换为16进制字符串String stringTemp = FeelTheBase.bytesToHexString(readTemp);// System.out.println("指令返回值" + stringTemp);return stringTemp;}。