Comm APP write outputstream error while another PC send many command to it.

hi,all

I have too apps,one running in 1 pc and sending command to com1 continuely ,the other running in 2nd pc,and accept command from 1st pc and write "ack" to it. They can run over 3 minutes,but later 2nd app will throws exception:"Write error", outputstream error excepiton throws out,and It can't receive any command from 1st pc. 1st pc running well ,It can still send command while 2nd App has stopped.

so I don't know how to solve it,I hope if 2nd app occurs exception,It can still accept command from 1st PC.

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) {

}

}

}

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));

// outputStream.write("servicebill@2605@1@咖啡定购@2007-01-20

// 20:18:49.0@@12#".getBytes(encode));

}

catch (IOException e) {

error = e.getMessage();

writeLog(error);

propertySet();

e.printStackTrace();

}

}

public void dbInitialize() {

// 登记JDBC驱动对象

}

public static void propertySet() {

p.setProperty("CurrentID", String.valueOf(nCurrentID));

p.setProperty("ErrorRow", errorRow.toString());

p.setProperty("Error", error);

File file = new File("interface.properties");

try {

OutputStream fos = new FileOutputStream(file);

p.store(fos, "interface.properties");

fos.close();

}

catch (FileNotFoundException ex) {

System.out.println("file is NULL !!!");

error = ex.getMessage();

propertySet();

//ex.printStackTrace();

}

catch (IOException ex) {

System.out.println("IO is Error !!!");

error = ex.getMessage();

propertySet();

//ex.printStackTrace();

}

}

}

when I receive a command,Iwill sendmessage("ack") to another app,but it often throws exception ,and I can't receive any command.

.....

[9744 byte] By [linvoca] at [2007-11-27 0:18:13]
# 1

Dude, there's no way i'm reading an unformated code that long! Try finding out where the error actually is, or at least use the [code] tag to keep the formatting.

Anyway, you can always handle the exception in a way that will not terminate the listening process if that is your only concern, right?

jadespirita at 2007-7-11 22:08:01 > top of Java-index,Core,Core APIs...