串口堵塞问题,通讯采用com.sun.comm...,一台机大量发送数据,另一台机接收到一定时候,就回复出错?

linvoc 2007-04-07 01:09:58
我用串口API写了一个通讯程序,用于测试java串口的性能,其中A机只管发,同时也会接收B机的回复;B机只管收,收到一条消息后,打印出来,并给A机回复一个“ack”。现在问题是A机发送没问题,就算B机程序没运行也没关系,B机运行大约3分钟后,就出现outputstream.write()出错,然后Listenevent就不会再接收到消息了!

为了更快的接收A机传来的数据,B程序采用两线程,一个线程起监听事件addEventListener,专门接收消息,并且送入一个堆栈。第二个线程查询堆栈,有消息,就pop出来,再给A机传一个“ack”。

一开始运行挺稳定的,但运行几分钟B程序就停止运行了。为了测试强度,我将A程序每隔100毫秒就送一条消息过来。
如果我采用B反馈一条,A再发送一条,两个程序将会运行非常稳定的。现在关键问题就是,我如何解决B outputstream.write()出错后,能够再次接收到消息,并且执行一定的处理。

...全文
384 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
linvoc 2007-04-07
  • 打赏
  • 举报
回复
没有人对串口了解吗?
linvoc 2007-04-07
  • 打赏
  • 举报
回复
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: {
writeLog("Data output buffer empty");
break;
}
case SerialPortEvent.DATA_AVAILABLE:
byte[] param = new byte[8];

try {
int neddata = inputStream.read(param);

System.arraycopy(param, 0, params, i * 8, neddata);

try {
String paramstring = new String(params, encode);

paramstring = paramstring.trim();
writeLog("接收到的消息指令:" + paramstring);
if (i >= 14) {
System.out.println("参数解析错误!");
writeLog("参数解析错误!" + paramstring);
System.out.println(paramstring);
params = new byte[120];
i = 0;
sendOkFlag = true;
break;
}

if (paramstring.trim().indexOf("ack") != -1) {
writeLog("接收到ack回复!");
params = new byte[120];
i = 0;
sendOkFlag = true;
break;
}

if (paramstring.indexOf("#") != -1) {

System.out.println("接受:" + paramstring);
writeLog("接受到某一个指令:" + paramstring);
String[] paramarray = paramstring.trim().split("@");



if (action) {
sendMessage("ack");
}
else {
writeLog("指令接收成功,但处理失败!");
}
params = new byte[120];
i = 0;
// send messsage
sendMessage("ack");
break;
}

}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
error = e.getMessage();
writeLog(error);
propertySet();
//ex.printStackTrace();
}

i++;
}
catch (IOException ex) {
error = ex.getMessage();
writeLog(error);
propertySet();
//ex.printStackTrace();
return;
}

break;
}
}


/**
* 接到消息后发送确认字段,使用"ack"为成功标志符
*/
public void sendMessage(String message) {
try {
//System.out.println("发送:"+message);
outputStream = serialPort.getOutputStream();
outputStream.write(message.getBytes(encode));

}
catch (IOException e) {
error = e.getMessage();
writeLog(error);
propertySet();
e.printStackTrace();
}
}

public void dbInitialize() {

// 登记JDBC驱动对象

}



public static void propertySet() {


}
}

就是在接收到消息后,sendmessage("ack"),最容易出错。
发送进程持续发送数据,每隔100毫秒就送一次,接收进程一收到,就处理,然后就发给发送进程一个'ack"。

一旦接收进程出错,就无法再接收到消息了,程序就死在那里。只能重启,一重启程序,就又能接收到发送进程的消息(发送进程很奇怪,他怎么就死不了!)


linvoc 2007-04-07
  • 打赏
  • 举报
回复
我把程序贴出来,大家帮我看看:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.TooManyListenersException;
import java.util.*;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import java.sql.*;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.util.EncodingUtil;

public class ManageInterface
implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;

static Enumeration portList;

InputStream inputStream;

SerialPort serialPort;

Thread readThread;


OutputStream outputStream;

byte[] params = new byte[120];

int i = 0;

// 请求url
public static Properties p;

// 发送成功标志
static boolean sendOkFlag;



public String encode = "GB2312"; // 参数编码

public static StringBuffer errorRow = new StringBuffer();

public static String error = "";
/**
* 以下代码用于日志使用
*/
public String strLogFile; //日志文件名
File file = null;
FileOutputStream fileOut = null;
DataOutputStream out = null;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

System.out.println("Initialize parameters...");
p = new Properties();
try {
p.load(new FileInputStream("interface.properties"));
}
catch (IOException ex) {
error = ex.getMessage();
propertySet();
//ex.printStackTrace();
return;
}
nSleepTime = Integer.valueOf(p.getProperty("SleepTime")).intValue();

nCurrentID = Integer.parseInt(p.getProperty("CurrentID"));
errorRow.append(String.valueOf(p.getProperty("ErrorRow")));

DBserver = p.getProperty("DBServer");
DBuser = p.getProperty("DBUser");
DBpassword = p.getProperty("DBPassword");
sendOkFlag = false;

requestURL = p.getProperty("ServerAddr");
requestURL = "http://" + requestURL;

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//if (portId.getName().equals("/dev/ttyS0")) {
if (portId.getName().equals("COM1")) {
ManageInterface reader = new ManageInterface();
}
}
}
}

public String getCurrentTime() {
String rtn = "";
GregorianCalendar todaysDate = new GregorianCalendar();

int year = todaysDate.get(Calendar.YEAR);
int month = todaysDate.get(Calendar.MONTH) + 1;
int day = todaysDate.get(Calendar.DAY_OF_MONTH);
int hour = todaysDate.get(Calendar.HOUR_OF_DAY);
int min = todaysDate.get(Calendar.MINUTE);
int sec = todaysDate.get(Calendar.SECOND);
return "" + year + "-" + month + "-" + day + " " + hour + ":" + min +
":" +
sec;
}

/**
* 写入log日志
* @param strLog
*/
public void writeLog(String strLog) {
try {
System.out.println(this.getCurrentTime() + ":" + strLog);
out.writeUTF(this.getCurrentTime() + ":" + strLog);
out.flush();

}
catch (IOException ex) {
ex.printStackTrace();
return;
}

}

/**
* 构造函数,启动工作
*/
public ManageInterface() {

/**
* 启动配置文件
*/
this.strLogFile = "log" + File.separator + System.currentTimeMillis() +
".log";

try {
file = new File(this.strLogFile);
fileOut = new FileOutputStream(file, true);
out = new DataOutputStream(fileOut);
}
catch (IOException e) {
e.printStackTrace();
}

try {
serialPort = (SerialPort) portId.open("ManageInterfaceApp", 2000);
}
catch (PortInUseException e) {
error = e.getMessage();
propertySet();
//ex.printStackTrace();
}
try {
inputStream = serialPort.getInputStream();
}
catch (IOException e) {
error = e.getMessage();
propertySet();
//ex.printStackTrace();
}
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {
error = e.getMessage();
propertySet();
//ex.printStackTrace();
}
serialPort.notifyOnDataAvailable(true);
try {

serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
error = e.getMessage();
writeLog(error);
propertySet();
e.printStackTrace();
}

this.dbInitialize();

readThread = new Thread(this);
readThread.start();
}

public void run() {
while (true) {
this.logSearch();
try {
Thread.sleep(5000);
}
catch (InterruptedException ex) {
}
}
}

左大神在这 2007-04-07
  • 打赏
  • 举报
回复
up

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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