一些关于串口通讯的问题,希望有人帮我解答 怎么可能读不到数据呢?你试试comm包的那些例子程序好了,我就是参考那些例子来做的,程序都是没有问题的. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;import java.util.*;import javax.comm.*;public class Dial_up implements SerialPortEventListener{ static Enumeration portList; static CommPortIdentifier portId; static String messageString = "Hello, world!\n"; static SerialPort serialPort; static OutputStream outputStream; static InputStream inputStream; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM1")) { //if (portId.getName().equals("/dev/term/a")) { try { serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000); } catch (PortInUseException e) {} try { outputStream = serialPort.getOutputStream(); } catch (IOException e) {} try { inputStream = serialPort.getInputStream(); } catch (IOException e) {} try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {} try { serialPort.addEventListener(new SerialListener1()); } catch (TooManyListenersException e) {} serialPort.notifyOnDataAvailable(true); try { outputStream.write(messageString.getBytes()); } catch (IOException e) {} } }//end of if COM1 }//end of while }//end of main public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer = new byte[20]; try { while (inputStream.available() > 0) { int numBytes = inputStream.read(readBuffer); } System.out.print(new String(readBuffer)); } catch (IOException e) {} break; } }}//end of class 请教exec调用cmd的问题 java主类方法中的 static 关键字 Spider读取网页最快的方法 Cacheable接口是干嘛用的? 用Winsock抓Java写的服务端的程序发送和接收的包,应该监视哪个进程??? 如何使自己的程序支持插件 mysql中文问题?? 各位大虾,请问通过Java怎么去访问串口呢??? HSSF 中文问题 请问JRun是个什么东东?在哪里有下载? 大家好,今天做游戏,遇到JAVA的编程问题,不懂不懂,请大家帮我! 怎样判断输入的字符串是否是“YYYY-MM-DD”格式的?
import java.util.*;
import javax.comm.*;public class Dial_up implements SerialPortEventListener{
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream; public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {} try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {} try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {} try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {} try {
serialPort.addEventListener(new SerialListener1());
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true); try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
}
}//end of if COM1
}//end of while
}//end of main public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20]; try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}//end of class