[jacorb-developer] Delay in serial communication

Kujtim Hyseni kujtimhyseni at hotmail.com
Thu Apr 19 09:54:50 CEST 2012


Dear Mr. Cross,
thank you for your valuable time spent to answer my questions.
I will now answer your questions and present views.
>>Hi,>>>>I would advise using the latest available version (ideally 3.0 beta 2).I heave downloaded the latest version from web but heaven't installed yet. I think installing newest JacORB is a good idea. JacORB 2.3.0 takes so much resources thus blocking computer (PC) for doing other tasks. I will demonstrate this by an test I've done: I was testing influence of delays in servers routine operations (by inserting Thread.sleep(milliseconds) ) in overall system performance and watched the order of operations executed in server invoked by more than one client (from 2 and 3 clients) at the same time. The order of execution was correct but the PC was almost down (I was listening music in media player and media player was stoped), all programs were stoped or malfunctioning. This is not OK and should not be happening in newest version 3.0. I think this is a cause for my delay too, Jacorb 2.3 somehow enters in a delay when it has to wait (in my case for incomming stream) for something.>>>>I presume you must have some IDL and client code as well? You state you >>provided a version outside of the 'CORBA world' but that version also >>implements BindServerPOA and has similar operation* calls? I would have >>expected something 'standalone'.The two versions I heave presented are both from CORBA but with different source organization. The solutions outside of the 'CORBA world' is very similar and is present as follows:// ********* 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 = "COM5";	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 *********and// ********* CODE *********class TestServer{	MISPSerial ms;		public TestServer()	{			try		{			ms = new MISPSerial();			while(!ms.gotOutputStream);		}		catch(Exception xps)		{			System.out.println(xps);			System.exit(0);		}	}		public String operation1() // Get attribute	{		String retVal = "";		try		{			System.out.println("---------------------------------------------------");			System.out.println("Sending bytes ...");			//byte [] bts = {3, 0, 0};			//byte [] bts = {3, 0, 2};			byte [] bts = {3, 0, 4};			for(int i = 0; i < bts.length; i++)				if (bts[i] >= 0)					System.out.printf("%2d %3d %c\n",i,bts[i],bts[i]);				else					System.out.printf("%2d %3d \n",i,bts[i]);							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= 0; i < ms.readStream[0]; i++)				//rcvd[i-1]=ms.readStream[i];				if (ms.readStream[i] >= 0)					System.out.printf("%2d %3d %c\n",i,ms.readStream[i],ms.readStream[i]);				else					System.out.printf("%2d %3d \n",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() // Set attribute	{		String retVal = "";		try		{			System.out.println("---------------------------------------------------");			System.out.println("Sending bytes ...");			//byte [] bts = {11, 0, 1, 1, 2, 3, 4, 5, 6, 7, 8};			//byte [] bts = {4, 0, 3, 1};			byte [] bts = {7, 0, 5, 1, 2, 3, 4};			for(int i = 0; i < bts.length; i++)				if (bts[i] >= 0)					System.out.printf("%2d %3d %c\n",i,bts[i],bts[i]);				else					System.out.printf("%2d %3d \n",i,bts[i]);							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= 0; i < ms.readStream[0]; i++)				//rcvd[i-1]=ms.readStream[i];				if (ms.readStream[i] >= 0)					System.out.printf("%2d %3d %c\n",i,ms.readStream[i],ms.readStream[i]);				else					System.out.printf("%2d %3d \n",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 static void main(String [] args)	{		try		{			TestServer ts = new TestServer();			//for(byte loop = 0; loop < 100; loop++)			//ts.operation2();			//ts.operation1();			ts.operation1();// 			ts.operation2();// 			ts.operation1();// 			Thread.sleep(2000); 			System.exit(0);		}		catch(Exception xps)		{		}	}}// ********* CODE *********
as you can see MISPSerial class is the same as with CORBA solutions I have presented before.
>>>>Given the serial port will be delivering a byte stream it should be >>possible to reproduce this as a test case without the serial port >>reader/writer code (by using a sample byte stream) and demonstrate how >>JacORB is affecting the transport time. You might want to look at the >>JacORB demos/tests as a starting point.This is a good idea. I will replace serial port reader/writer with file or TCP/IP reader/writer and see how the JacORB is affecting the transport time. I heavent seen the demos/tests provided with JacORB 3.0, I think that in 2.3.0 there is nothing useful. But again I think that a little delay in execution of server operations invokes/affects greater delays.
>>>>Have you tried using Wireshark to analyse the network trace? I cannot use Wireshark in local (localhost) invocations since Windows (XP in my case) doesn't allows it - I will try it when I will establish a network.>>Have you >>tried turning on JacORB debug logging?Can you explain how to do this, please?>>>>> I must add that the actual results (delay times) raise the question>>> whether to use further JacORB in our project!>>>>We would of course be interested to hear of any issues and help on >>diagnosing them would be appreciated. I await your test cases / traces >>with interest.Currently I am designing my data acquisition system in a single machine. It will involve many sensors distributed maybe over the internet. These sensors are seen as servers and will be contacted (values read from them) as ordinary CORBA (JacORB) servers. However, I will send to you the results for each case.>>>>Regards>>>>Nick

> Date: Tue, 10 Apr 2012 19:35:30 +0100
> From: jacorb at goots.org
> To: kujtimhyseni at hotmail.com
> Subject: Re: [jacorb-developer] Delay in serial communication
> 
> 
> Hi,
> 
> I would advise using the latest available version (ideally 3.0 beta 2).
> 
> I presume you must have some IDL and client code as well? You state you 
> provided a version outside of the 'CORBA world' but that version also 
> implements BindServerPOA and has similar operation* calls? I would have 
> expected something 'standalone'.
> 
> Given the serial port will be delivering a byte stream it should be 
> possible to reproduce this as a test case without the serial port 
> reader/writer code (by using a sample byte stream) and demonstrate how 
> JacORB is affecting the transport time. You might want to look at the 
> JacORB demos/tests as a starting point.
> 
> Have you tried using Wireshark to analyse the network trace? Have you 
> tried turning on JacORB debug logging?
> 
>  > I must add that the actual results (delay times) raise the question
>  > whether to use further JacORB in our project!
> 
> We would of course be interested to hear of any issues and help on 
> diagnosing them would be appreciated. I await your test cases / traces 
> with interest.
> 
> Regards
> 
> Nick
> 
> 
> 
> On 10/04/12 09:50, Kujtim Hyseni wrote:
> > 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