[jacorb-developer] Delay in serial communication
Kujtim Hyseni
kujtimhyseni at hotmail.com
Tue Apr 10 10:50:44 CEST 2012
Following I am answering your questions:
>Hi,
>
> What version of JacORB are you using?
I am using JacORB 2.3.0.
>
>
>Can you provide more details of your system (sample code perhaps) to
>clarify how your CORBA client/server architecture is affecting the
>separate serial port comms?
As I explained before (see email below) my implementation class builds the streams and sends them to serial port (microcontroller). It then receives answer from serial port, but it takes a long time for receiving of order of 20 seconds.
When I implement the communication from usual Java program (outside from JacORB) and use the same mechanisms (package) for communication it takes less than a second to receive the answer. Please not that there is a delay in receiving only, we checked sending and it takes very little time to send.
I realized the communication from within the implementation class and outside it.
1. Here is the code for the first version (communication within the implementation class):
**************** CODE ****************
import gnu.io.*;
import java.io.*;
import java.util.*;
import java.net.*;
class BindServerImpl extends BindServerPOA implements Runnable, SerialPortEventListener
{
CommPortIdentifier portId;
Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public OutputStream outputStream;
boolean outputBufferEmptyFlag = false;
final int BUFSIZE = 256;
public int streamLength = 0;
int totalBytesRead = 0;
int actualBytesRead = 0;
public byte [] readStream = new byte[BUFSIZE];
public boolean isDataReady = false;
public boolean gotOutputStream = false;
//
String comPort = "COM17";
int baudRate = 1200;
public BindServerImpl() throws IOException
{
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// br.readLine();
boolean portFound = false;
Date dt1=new Date();
System.out.println("Starting identification at: " + dt1.toString());
//
portList = CommPortIdentifier.getPortIdentifiers();
// Date
Date dt2=new Date();
System.out.println("Ending identification at: " + dt2.toString());
//
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
// System.out.println(portId.getName());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comPort)) {
System.out.println("Found port: " + comPort);
portFound = true;
}
}
}
if (!portFound) {
System.out.println("Port " + comPort + " not found.");
System.exit(-1);
}
try {
serialPort = (SerialPort) portId.open(comPort, 2000);
}
catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
}
catch (IOException e) {
System.out.println(e);
}
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {}
// activate the DATA_AVAILABLE notifier
serialPort.notifyOnDataAvailable(true);
try {
// set port parameters
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {}
// start the read thread
readThread = new Thread(this);
readThread.start();
System.out.println("Trying starting thread ...");
}
public void initwritetoport() {
// initwritetoport() assumes that the port has already been opened and
// initialized by "public multitcp2comb()"
try {
// get the outputstream
outputStream = serialPort.getOutputStream();
gotOutputStream = true;
System.out.println("Got output stream ...");
}
catch (IOException e) {}
try {
// activate the OUTPUT_BUFFER_EMPTY notifier
serialPort.notifyOnOutputEmpty(true);
}
catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}
}
public void run() {
System.out.println("Thread started ...");
initwritetoport();
}
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:
break;
case SerialPortEvent.DATA_AVAILABLE:
// we get here if data has been received
byte[] readBuffer = new byte[BUFSIZE];
actualBytesRead = 0;
try {
// read data
while (inputStream.available() > 0) {
actualBytesRead=inputStream.read(readBuffer);
if(streamLength==0)
streamLength=readBuffer[0];
if(totalBytesRead+actualBytesRead>streamLength)
{
for(int place_indx=0;place_indx<streamLength-totalBytesRead; place_indx++)
readStream[place_indx+totalBytesRead]=readBuffer[place_indx];
isDataReady = true;
// Data is ready - received
// for(int i=0; i<noOfServers; i++)
// if(t[i]!=null)
// {
// System.out.println("Receiving back the response ... 1 "+i+" "+streamLength);
// t[i].os.write(readStream, 0, streamLength);
// }
// Read the new stream for partial reading
for(int place_indx=streamLength-totalBytesRead;place_indx<actualBytesRead; place_indx++)
readStream[place_indx-(streamLength-totalBytesRead)]=readBuffer[place_indx];
// Set new stream parammeters
streamLength = readBuffer[streamLength-totalBytesRead];
totalBytesRead = actualBytesRead-(streamLength-totalBytesRead);
}
else
if(totalBytesRead+actualBytesRead==streamLength)
{
for(int place_indx=0;place_indx<actualBytesRead; place_indx++)
readStream[place_indx+totalBytesRead]=readBuffer[place_indx];
totalBytesRead=totalBytesRead+actualBytesRead;
isDataReady = true;
// Data is ready - received
// for(int i=0; i<noOfServers; i++)
// if(t[i]!=null)
// {
//
// System.out.println("Receiving back the response ... 2 " + i + " "+streamLength);
// t[i].os.write(readStream, 0, streamLength);
// }
// Reset and prepare for new stream
streamLength = 0;
totalBytesRead = 0;
}
else
{
for(int place_indx=0;place_indx<actualBytesRead; place_indx++)
readStream[place_indx+totalBytesRead]=readBuffer[place_indx];
totalBytesRead=totalBytesRead+actualBytesRead;
}
}
}
catch (IOException e) {}
break;
}
}
public String operation1()
{
String retVal = "";
try
{
System.out.println("---------------------------------------------------");
System.out.println("Sending bytes ...");
byte [] bts = {5, 4, 3, 2, 1};
outputStream.write(bts, 0, bts.length);
System.out.println("Bytes sent ...");
System.out.println("Waiting for receive ...");
while(!isDataReady);
System.out.println("Received ... " + readStream[0] + " byte(s)");
byte [] rcvd = new byte[readStream[0]-1];
for(int i= 1; i < readStream[0]; i++)
rcvd[i-1]=readStream[i];
//System.out.printf("%2d %3d %c\n",i,readStream[i],readStream[i]);
isDataReady = false;
retVal = new String(rcvd);
System.out.println("---------------------------------------------------");
}
catch(Exception xps)
{
System.out.println(xps);
retVal = "Error";
}
return retVal;
}
public String operation2()
{
String retVal = "";
try
{
System.out.println("---------------------------------------------------");
System.out.println("Sending bytes ...");
byte [] bts = {5, 4, 3, 2, 1};
outputStream.write(bts, 0, bts.length);
System.out.println("Bytes sent ...");
System.out.println("Waiting for receive ...");
while(!isDataReady);
System.out.println("Received ... " + readStream[0] + " byte(s)");
byte [] rcvd = new byte[readStream[0]-1];
for(int i= 1; i < readStream[0]; i++)
rcvd[i-1]=readStream[i];
//System.out.printf("%2d %3d %c\n",i,readStream[i],readStream[i]);
isDataReady = false;
retVal = new String(rcvd);
System.out.println("---------------------------------------------------");
}
catch(Exception xps)
{
System.out.println(xps);
retVal = "Error";
}
return retVal;
}
}
**************** CODE ****************
2. The second version (communication outside the implementation class):
**************** CODE ****************
class BindServerImpl extends BindServerPOA
{
MISPSerial ms;
public BindServerImpl()
{
try
{
ms = new MISPSerial();
while(!ms.gotOutputStream);
}
catch(Exception xps)
{
System.out.println(xps);
System.exit(0);
}
}
public String operation1()
{
String retVal = "";
try
{
System.out.println("---------------------------------------------------");
System.out.println("Sending bytes ...");
byte [] bts = {5, 4, 3, 2, 1};
ms.outputStream.write(bts, 0, bts.length);
System.out.println("Bytes sent ...");
System.out.println("Waiting for receive ...");
while(!ms.isDataReady);
System.out.println("Received ... " + ms.readStream[0] + " byte(s)");
byte [] rcvd = new byte[ms.readStream[0]-1];
for(int i= 1; i < ms.readStream[0]; i++)
rcvd[i-1]=ms.readStream[i];
//System.out.printf("%2d %3d %c\n",i,ms.readStream[i],ms.readStream[i]);
ms.isDataReady = false;
retVal = new String(rcvd);
System.out.println("---------------------------------------------------");
}
catch(Exception xps)
{
System.out.println(xps);
retVal = "Error";
}
return retVal;
}
public String operation2()
{
String retVal = "";
try
{
System.out.println("---------------------------------------------------");
System.out.println("Sending bytes ...");
byte [] bts = {5, 4, 3, 2, 1};
ms.outputStream.write(bts, 0, bts.length);
System.out.println("Bytes sent ...");
System.out.println("Waiting for receive ...");
while(!ms.isDataReady);
System.out.println("Received ... " + ms.readStream[0] + " byte(s)");
byte [] rcvd = new byte[ms.readStream[0]-1];
for(int i= 1; i < ms.readStream[0]; i++)
rcvd[i-1]=ms.readStream[i];
//System.out.printf("%2d %3d %c\n",i,ms.readStream[i],ms.readStream[i]);
ms.isDataReady = false;
retVal = new String(rcvd);
System.out.println("---------------------------------------------------");
}
catch(Exception xps)
{
System.out.println(xps);
retVal = "Error";
}
return retVal;
}
}
**************** CODE ****************
and
**************** CODE ****************
import gnu.io.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class MISPSerial implements Runnable, SerialPortEventListener
{
CommPortIdentifier portId;
Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public OutputStream outputStream;
boolean outputBufferEmptyFlag = false;
final int BUFSIZE = 256;
public int streamLength = 0;
int totalBytesRead = 0;
int actualBytesRead = 0;
public byte [] readStream = new byte[BUFSIZE];
public boolean isDataReady = false;
public boolean gotOutputStream = false;
//
String comPort = "COM17";
int baudRate = 1200;
public MISPSerial() throws IOException
{
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// br.readLine();
boolean portFound = false;
Date dt1=new Date();
System.out.println("Starting identification at: " + dt1.toString());
//
portList = CommPortIdentifier.getPortIdentifiers();
// Date
Date dt2=new Date();
System.out.println("Ending identification at: " + dt2.toString());
//
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
// System.out.println(portId.getName());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(comPort)) {
System.out.println("Found port: " + comPort);
portFound = true;
}
}
}
if (!portFound) {
System.out.println("Port " + comPort + " not found.");
System.exit(-1);
}
try {
serialPort = (SerialPort) portId.open(comPort, 2000);
}
catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
}
catch (IOException e) {
System.out.println(e);
}
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {}
// activate the DATA_AVAILABLE notifier
serialPort.notifyOnDataAvailable(true);
try {
// set port parameters
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {}
// start the read thread
readThread = new Thread(this);
readThread.start();
System.out.println("Trying starting thread ...");
}
public void initwritetoport() {
// initwritetoport() assumes that the port has already been opened and
// initialized by "public multitcp2comb()"
try {
// get the outputstream
outputStream = serialPort.getOutputStream();
gotOutputStream = true;
System.out.println("Got output stream ...");
}
catch (IOException e) {}
try {
// activate the OUTPUT_BUFFER_EMPTY notifier
serialPort.notifyOnOutputEmpty(true);
}
catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}
}
public void run() {
System.out.println("Thread started ...");
initwritetoport();
}
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:
break;
case SerialPortEvent.DATA_AVAILABLE:
// we get here if data has been received
byte[] readBuffer = new byte[BUFSIZE];
actualBytesRead = 0;
try {
// read data
while (inputStream.available() > 0) {
actualBytesRead=inputStream.read(readBuffer);
if(streamLength==0)
streamLength=readBuffer[0];
if(totalBytesRead+actualBytesRead>streamLength)
{
for(int place_indx=0;place_indx<streamLength-totalBytesRead; place_indx++)
readStream[place_indx+totalBytesRead]=readBuffer[place_indx];
isDataReady = true;
// Data is ready - received
// for(int i=0; i<noOfServers; i++)
// if(t[i]!=null)
// {
// System.out.println("Receiving back the response ... 1 "+i+" "+streamLength);
// t[i].os.write(readStream, 0, streamLength);
// }
// Read the new stream for partial reading
for(int place_indx=streamLength-totalBytesRead;place_indx<actualBytesRead; place_indx++)
readStream[place_indx-(streamLength-totalBytesRead)]=readBuffer[place_indx];
// Set new stream parammeters
streamLength = readBuffer[streamLength-totalBytesRead];
totalBytesRead = actualBytesRead-(streamLength-totalBytesRead);
}
else
if(totalBytesRead+actualBytesRead==streamLength)
{
for(int place_indx=0;place_indx<actualBytesRead; place_indx++)
readStream[place_indx+totalBytesRead]=readBuffer[place_indx];
totalBytesRead=totalBytesRead+actualBytesRead;
isDataReady = true;
// Data is ready - received
// for(int i=0; i<noOfServers; i++)
// if(t[i]!=null)
// {
//
// System.out.println("Receiving back the response ... 2 " + i + " "+streamLength);
// t[i].os.write(readStream, 0, streamLength);
// }
// Reset and prepare for new stream
streamLength = 0;
totalBytesRead = 0;
}
else
{
for(int place_indx=0;place_indx<actualBytesRead; place_indx++)
readStream[place_indx+totalBytesRead]=readBuffer[place_indx];
totalBytesRead=totalBytesRead+actualBytesRead;
}
}
}
catch (IOException e) {}
break;
}
}
}
**************** CODE ****************
As can be seen the communication is almost the same and hence the delay times (see email below).
I must add that the actual results (delay times) raise the question whether to use further JacORB in our project!
Waiting for answer,
Kind Regards,
Kujtim Hyseni
>
>Regards
>
>Nick
> Date: Thu, 29 Mar 2012 14:16:30 +0100
> From: jacorb at goots.org
> To: jacorb-developer at lists.spline.inf.fu-berlin.de
> CC: kujtimhyseni at hotmail.com
> Subject: Re: [jacorb-developer] Delay in serial communication
>
>
> Hi,
>
> What version of JacORB are you using?
>
> Can you provide more details of your system (sample code perhaps) to
> clarify how your CORBA client/server architecture is affecting the
> separate serial port comms?
>
> Regards
>
> Nick
>
>
> On 29/03/12 12:46, Kujtim Hyseni wrote:
> >
> > Hello,
> >
> > for my project I heave developed a class (implementation class) which
> > forwards requests (operations) from client to serial port, waits for
> > response, receives the response and sends it back to client but it
> > takes a long time to perform it. It delays in receiving the response
> > from serial port. For communication with serial port I have developed
> > a dedicated class which implements Runnable and
> > SerialPortEventListener classes. My class communicates serially with
> > 8051 microcontroller. Again, it seems that class delays in receiving
> > the byte stream from microcontroller. Microcontroller has a built in
> > serial port while my PC uses USB-to-Serial cable. When I communicate
> > microcontroller with the class from usual Java program (from main()
> > routine) it works fine and it takes less than a second to receive the
> > response back.
> >
> > I did some measurements (100 requests) in two ways. In the first,
> > communication class is external and is instantiated from
> > implementation class. In the second, communication class is merged
> > with implementation class. The results are: for first way average
> > response time 9.33266 seconds, minimal response time 4.984 seconds
> > while maximal response time 18.015 seconds; for second way average
> > response time 8.62625 seconds, minimal response time 4.969 seconds
> > while maximal response time 16.015 seconds. These values are
> > unacceptable for real time applications and raise the question for
> > using JacORB further.
> >
> >
> > What do you think and suggest about?
> >
> > Best Regards, Kujtim Hyseni
> > _______________________________________________ jacorb-developer
> > maillist - jacorb-developer at lists.spline.inf.fu-berlin.de
> > https://lists.spline.inf.fu-berlin.de/mailman/listinfo/jacorb-developer
More information about the jacorb-developer
mailing list