Manesh Manesh - 1 month ago 15
Java Question

Sending data by serial port

I'm trying to send data into serial port in a device that I have and connected the device serial to my PC. In my PC I'm trying to receive the data through a terminal application. The device is using J2ME, the code that I'm using to connect to the com port is given below.

public boolean connect() {
if (bConnected) {
return true;
} else {
try {
StringBuffer strCom = new StringBuffer(80);
strCom.append("comm:" + strCOMPort
+ ";blocking=on;autocts=off;autorts=off;stopbits=");
//autocts=on;autorts=on;
strCom.append(stopbits);
strCom.append(";bitsperchar=");
strCom.append(bitsperchar);
//strCom.append(";parity=");
//strCom.append(parity);
strCom.append(";baudrate=");
strCom.append(baudrate);

commConn = (CommConnection) Connector.open(strCom.toString());
writeStatusToFile(
"CommConnection(" + strCom.toString()
+ ") with device opened.");
strCom = null;
inStream = commConn.openDataInputStream();
outStream = commConn.openOutputStream();
inStream.skip(inStream.available());
bConnected = true;
return true;
} catch (Exception IOe) {
writeStatusToFile(
"Opening COM0 IOException :" + IOe.getMessage());
return false;
}
}
}


The code that I'm using write data into the serial port is given below.

public void sendData(short[] message){
String bytedata = "";
try
{
System.out.println("Length of message array: " + message.length);
for(int i = 0; i<message.length; i++){
System.out.println("Data: " +message[i]);
bytedata += message[i];
outStream.write(message[i]);
System.out.println("Done");
}
//outStream.write(message);
outStream.flush();
}
catch(Exception ex)
{
System.out.println("Exception during sending bytes--->" + ex.toString());
ex.printStackTrace();
}
System.out.println(
"Data flushed to output stream: " + bytedata);
}


The COM settings for the device is COM0, the baud rate is 4800, parity is none, bits per character 8 and stop bits is 1 (these values are initialised globaly). I set the same in the terminal application that I'm receiving the data from COM port.

The issue that I'm facing is that I'm not receiving anything in my PC when I connect to the serial port. I want to know if I made any mistakes in the code logic. Any suggestions that will help me to analyse the issue are welcome. Please say so if any other information is required.

Answer

This issue was related to the fact that the serial port that my application was trying to access was of type RS232 and this type of serial port will only allow on thread to access the port and there by I could not see the logs along with out to the display. Please note this is not a solution it is a reason

Comments