outputStream.write()出错?

dyufei 2010-08-26 06:00:43

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SerialWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;

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("COM7")) {
try {
serialPort = (SerialPort) portId.open("serialReadApp",
2000);
} catch (PortInUseException e) {
}

}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
System.out.print("outputStream failed!"
+ "\n");
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
try {

outputStream.write(messageString.getBytes());

} catch (IOException e) {

}
}
}
}


outputStream.write(messageString.getBytes()); 向串口写数据时候出错,
java.io.IOException: write error
Error 31 while waiting for write to complete

1、这是为什么啊?怎么解决呢?

2、另外 虚拟串口如何调试啊? 我再写一个度串口的程序 ,运行的时候因为写的程序已将串口打开度的只能从另外一个串口读,如何将两个虚拟串口联系在一起呢?
...全文
1095 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianshi1017 2010-08-27
  • 打赏
  • 举报
回复
static OutputStream outputStream;
为啥要是静态的
dyufei 2010-08-27
  • 打赏
  • 举报
回复
貌似和端口有关 如果是COM1不会出错 而改成虚拟端口则出错
aafdfafqwerqwru 2010-08-27
  • 打赏
  • 举报
回复
飘过,.......你找几个专家加好友,直接问他们
dyufei 2010-08-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 tianshi1017 的回复:]

static OutputStream outputStream;
为啥要是静态的
[/Quote]

因为 outputStream = serialPort.getOutputStream(); 中的getOutputStream()是静态的,如果非静态的报错
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot make a static reference to the non-static field outputStream

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧