java编写通讯串口的问题 谁有java通讯串口的例子请发个我,谁的例子能用直接给分直接结贴!目前手头试了2个程序都有问题,所以希望看下成功的例子!1,打开串口2,初始化串口3,读写串口4,关闭串口用到的是javax.comm包,或有其他的也可! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我毕业设计中的一段代码,向串口中写数据import java.io.*;import java.util.*;import javax.comm.*;/** * * @author Administrator */public class CommWrite extends Thread{ private static Enumeration portList; private static CommPortIdentifier portId; private static SerialPort serialPort; private String messageString; private OutputStream outputStream; private String comName; public static void main(String[] args){ new CommWrite("COM2","你好"); } public CommWrite(String comName,String message){ this.comName = comName; messageString = message; this.start(); } public void run(){ write(messageString); } private void write(String str) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals(this.comName)) { try { serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000); } catch (PortInUseException e) {} try { outputStream = serialPort.getOutputStream(); } catch (IOException e) {} try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {} try { outputStream.write(str.getBytes()); serialPort.close(); } catch (IOException e) {} } } } }} while(portList.hasMoreElements())这一条前面我加了System.out.print(portList.hasMoreElements());打出来居然是false,可是我电脑上明明有2个串口都是好的。奇怪! bayougeng OK 3Q了 ,数据出来了!结贴! 哇,真的那么灵?哈哈!说实话,在google上和baidu上,我真没看到很好的关于串口通讯的例子。大多数都是按照官方文档翻译过来的。很容易出问题。我blog里的方法,确实还是蛮实用的。 问个Swing 皮肤的问题 请解释下 Thread.init()这个方法吧 请问一般来说 什么时候用 主要的用处又是什么? 奇怪的数据 请问,怎么才能把JPanle添加倒JFrame??急用请哪位大侠给点代码好吗? window.open()自身覆盖自身页面 到底哪有可编译的java.c java中读写文件为何是乱码?请大家看看 java 编译初级问题,望大家多多帮忙 紧急求救,急阿! 如何在java中按一下按钮便弹出一个IE浏览窗口?谢谢! 懂jni的帮帮忙,遇到难题了.. 如何判断输入的日期格式正确?
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* @author Administrator
*/
public class CommWrite extends Thread{
private static Enumeration portList;
private static CommPortIdentifier portId;
private static SerialPort serialPort;
private String messageString;
private OutputStream outputStream;
private String comName;
public static void main(String[] args){
new CommWrite("COM2","你好");
}
public CommWrite(String comName,String message){
this.comName = comName;
messageString = message;
this.start();
}
public void run(){
write(messageString);
}
private void write(String str) {
portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(this.comName)) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(str.getBytes());
serialPort.close();
} catch (IOException e) {}
}
}
}
}
}
System.out.print(portList.hasMoreElements());
打出来居然是false,可是我电脑上明明有2个串口都是好的。
奇怪!
说实话,在google上和baidu上,我真没看到很好的关于串口通讯的例子。
大多数都是按照官方文档翻译过来的。很容易出问题。
我blog里的方法,确实还是蛮实用的。