ImageVerifierCode 换一换
格式:DOCX , 页数:8 ,大小:16.81KB ,
资源ID:19910570      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/19910570.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java串口通信程序程序及注释 可编译运行Word格式.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java串口通信程序程序及注释 可编译运行Word格式.docx

1、import java.awt.event.*;import java.io.*;import java.util.*; class S_Frame extends Frame implements Runnable,ActionListener/*检测系统中可用的通讯端口类*/static CommPortIdentifier portId;/*Enumeration为枚举型类,在util中*/static Enumeration portList;OutputStream outputStream;/*RS-232的串行口*/SerialPort serialPort;Thread rea

2、dThread;Panel p=new Panel();TextField in_message=new TextField(打开COM1,波特率9600,数据位8,停止位1.);TextArea out_message=new TextArea();Button btnOpen=new Button(打开串口,发送数据Button btnClose=new Button(关闭串口,停止发送数据byte data=new byte10240;/*设置判断要是否关闭串口的标志*/boolean mark;/*安排窗体*/S_Frame() super(串口发送数据setSize(200,200)

3、;setVisible(true);add(out_message,Centeradd(p,Northp.add(btnOpen);p.add(btnClose);add(in_message,SouthbtnOpen.addActionListener(this);btnClose.addActionListener(this); /R_Frame() end/*点击按扭打开串口.*/public void actionPerformed(ActionEvent event) if (event.getSource()=btnClose)serialPort.close(); /关闭串口ma

4、rk=true; /用于中止线程的run()方法in_message.setText(串口COM1已经关闭,停止发送数据.else mark=false;/*从文本区按字节读取数据*/data=out_message.getText().getBytes();/*打开串口*/start();串口COM1已经打开,正在每2秒钟发送一次数据. /actionPerformed() end/*打开串口,并调用线程发送数据*/public void start()/*获取系统中所有的通讯端口*/portList=CommPortIdentifier.getPortIdentifiers();/*用循环

5、结构找出串口*/while (portList.hasMoreElements()/*强制转换为通讯端口类型*/portId=(CommPortIdentifier)portList.nextElement();if(portId.getPortType() = CommPortIdentifier.PORT_SERIAL)if (portId.getName().equals(COM1) try serialPort = (SerialPort) portId.open(ReadComm, 2000);catch (PortInUseException e) /*设置串口输出流*/outpu

6、tStream = serialPort.getOutputStream();catch (IOException e) /if end /while end/*调用线程发送数据*/tryreadThread = new Thread(this);/线程负责每发送一次数据,休眠2秒钟readThread.start();catch (Exception e) /start() end/*发送数据,休眠2秒钟后重发*/public void run() /*设置串口通讯参数*/serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,Se

7、rialPort.STOPBITS_1,SerialPort.PARITY_NONE);catch (UnsupportedCommOperationException e) /*发送数据流(将数组data中的数据发送出去)*/outputStream.write(data);catch (IOException e) /*发送数据后休眠2秒钟,然后再重发*/try Thread.sleep(2000);if (mark)return; /结束run方法,导致线程死亡start();catch (InterruptedException e) /run() end /类S_Frame endp

8、ublic class SendCommpublic static void main(String args) S_Frame S_win=new S_Frame();S_win.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0); );S_win.pack();接收程序: class R_Frame extends Frame implementsRunnable,ActionListener,SerialPortEventListener/*检测系统中可用的

9、通讯端口类*/* Enumeration为枚举型类,在java.util中*/InputStream inputStream;/*声明RS-232串行端口的成员变量*/String str=;TextField out_message=new TextField(上面文本框显示接收到的数据TextArea in_message=new TextArea();打开串口/*建立窗体*/R_Frame()super(串口接收数据add(btnOpen,/*点击按扭所触发的事件:打开串口,并监听串口. */public void actionPerformed(ActionEvent event)/*

10、获取系统中所有的通讯端口*/out_message.setText(已打开端口COM1 ,正在接收数据. /*设置串口监听器*/serialPort.addEventListener(this);catch (TooManyListenersException e) /*侦听到串口有数据,触发串口事件*/serialPort.notifyOnDataAvailable(true); /线程负责每接收一次数据休眠20秒钟/*接收数据后休眠20秒钟*/Thread.sleep(200);/*串口监听器触发的事件,设置串口通讯参数,读取数据并写到文本区中*/public void serialEve

11、nt(SerialPortEvent event) /*设置串口通讯参数:波特率、数据位、停止位、奇偶校验*/tryserialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);catch (UnsupportedCommOperationException e) byte readBuffer = new byte20;inputStream = serialPort.getInputStream();try /*从线路上读取数据流*/while

12、(inputStream.available() 0) int numBytes = inputStream.read(readBuffer);str=new String(readBuffer);/*接收到的数据存放到文本区中*/in_message.append(str+n /serialEvent() end /类R_Frame endpublic class ReadCommpublic static void main(String args)/*实例化接收串口数据的窗体类*/R_Frame R_win=new R_Frame();/*定义窗体适配器的关闭按钮功能*/R_win.addWindowListener(new WindowAdapter()R_win.pack();

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1