在网上COPY了两个类,一个通过COM写入数据,一个监听COM,打印出接收的数据。
问题是,我在开启监听类的时候,是要占用COM3端口的。
那么在同时执行写入的类,COM3端口已被占用,程序报错了。
报错代码:outputStream = serialPort.getOutputStream(); 同样,我先执行那个入数据的类,再开启监听类,也是报错。感觉被占用了。
串口不是上行下行可以同时写入输出的吗?该如何调试?import java.awt.Button;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;class R_Frame extends Frame implements Runnable,ActionListener,SerialPortEventListener
{
/* 检测系统中可用的通讯端口类 */
static CommPortIdentifier portId;
/* Enumeration 为枚举型类,在java.util中 */
static Enumeration portList;
InputStream inputStream;
/* 声明RS-232串行端口的成员变量 */
SerialPort serialPort;
Thread readThread;
String str="";
TextField out_message=new TextField("上面文本框显示接收到的数据");
TextArea in_message=new TextArea();
Button btnOpen=new Button("打开串口"); /*建立窗体*/
R_Frame()
{
super("串口接收数据");
setSize(200,200);
setVisible(true);
btnOpen.addActionListener(this);
add(out_message,"South");
add(in_message,"Center");
add(btnOpen,"North");
} //R_Frame() end /*点击按扭所触发的事件:打开串口,并监听串口. */
public void actionPerformed(ActionEvent event)
{
/*获取系统中所有的通讯端口 */
portList=CommPortIdentifier.getPortIdentifiers();
/* 用循环结构找出串口 */
while (portList.hasMoreElements()){
/*强制转换为通讯端口类型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals("COM3")) {
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
out_message.setText("已打开端口COM1 ,正在接收数据..... ");
}
catch (PortInUseException e) { } /*设置串口监听器*/
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) { }
/* 侦听到串口有数据,触发串口事件*/
serialPort.notifyOnDataAvailable(true);
} //if end
} //if end
} //while end
readThread = new Thread(this);
readThread.start();//线程负责每接收一次数据休眠20秒钟
} //actionPerformed() end
/*接收数据后休眠20秒钟*/
public void run() {
try {
Thread.sleep(20000);
}
catch (InterruptedException e) { }
} //run() end /*串口监听器触发的事件,设置串口通讯参数,读取数据并写到文本区中*/
public void serialEvent(SerialPortEvent event) {
/*设置串口通讯参数:波特率、数据位、停止位、奇偶校验*/
try {
serialPort.setSerialPortParams(19200,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);
/*接收到的数据存放到文本区中*/
in_message.append(str+"\n");
}
catch (IOException e) { }
} //serialEvent() end
} //类R_Frame endpublic class ReadComm
{
public static void main(String args[])
{
/* 实例化接收串口数据的窗体类 */
R_Frame R_win=new R_Frame();
/* 定义窗体适配器的关闭按钮功能 */
R_win.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0); }
});
R_win.pack();
}
}[code=Java]
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;class S_Frame extends Frame implements Runnable,ActionListener
{
/*检测系统中可用的通讯端口类 */
static CommPortIdentifier portId;
/*Enumeration 为枚举型类,在util中 */
static Enumeration portList;
OutputStream outputStream;
/*RS-232的串行口 */
SerialPort serialPort;
Thread readThread;
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 byte[10240];
/*设置判断要是否关闭串口的标志*/
boolean ; /*安排窗体*/
S_Frame()
{ super("串口发送数据");
setSize(200,200);
setVisible(true);
add(out_message,"Center");
add(p,"North");
p.add(btnOpen);
p.add(btnClose);
add(in_message,"South");
btnOpen.addActionListener(this);
btnClose.addActionListener(this);
} //R_Frame() end /*点击按扭打开串口.*/
public void actionPerformed(ActionEvent event) {
if (event.getSource()==btnClose){
serialPort.close();//关闭串口
=true; //用于中止线程的run()方法
in_message.setText("串口COM1已经关闭,停止发送数据.");
}
else { =false;
/*从文本区按字节读取数据*/
data=out_message.getText().getBytes();
/*打开串口*/
start();
in_message.setText("串口COM1已经打开,正在每2秒钟发送一次数据.....");
}
} //actionPerformed() end /*打开串口,并调用线程发送数据*/
public void start(){
/*获取系统中所有的通讯端口 */
portList=CommPortIdentifier.getPortIdentifiers();
/* 用循环结构找出串口 */
while (portList.hasMoreElements()){
/*强制转换为通讯端口类型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals("COM3")) {
/*打开串口 */
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
}
catch (PortInUseException e) { }
/*设置串口输出流*/
try {
outputStream = serialPort.getOutputStream();
}
catch (IOException e) {}
} //if end
} //if end
} //while end
/*调用线程发送数据*/
try{
readThread = new Thread(this);
//线程负责每发送一次数据,休眠2秒钟
readThread.start();
}
catch (Exception e) { }
} //start() end
/*发送数据,休眠2秒钟后重发*/
public void run() {
/*设置串口通讯参数*/
try {
serialPort.setSerialPortParams(19200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) { }
/*发送数据流(将数组data[]中的数据发送出去)*/
try {
outputStream.write(data);
}
catch (IOException e) { }
/*发送数据后休眠2秒钟,然后再重发*/
try { Thread.sleep(2000);
if ()
{return; //结束run方法,导致线程死亡
}
start();
}
catch (InterruptedException e) { }
} //run() end
} //类S_Frame endpublic class SendComm
{public 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();
}
}
问题是,我在开启监听类的时候,是要占用COM3端口的。
那么在同时执行写入的类,COM3端口已被占用,程序报错了。
报错代码:outputStream = serialPort.getOutputStream(); 同样,我先执行那个入数据的类,再开启监听类,也是报错。感觉被占用了。
串口不是上行下行可以同时写入输出的吗?该如何调试?import java.awt.Button;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;class R_Frame extends Frame implements Runnable,ActionListener,SerialPortEventListener
{
/* 检测系统中可用的通讯端口类 */
static CommPortIdentifier portId;
/* Enumeration 为枚举型类,在java.util中 */
static Enumeration portList;
InputStream inputStream;
/* 声明RS-232串行端口的成员变量 */
SerialPort serialPort;
Thread readThread;
String str="";
TextField out_message=new TextField("上面文本框显示接收到的数据");
TextArea in_message=new TextArea();
Button btnOpen=new Button("打开串口"); /*建立窗体*/
R_Frame()
{
super("串口接收数据");
setSize(200,200);
setVisible(true);
btnOpen.addActionListener(this);
add(out_message,"South");
add(in_message,"Center");
add(btnOpen,"North");
} //R_Frame() end /*点击按扭所触发的事件:打开串口,并监听串口. */
public void actionPerformed(ActionEvent event)
{
/*获取系统中所有的通讯端口 */
portList=CommPortIdentifier.getPortIdentifiers();
/* 用循环结构找出串口 */
while (portList.hasMoreElements()){
/*强制转换为通讯端口类型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals("COM3")) {
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
out_message.setText("已打开端口COM1 ,正在接收数据..... ");
}
catch (PortInUseException e) { } /*设置串口监听器*/
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) { }
/* 侦听到串口有数据,触发串口事件*/
serialPort.notifyOnDataAvailable(true);
} //if end
} //if end
} //while end
readThread = new Thread(this);
readThread.start();//线程负责每接收一次数据休眠20秒钟
} //actionPerformed() end
/*接收数据后休眠20秒钟*/
public void run() {
try {
Thread.sleep(20000);
}
catch (InterruptedException e) { }
} //run() end /*串口监听器触发的事件,设置串口通讯参数,读取数据并写到文本区中*/
public void serialEvent(SerialPortEvent event) {
/*设置串口通讯参数:波特率、数据位、停止位、奇偶校验*/
try {
serialPort.setSerialPortParams(19200,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);
/*接收到的数据存放到文本区中*/
in_message.append(str+"\n");
}
catch (IOException e) { }
} //serialEvent() end
} //类R_Frame endpublic class ReadComm
{
public static void main(String args[])
{
/* 实例化接收串口数据的窗体类 */
R_Frame R_win=new R_Frame();
/* 定义窗体适配器的关闭按钮功能 */
R_win.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0); }
});
R_win.pack();
}
}[code=Java]
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;class S_Frame extends Frame implements Runnable,ActionListener
{
/*检测系统中可用的通讯端口类 */
static CommPortIdentifier portId;
/*Enumeration 为枚举型类,在util中 */
static Enumeration portList;
OutputStream outputStream;
/*RS-232的串行口 */
SerialPort serialPort;
Thread readThread;
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 byte[10240];
/*设置判断要是否关闭串口的标志*/
boolean ; /*安排窗体*/
S_Frame()
{ super("串口发送数据");
setSize(200,200);
setVisible(true);
add(out_message,"Center");
add(p,"North");
p.add(btnOpen);
p.add(btnClose);
add(in_message,"South");
btnOpen.addActionListener(this);
btnClose.addActionListener(this);
} //R_Frame() end /*点击按扭打开串口.*/
public void actionPerformed(ActionEvent event) {
if (event.getSource()==btnClose){
serialPort.close();//关闭串口
=true; //用于中止线程的run()方法
in_message.setText("串口COM1已经关闭,停止发送数据.");
}
else { =false;
/*从文本区按字节读取数据*/
data=out_message.getText().getBytes();
/*打开串口*/
start();
in_message.setText("串口COM1已经打开,正在每2秒钟发送一次数据.....");
}
} //actionPerformed() end /*打开串口,并调用线程发送数据*/
public void start(){
/*获取系统中所有的通讯端口 */
portList=CommPortIdentifier.getPortIdentifiers();
/* 用循环结构找出串口 */
while (portList.hasMoreElements()){
/*强制转换为通讯端口类型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals("COM3")) {
/*打开串口 */
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
}
catch (PortInUseException e) { }
/*设置串口输出流*/
try {
outputStream = serialPort.getOutputStream();
}
catch (IOException e) {}
} //if end
} //if end
} //while end
/*调用线程发送数据*/
try{
readThread = new Thread(this);
//线程负责每发送一次数据,休眠2秒钟
readThread.start();
}
catch (Exception e) { }
} //start() end
/*发送数据,休眠2秒钟后重发*/
public void run() {
/*设置串口通讯参数*/
try {
serialPort.setSerialPortParams(19200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) { }
/*发送数据流(将数组data[]中的数据发送出去)*/
try {
outputStream.write(data);
}
catch (IOException e) { }
/*发送数据后休眠2秒钟,然后再重发*/
try { Thread.sleep(2000);
if ()
{return; //结束run方法,导致线程死亡
}
start();
}
catch (InterruptedException e) { }
} //run() end
} //类S_Frame endpublic class SendComm
{public 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();
}
}
portList=CommPortIdentifier.getPortIdentifiers();
/* 用循环结构找出串口 */
while (portList.hasMoreElements()){
/*强制转换为通讯端口类型*/
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals("COM3")) {
/*打开串口 */
try {
serialPort = (SerialPort) portId.open("ReadComm", 2000);
}
catch (PortInUseException e) { }这一段应该单独拿出来,或者共用
我自己也写了串口收发的程序,没有问题的
其实COM口应该是一个发,一个接受.用串口线把两台服务器连起来,程序就可以跑了.如果在单机开发中,需要使用串口进行收发调试,可以下载串口模拟工具,在PC硬件里新增串口,配置一下就可以了