为啥我写的串口监听器不能用呢? Java串口 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我自己写了一个监(为什么这是和谐词语)听器,想监(为什么这是和谐词语)听别的串口发给我的串口的数据,但是不起效果,为什么啊?代码如下:SerialDemoMain类,主类:package com.ue.serialDemo;import java.io.IOException;import java.io.OutputStream;import java.util.TooManyListenersException;import javax.comm.CommPortIdentifier;import javax.comm.NoSuchPortException;import javax.comm.PortInUseException;import javax.comm.SerialPort;public class SerialDemoMain { public CommPortIdentifier portId; public SerialPort serialPort ; /** * 打开串口 * @throws NoSuchPortException * @throws PortInUseException * @throws TooManyListenersException */ public void open() throws NoSuchPortException, PortInUseException, TooManyListenersException{ portId = CommPortIdentifier.getPortIdentifier("COM1"); serialPort = (SerialPort) portId.open("zgck", 600); serialPort.addEventListener(new MyPortListener(serialPort)); } public void sendMsg(String msg) throws IOException, InterruptedException{ /*往端口中写数据*/ OutputStream os = serialPort.getOutputStream(); if("1".equals(msg)){ os.write(0x3B); os.write(0x55); os.write(0x31); os.write(0x0D); Thread.sleep(1000); os.write(0x3B); os.write(0x44); os.write(0x31); os.write(0x0D); }else if("2".equals(msg)){ os.write(0x3B); os.write(0x55); os.write(0x32); os.write(0x0D); Thread.sleep(1000); os.write(0x3B); os.write(0x44); os.write(0x32); os.write(0x0D); }else if("3".equals(msg)){ os.write(0x3B); os.write(0x55); os.write(0x33); os.write(0x0D); Thread.sleep(1000); os.write(0x3B); os.write(0x44); os.write(0x33); os.write(0x0D); }else{ System.out.println("无法识别的数据:"+msg); } } public static void main(String[] args) throws NoSuchPortException, PortInUseException, TooManyListenersException { SerialDemoMain sm2 = new SerialDemoMain(); sm2.open(); } }MyPortListener类,监(为什么这是和谐词语)听器类:package com.ue.serialDemo;import java.io.IOException;import java.io.InputStream;import javax.comm.CommPort;import javax.comm.SerialPort;import javax.comm.SerialPortEvent;import javax.comm.SerialPortEventListener;public class MyPortListener implements SerialPortEventListener{ public SerialPort serialPort ; public MyPortListener(SerialPort serialPort ) { this.serialPort = serialPort; } @Override public void serialEvent(SerialPortEvent evt) { switch (evt.getEventType()) { case SerialPortEvent.CTS : System.out.println("CTS event occured."); break; case SerialPortEvent.CD : System.out.println("CD event occured."); break; case SerialPortEvent.BI : System.out.println("BI event occured."); break; case SerialPortEvent.DSR : System.out.println("DSR event occured."); break; case SerialPortEvent.FE : System.out.println("FE event occured."); break; case SerialPortEvent.OE : System.out.println("OE event occured."); break; case SerialPortEvent.PE : System.out.println("PE event occured."); break; case SerialPortEvent.RI : System.out.println("RI event occured."); break; case SerialPortEvent.OUTPUT_BUFFER_EMPTY : System.out.println("OUTPUT_BUFFER_EMPTY event occured."); break; case SerialPortEvent.DATA_AVAILABLE : System.out.println("DATA_AVAILABLE event occured."); int ch; StringBuffer buf = new StringBuffer(); try { InputStream input = serialPort.getInputStream(); while ( (ch=input.read()) > 0) { buf.append((char)ch); } System.out.print(buf); } catch (IOException e) {} break; } } }跪求高手告诉我错哪了? java 如何将数据库获得的数据显示在jtable中 初学者有一问题,在线等待。。。。 问一个Swing的问题!jTextArea 的问题. 请教一个困扰一下午的问题,组件重用? 构造对象的问题 一个简单的问题,送分大家 Jtable里同一单元格中的数据如何分2行显示? 将JDK换为1.4以后,出现的Jbuilder6.0的中文问题。请各位大侠不吝赐教! 初学java 搞不清问题在那 哪位高手用几句话说说接口是什么?小弟感激不尽! 希望JAVA厉害的能给讲下这个程序是怎么运行的 一个简单运算窗口,运行后无法输出值
SerialDemoMain类,主类:package com.ue.serialDemo;import java.io.IOException;
import java.io.OutputStream;
import java.util.TooManyListenersException;import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;public class SerialDemoMain {
public CommPortIdentifier portId;
public SerialPort serialPort ;
/**
* 打开串口
* @throws NoSuchPortException
* @throws PortInUseException
* @throws TooManyListenersException
*/
public void open() throws NoSuchPortException, PortInUseException, TooManyListenersException{
portId = CommPortIdentifier.getPortIdentifier("COM1");
serialPort = (SerialPort) portId.open("zgck", 600);
serialPort.addEventListener(new MyPortListener(serialPort));
}
public void sendMsg(String msg) throws IOException, InterruptedException{
/*往端口中写数据*/
OutputStream os = serialPort.getOutputStream();
if("1".equals(msg)){
os.write(0x3B);
os.write(0x55);
os.write(0x31);
os.write(0x0D);
Thread.sleep(1000);
os.write(0x3B);
os.write(0x44);
os.write(0x31);
os.write(0x0D);
}else if("2".equals(msg)){
os.write(0x3B);
os.write(0x55);
os.write(0x32);
os.write(0x0D);
Thread.sleep(1000);
os.write(0x3B);
os.write(0x44);
os.write(0x32);
os.write(0x0D);
}else if("3".equals(msg)){
os.write(0x3B);
os.write(0x55);
os.write(0x33);
os.write(0x0D);
Thread.sleep(1000);
os.write(0x3B);
os.write(0x44);
os.write(0x33);
os.write(0x0D);
}else{
System.out.println("无法识别的数据:"+msg);
}
}
public static void main(String[] args) throws NoSuchPortException, PortInUseException, TooManyListenersException {
SerialDemoMain sm2 = new SerialDemoMain();
sm2.open();
}
}
MyPortListener类,监(为什么这是和谐词语)听器类:package com.ue.serialDemo;import java.io.IOException;
import java.io.InputStream;import javax.comm.CommPort;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;public class MyPortListener implements SerialPortEventListener{
public SerialPort serialPort ;
public MyPortListener(SerialPort serialPort ) {
this.serialPort = serialPort;
}
@Override
public void serialEvent(SerialPortEvent evt)
{
switch (evt.getEventType())
{
case SerialPortEvent.CTS :
System.out.println("CTS event occured.");
break;
case SerialPortEvent.CD :
System.out.println("CD event occured.");
break;
case SerialPortEvent.BI :
System.out.println("BI event occured.");
break;
case SerialPortEvent.DSR :
System.out.println("DSR event occured.");
break;
case SerialPortEvent.FE :
System.out.println("FE event occured.");
break;
case SerialPortEvent.OE :
System.out.println("OE event occured.");
break;
case SerialPortEvent.PE :
System.out.println("PE event occured.");
break;
case SerialPortEvent.RI :
System.out.println("RI event occured.");
break;
case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
System.out.println("OUTPUT_BUFFER_EMPTY event occured.");
break;
case SerialPortEvent.DATA_AVAILABLE :
System.out.println("DATA_AVAILABLE event occured.");
int ch;
StringBuffer buf = new StringBuffer();
try {
InputStream input = serialPort.getInputStream();
while ( (ch=input.read()) > 0) {
buf.append((char)ch);
}
System.out.print(buf);
} catch (IOException e) {}
break;
}
}
}跪求高手告诉我错哪了?