串口通信.docx
- 文档编号:26691368
- 上传时间:2023-06-21
- 格式:DOCX
- 页数:9
- 大小:151.59KB
串口通信.docx
《串口通信.docx》由会员分享,可在线阅读,更多相关《串口通信.docx(9页珍藏版)》请在冰豆网上搜索。
串口通信
一.实验目的及实验环境
<1>实验环境
Javaeclipse下
<2>实验目的
了解串行通信的背景知识后,通过三线制制作一条串口通信线(PC-PC),并编程实现两台PC间通过RS-232C通信。
要求两台PC机能进行实时的字符通信,并了解工业自动化控制中的基本通信方式。
2.实验内容
1、检查PC是否具有串行通信接口,并按其针脚类准备一条串口通信线缆。
2、串口包的安装,下载javacomm20-win32.zip并解压,将win32com.dll复制到
三、方案设计
1、将实验所需RS-232缆线准备好,并将JAVA串口包复制到相应地目录下。
2、查找有关串口通信的书籍以及在网上查找相应地串口通信代码。
3、用JAVA编程软件JCreator编写代码。
4.测试数据及运行结果
图一主界面
图二发送消息
图三接收消息
五.总结
1、实验过程中遇到的问题及解决办法;
串口包的安装配置比较难完成,最后在网上看各种博客和论坛,才将问题解决。
还有一些代码问题,最后找同学调试好了。
2、对设计及调试过程的心得体会。
通过本次串口实验,我对串口通信的知识了解的更透彻,这是在刚开始对串口通信知识不了解的情况下就编程而造成许多错误之后才得到的结果。
在网上查找资料的时候也接触到了不少其他的编程语言例如VB,delphi,C#等,这也让我对这些从没有学过的语言有所了解,我想这些知识对以后的实验工作都有帮助。
我也进一步发现了自己动手能力和自学能力都得到很多的进步,同时也对串口的发送与接收信息有了进一步的了解。
6.附录:
源代码
确认本机可以使用的串口:
packagetest;
importjava.util.Enumeration;
importjava.util.HashMap;
importm.CommPortIdentifier;
importm.SerialPort;
publicclassGetSerialPorts{
publicvoidlistPortChoices(){
CommPortIdentifierportId;
Enumerationen=CommPortIdentifier.getPortIdentifiers();
//iteratethroughtheports.
while(en.hasMoreElements()){
portId=(CommPortIdentifier)en.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName());
}
}
}
publicstaticvoidmain(String[]args){
GetSerialPortsGSP=newGetSerialPorts();
GSP.listPortChoices();
}
}
打开串口,关闭串口:
packagetest;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.util.Enumeration;
importjava.util.HashMap;
importm.CommPortIdentifier;
importm.PortInUseException;
importm.SerialPort;
importm.UnsupportedCommOperationException;
publicclassGetSerialPorts{
privateCommPortIdentifierportId;
privateSerialPorttestPort;
privateCommPortIdentifiermyPort;
privateInputStreamis;
privateOutputStreamos;
publicvoidlistPortChoices(){
Enumerationen=CommPortIdentifier.getPortIdentifiers();
//iteratethroughtheports.
while(en.hasMoreElements()){
portId=(CommPortIdentifier)en.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName());
}
myPort=portId;//任意取一个串口,比如com1
}
}
publicbooleanopenPort(){
try{
testPort=(SerialPort)myPort.open("COM1",500);//注意这里必须换成一个真实的串口
try{
this.testPort.setSerialPortParams(38400,SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,SerialPort.PARITY_EVEN);
}catch(UnsupportedCommOperationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
try{
this.testPort.enableReceiveTimeout(30);
}catch(UnsupportedCommOperationExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
this.testPort.setOutputBufferSize(1024);
this.testPort.setInputBufferSize(1024);
try{
this.is=this.testPort.getInputStream();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
try{
this.os=this.testPort.getOutputStream();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
this.testPort.notifyOnDataAvailable(true);
this.testPort.notifyOnOutputEmpty(true);
this.testPort.notifyOnBreakInterrupt(true);
//this.printerPort.addEventListener(newPrintPortListener(is));
System.out.println("打开com1机串口成功");
returntrue;
}catch(PortInUseExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnfalse;
}
}
/******TODO关闭端口*****/
publicbooleanclosePort(){
//TODOAuto-generatedmethodstub
try{
if(null!
=this.testPort){
is.close();
os.close();
this.testPort.close();
}
System.out.println("关闭COM1串口成功");
returntrue;
}catch(Exceptione){
//TODOAuto-generatedcatchblock
//e.printStackTrace();
System.out.println("关闭COM1串口失败");
returnfalse;
}
}
publicstaticvoidmain(String[]args){
GetSerialPortsGSP=newGetSerialPorts();
GSP.listPortChoices();
GSP.openPort();
}
}
读数据:
/******TODO接收端口數******/
publicStringreadData(InputStreamis){
//读取缓冲区域
byte[]readBuffer=newbyte[4096];
intreadDataLength=0;
try{
readDataLength=is.read(readBuffer);
//for(byteb:
readBuffer){
//System.out.print(b);
//}
//System.out.println();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
returnnull;
}
//将真实数据保存到零时数组中
byte[]readTemp=newbyte[readDataLength];
for(inti=0;i readTemp[i]=readBuffer[i]; } //将byte数组转换为16进制字符串 StringstringTemp=FeelTheBase.bytesToHexString(readTemp); //System.out.println("指令返回值"+stringTemp); returnstringTemp; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 串口 通信