⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readcomm.java

📁 linux下的串口编程,清注意端口的差别。
💻 JAVA
字号:
/****************************************** * 程序文件名称:ReadComm.java * 功能:从串行口COM1中接收数据 ******************************************/import java.awt.event.*;import java.io.*;import java.util.*;import javax.comm.*;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;class R_Frame extends JFrame implements Runnable, ActionListener,		SerialPortEventListener {	/* 检测系统中可用的通讯端口类 */	static CommPortIdentifier portId;	/* Enumeration 为枚举型类,在java.util中 */	static Enumeration portList;	InputStream inputStream;	/* 声明RS-232串行端口的成员变量 */	SerialPort serialPort;	/** 读取串口线程 */	Thread readThread;	/** 写串口线程 */	Thread writeThread;		JPanel panOut = new JPanel();	JPanel panIn = new JPanel();	JPanel panBtn = new JPanel();		String str = "";	JTextField txtInmessage = new JTextField("上面文本框显示接收到的数据");	JTextArea txtArea_In = new JTextArea();	JTextField txtOutmessage = new JTextField("上面文本框显示发送的数据");	JTextArea txtArea_Out = new JTextArea();	JButton btnOpen = new JButton("打开串口");	JButton btnClose = new JButton("关闭串口");	JButton btnSend = new JButton("发送数据");	//发送数据变量	byte data[] = new byte[10240];	OutputStream outputStream;	/* 建立窗体 */	R_Frame() {		this.getContentPane().setLayout(null);				panBtn.setLayout(null);		panBtn.setBounds(0,0,300,30);		panBtn.add(btnOpen);		panBtn.add(btnClose);		panBtn.add(btnSend);		btnOpen.setBounds(0, 0, 100, 30);		btnClose.setBounds(100, 0, 100, 30);		btnSend.setBounds(200, 0, 100, 30);		this.getContentPane().add(panBtn);						btnOpen.addActionListener(this);		btnClose.addActionListener(this);		btnSend.addActionListener(this);				//发送区域		panOut.setLayout(null);		panOut.setBounds(0,30,300,200);		txtArea_Out.setBounds(0,0,300,180);		txtOutmessage.setBounds(0,180,300,20);		panOut.add(txtArea_Out);		panOut.add(txtOutmessage);				this.getLayeredPane().add(panOut);		//接受区域		panIn.setLayout(null);		panIn.setBounds(0,230,300,200);		txtArea_In.setBounds(0,0,300,180);		txtInmessage.setBounds(0,180,300,20);		panIn.add(txtArea_In);		panIn.add(txtInmessage);				this.getLayeredPane().add(panIn);			} // R_Frame() end	/* 点击按扭所触发的事件:打开串口,并监听串口. */	public void actionPerformed(ActionEvent event) {		if(event.getSource().equals(btnOpen)) {			//读取用户配置端口名称			String portName = ConfigFile.findProperty("CommPort");			/* 获取系统中所有的通讯端口 */			portList = CommPortIdentifier.getPortIdentifiers();			/* 用循环结构找出串口 */			while (portList.hasMoreElements()) {				/* 强制转换为通讯端口类型 */				portId = (CommPortIdentifier) portList.nextElement();				if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {					if (portId.getName().equals(portName)) {						try {							serialPort = (SerialPort) portId.open("ReadComm", 2000);							txtInmessage.setText("已打开端口COM1 ,正在接收数据..... ");						} catch (PortInUseException e) {						}						/* 设置串口监听器 */						try {							serialPort.addEventListener(this);						} catch (TooManyListenersException e) {						}						/* 侦听到串口有数据,触发串口事件 */						serialPort.notifyOnDataAvailable(true);												/* 设置串口输出流 */						try {							outputStream = serialPort.getOutputStream();						} catch (IOException e) {						}					} // if end				} // if end			} // while end			readThread = new Thread(this);			readThread.start(); // 线程负责每接收一次数据休眠20秒钟					}		if(event.getSource().equals(btnClose)){						if(serialPort!=null) {				serialPort.close();				txtInmessage.setText("端口已关闭");			}		}		if(event.getSource().equals(btnSend)){			/* 设置串口通讯参数 */			try {				serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,						SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);			} catch (UnsupportedCommOperationException e) {			}			/* 从文本区按字节读取数据 */			data = txtArea_Out.getText().getBytes();			/* 发送数据流(将数组data[]中的数据发送出去) */			try {				outputStream.write(data);			} catch (IOException e) {			}		}			} // actionPerformed() end	/* 接收数据后休眠20秒钟 */	public void run() {		try {			Thread.sleep(20000);		} catch (InterruptedException e) {		}	} // run() end	/* 串口监听器触发的事件,设置串口通讯参数,读取数据并写到文本区中 */	public void serialEvent(SerialPortEvent event) {		/* 设置串口通讯参数:波特率、数据位、停止位、奇偶校验 */		try {			serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,					SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);		} catch (UnsupportedCommOperationException e) {		}		byte[] readBuffer = new byte[20];		try {			inputStream = serialPort.getInputStream();		} catch (IOException e) {		}		try {			/* 从线路上读取数据流 */			while (inputStream.available() > 0) {				int numBytes = inputStream.read(readBuffer);			} // while end			str = new String(readBuffer);			/* 接收到的数据存放到文本区中 */			txtArea_In.append(str + "\n");		} catch (IOException e) {		}	} // serialEvent() end} // 类R_Frame endpublic class ReadComm {	public static void main(String args[]) {		R_Frame frame = new R_Frame();		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		frame.setResizable(false); //设置窗口为固定大小		frame.setSize(300,460);  		frame.setLocation(200, 200);		frame.setVisible(true);			}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -