os/boardsupport/haitest/bspsvs/tools/java/UartEchoServer/src/UartEchoServer.java
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). 
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 *
    16 */
    17 
    18 
    19 import gnu.io.CommPort;
    20 import gnu.io.CommPortIdentifier;
    21 import gnu.io.SerialPort;
    22 import gnu.io.SerialPortEvent;
    23 import gnu.io.SerialPortEventListener;
    24 
    25 import java.util.logging.Logger;
    26 
    27 import org.apache.commons.cli.Options;
    28 import org.apache.commons.cli.Option;
    29 import org.apache.commons.cli.OptionBuilder;
    30 import org.apache.commons.cli.CommandLineParser;
    31 import org.apache.commons.cli.PosixParser;
    32 import org.apache.commons.cli.CommandLine;
    33 import org.apache.commons.cli.HelpFormatter;
    34 
    35 public class UartEchoServer implements SerialPortEventListener
    36 	{
    37 	//	Command line options
    38 	private static final String	OPTION_HELP			="help";
    39 	private static final String	OPTION_PORT			="port";
    40 	private static final String	OPTION_BAUD			="baud_rate";
    41 	private static final String	OPTION_DATA			="data_bits";
    42 	private static final String	OPTION_STOP			="stop_bits";
    43 	private static final String	OPTION_PARITY		="parity";
    44 	private static final String	OPTION_FLOW			="flow_control";
    45 
    46 	//	Default settings for serial port
    47 	private static final String	DEFAULT_PORT_NAME	= "COM1";
    48 	private static final int	DEFAULT_BAUD_RATE	= 9600;
    49 	private static final int	DEFAULT_DATA_BITS	= SerialPort.DATABITS_8;
    50 	private static final int	DEFAULT_STOP_BITS	= SerialPort.STOPBITS_1;
    51 	private static final int	DEFAULT_PARITY		= SerialPort.PARITY_NONE;
    52 	private static final int	DEFAULT_FLOW_CONTROL= SerialPort.FLOWCONTROL_NONE;
    53 
    54 	//	Command format limiters
    55 	private static final byte	BYTE_IGNORE			= '\0'; // Null
    56 	private static final byte	BYTE_CLEARBI		= '\1'; // Ctrl A
    57 	private static final byte	BYTE_QUERYBI		= '\2'; // Ctrl B
    58 	private static final byte	BYTE_RESET			= '\3'; // Ctrl C
    59 	private static final byte	BYTE_BLOCK_START	= '[';
    60 	private static final byte	BYTE_BLOCK_END		= ']';
    61 	private static final String	BLOCK_ASSIGN		= "=";
    62 	private static final String	BLOCK_SEPERATOR		= ",";
    63 
    64 	//	Supported commands
    65 	private static final String	CMD_BAUD_RATE		= "baud_rate";
    66 	private static final String	CMD_DATA_BITS		= "data_bits";
    67 	private static final String	CMD_DELAY			= "delay";
    68 	private static final String	CMD_DISCONNECT		= "disconnect";
    69 	private static final String	CMD_ECHO			= "echo";
    70 	private static final String	CMD_FLOW_CONTROL	= "flow_control";
    71 	private static final String	CMD_LOG				= "log";
    72 	private static final String	CMD_PARITY			= "parity";
    73 	private static final String	CMD_STOP_BITS		= "stop_bits";
    74 
    75 	//	Supported data bits
    76 	private static final String	DATA_BITS_5			= "5";
    77 	private static final String	DATA_BITS_6			= "6";
    78 	private static final String	DATA_BITS_7			= "7";
    79 	private static final String	DATA_BITS_8			= "8";
    80 
    81 	//	Supported flow control
    82 	private static final String	FLOWCONTROL_NONE	= "none";
    83 	private static final String	FLOWCONTROL_RTSCTS	= "rtscts";
    84 	private static final String	FLOWCONTROL_XONXOFF	= "xonxoff";
    85 
    86 	//	Supported parity
    87 	private static final String	PARITY_NONE			= "none";
    88 	private static final String	PARITY_EVEN			= "even";
    89 	private static final String	PARITY_ODD			= "odd";
    90 	private static final String	PARITY_MARK			= "mark";
    91 	private static final String	PARITY_SPACE		= "space";
    92 
    93 	//	Supported stop bits
    94 	private static final String	STOP_BITS_1			= "1";
    95 	private static final String	STOP_BITS_1_5		= "1.5";
    96 	private static final String	STOP_BITS_2			= "2";
    97 
    98 	//	Constants
    99 	private static final int	BUFFER_SIZE			= 1024;
   100 	private static final int	SLEEP_PERIOD		= 200000;
   101 
   102 	private enum EStatus
   103 		{
   104 		EStatusEcho,
   105 		EStatusCommandStart,
   106 		};
   107 
   108 	private Thread				iMainThread;
   109 	private int					iRestartingDelay;
   110 	private boolean				iRestarting;
   111 	private boolean				iRunning;
   112 	private Logger				iLogger;
   113 	private byte[]				iBuffer;
   114 	private EStatus				iStatus;
   115 	private String				iCommand;
   116 	private String				iPortName;
   117 	private byte				iBI;
   118 
   119 	private int					iBaudRate;
   120 	private int					iDataBits;
   121 	private int					iStopBits;
   122 	private int					iParity;
   123 	private int					iFlowControl;
   124 
   125 	private int					iStartupBaudRate;
   126 	private int					iStartupDataBits;
   127 	private int					iStartupStopBits;
   128 	private int					iStartupParity;
   129 	private int					iStartupFlowControl;
   130 
   131 	private CommPortIdentifier	iPortIdentifier;
   132 	private SerialPort			iSerialPort;
   133 
   134 	/*
   135 	 *	Constructor
   136 	 */
   137 	protected UartEchoServer() throws Exception
   138 		{
   139 		iRestartingDelay=0;
   140 		iRestarting=true;
   141 		iRunning=true;
   142 		iLogger=Logger.getLogger("UartEchoServer");
   143 		iBuffer=new byte[BUFFER_SIZE];
   144 		iStatus = EStatus.EStatusEcho;
   145 		iCommand = "";
   146 		iBI=0;
   147 		iPortName=DEFAULT_PORT_NAME;
   148 		iBaudRate=DEFAULT_BAUD_RATE;
   149 		iDataBits=DEFAULT_DATA_BITS;
   150 		iStopBits=DEFAULT_STOP_BITS;
   151 		iParity=DEFAULT_PARITY;
   152 		iFlowControl=DEFAULT_FLOW_CONTROL;
   153 		}
   154 
   155 	/*
   156 	 *	Second pahse constructor
   157 	 *
   158 	 *	@param	aCommandLine	Command line parameters
   159 	 */
   160 	protected void Construct(final CommandLine aCommandLine) throws Exception
   161 		{
   162 		/*
   163 		 *	Set port name if passed in command line
   164 		 */
   165 		if ( aCommandLine.hasOption(OPTION_PORT) )
   166 			{
   167 			iPortName=aCommandLine.getOptionValue(OPTION_PORT);
   168 			iLogger.info("PortName:" + iPortName);
   169 			}
   170 
   171 		/*
   172 		 *	Set baud rate if passed in command line
   173 		 */
   174 		if ( aCommandLine.hasOption(OPTION_BAUD) )
   175 			{
   176 			setBaudRate(aCommandLine.getOptionValue(OPTION_BAUD));
   177 			iLogger.info("Baud Rate:" + iBaudRate);
   178 			}
   179 
   180 		/*
   181 		 *	Set data bits if passed in command line
   182 		 */
   183 		if ( aCommandLine.hasOption(OPTION_DATA) )
   184 			{
   185 			setDataBits(aCommandLine.getOptionValue(OPTION_DATA));
   186 			iLogger.info("Data Bits:" + iDataBits);
   187 			}
   188 
   189 		/*
   190 		 *	Set stop bits if passed in command line
   191 		 */
   192 		if ( aCommandLine.hasOption(OPTION_STOP) )
   193 			{
   194 			setStopBits(aCommandLine.getOptionValue(OPTION_STOP));
   195 			iLogger.info("Stop Bits:" + iStopBits);
   196 			}
   197 
   198 		/*
   199 		 *	Set parity if passed in command line
   200 		 */
   201 		if ( aCommandLine.hasOption(OPTION_PARITY) )
   202 			{
   203 			setParity(aCommandLine.getOptionValue(OPTION_PARITY));
   204 			iLogger.info("Parity:" + iParity);
   205 			}
   206 
   207 		/*
   208 		 *	Set flow control if passed in command line
   209 		 */
   210 		if ( aCommandLine.hasOption(OPTION_FLOW) )
   211 			{
   212 			setFlowControl(aCommandLine.getOptionValue(OPTION_FLOW));
   213 			iLogger.info("Flow Control:" + iFlowControl);
   214 			}
   215 
   216 		/*
   217 		 *	Save startup values. Used by reset command
   218 		 */
   219 		iStartupBaudRate=iBaudRate;
   220 		iStartupDataBits=iDataBits;
   221 		iStartupStopBits=iStopBits;
   222 		iStartupParity=iParity;
   223 		iStartupFlowControl=iFlowControl;
   224 
   225 		/*
   226 		 *	Make sure port is not in use
   227 		 */
   228 		iPortIdentifier=CommPortIdentifier.getPortIdentifier(iPortName);
   229 		if ( iPortIdentifier.isCurrentlyOwned() )
   230 			{
   231 			throw new Exception("Error: Port is currently in use");
   232 			}
   233 
   234 		/*
   235 		 *	Port not in use so open it
   236 		 */
   237 		CommPort	commPort=iPortIdentifier.open(this.getClass().getName(), 2000);
   238 
   239 		/*
   240 		 *	Save thread
   241 		 */
   242 		iMainThread=Thread.currentThread();
   243 
   244 		/*
   245 		 *	Make sure the port is of type serial
   246 		 */
   247 		if ( commPort instanceof SerialPort )
   248 			{
   249 			iSerialPort = (SerialPort) commPort;
   250 			iFlowControl=iSerialPort.getFlowControlMode();
   251 			while ( iRunning )
   252 				{
   253 				initPort();
   254 
   255 				iRestarting=false;
   256 				while ( iRunning && !iRestarting )
   257 					{
   258 					try
   259 						{
   260 						iMainThread.sleep(SLEEP_PERIOD);
   261 						}
   262 					catch ( InterruptedException e )
   263 						{
   264 						}
   265 					}
   266 				iSerialPort.close();
   267 				if ( iRestarting )
   268 					{
   269 					iLogger.finest("Restarting");
   270 					iMainThread.sleep(iRestartingDelay);
   271 					commPort=iPortIdentifier.open(this.getClass().getName(), 2000);
   272 					iSerialPort = (SerialPort) commPort;
   273 					}
   274 				}
   275 			}
   276 		else
   277 			{
   278 			throw new Exception("Error: Only serial ports are handled by this example.");
   279 			}
   280 		}
   281 
   282 	/*
   283 	 *	Initialise the port
   284 	 */
   285 	private void initPort() throws Exception
   286 		{
   287 		iSerialPort.setSerialPortParams(iBaudRate, iDataBits, iStopBits, iParity);
   288 		iSerialPort.setFlowControlMode(iFlowControl);
   289 		iSerialPort.addEventListener(this);
   290 		iSerialPort.notifyOnBreakInterrupt(true);
   291 		iSerialPort.notifyOnCarrierDetect(true);
   292 		iSerialPort.notifyOnCTS(true);
   293 		iSerialPort.notifyOnDataAvailable(true);
   294 		iSerialPort.notifyOnDSR(true);
   295 		iSerialPort.notifyOnFramingError(true);
   296 		iSerialPort.notifyOnOutputEmpty(true);
   297 		iSerialPort.notifyOnOverrunError(true);
   298 		iSerialPort.notifyOnParityError(true);
   299 		iSerialPort.notifyOnRingIndicator(true);
   300 		iSerialPort.setInputBufferSize(BUFFER_SIZE);
   301 		}
   302 
   303 	/*
   304 	 *	Set the baud rate
   305 	 *
   306 	 *	@param	aValue	String representation of the baud rate
   307 	 */
   308 	private void setBaudRate(final String aValue)
   309 		{
   310 		try
   311 			{
   312 			iBaudRate=Integer.parseInt(aValue);
   313 			}
   314 		catch (Exception e)
   315 			{
   316 			iLogger.severe("convertToBaudRate(" + aValue + "):exception" + e);
   317 			}
   318 		}
   319 
   320 	/*
   321 	 *	Set the data bits
   322 	 *
   323 	 *	@param	aValue	String representation of the data bits
   324 	 */
   325 	private void setDataBits(final String aValue)
   326 		{
   327 		try
   328 			{
   329 			if ( aValue.compareTo(DATA_BITS_5)==0 )
   330 				{
   331 				iDataBits=SerialPort.DATABITS_5;
   332 				}
   333 			else if ( aValue.compareTo(DATA_BITS_6)==0 )
   334 				{
   335 				iDataBits=SerialPort.DATABITS_6;
   336 				}
   337 			else if ( aValue.compareTo(DATA_BITS_7)==0 )
   338 				{
   339 				iDataBits=SerialPort.DATABITS_7;
   340 				}
   341 			else if ( aValue.compareTo(DATA_BITS_8)==0 )
   342 				{
   343 				iDataBits=SerialPort.DATABITS_8;
   344 				}
   345 			else
   346 				{
   347 				iLogger.severe("setDataBits(" + aValue + ")");
   348 				}
   349 			}
   350 		catch (Exception e)
   351 			{
   352 			iLogger.severe("setDataBits(" + aValue + "):excpetion" + e);
   353 			}
   354 		}
   355 
   356 	/*
   357 	 *	Set the flow control
   358 	 *
   359 	 *	@param	aValue	String representation of the flow control
   360 	 */
   361 	private void setFlowControl(final String aValue)
   362 		{
   363 		try
   364 			{
   365 			if ( aValue.compareTo(FLOWCONTROL_NONE)==0 )
   366 				{
   367 				iFlowControl=SerialPort.FLOWCONTROL_NONE;
   368 				}
   369 			else if ( aValue.compareTo(FLOWCONTROL_RTSCTS)==0 )
   370 				{
   371 				iFlowControl=SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT;
   372 				}
   373 			else if ( aValue.compareTo(FLOWCONTROL_XONXOFF)==0 )
   374 				{
   375 				iFlowControl=SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT;
   376 				}
   377 			else
   378 				{
   379 				iLogger.severe("setFlowControl(" + aValue + ")");
   380 				}
   381 			}
   382 		catch (Exception e)
   383 			{
   384 			iLogger.severe("setFlowControl(" + aValue + "):exception" + e);
   385 			}
   386 		}
   387 
   388 	/*
   389 	 *	Set the parity
   390 	 *
   391 	 *	@param	aValue	String representation of the parity
   392 	 */
   393 	private void setParity(final String aValue)
   394 		{
   395 		try
   396 			{
   397 			if ( aValue.compareTo(PARITY_NONE)==0 )
   398 				{
   399 				iParity=SerialPort.PARITY_NONE;
   400 				}
   401 			else if ( aValue.compareTo(PARITY_EVEN)==0 )
   402 				{
   403 				iParity=SerialPort.PARITY_EVEN;
   404 				}
   405 			else if ( aValue.compareTo(PARITY_ODD)==0 )
   406 				{
   407 				iParity=SerialPort.PARITY_ODD;
   408 				}
   409 			else if ( aValue.compareTo(PARITY_MARK)==0 )
   410 				{
   411 				iParity=SerialPort.PARITY_MARK;
   412 				}
   413 			else if ( aValue.compareTo(PARITY_SPACE)==0 )
   414 				{
   415 				iParity=SerialPort.PARITY_SPACE;
   416 				}
   417 			else
   418 				{
   419 				iLogger.severe("setParity(" + aValue + ")");
   420 				}
   421 			}
   422 		catch (Exception e)
   423 			{
   424 			iLogger.severe("setParity(" + aValue + "):exception" + e);
   425 			}
   426 		}
   427 
   428 	/*
   429 	 *	Set the stop bits
   430 	 *
   431 	 *	@param	aValue	String representation of the stop bits
   432 	 */
   433 	private void setStopBits(final String aValue)
   434 		{
   435 		try
   436 			{
   437 			if ( aValue.compareTo(STOP_BITS_1)==0 )
   438 				{
   439 				iStopBits=SerialPort.STOPBITS_1;
   440 				}
   441 			else if ( aValue.compareTo(STOP_BITS_1_5)==0 )
   442 				{
   443 				iStopBits=SerialPort.STOPBITS_1_5;
   444 				}
   445 			else if ( aValue.compareTo(STOP_BITS_2)==0 )
   446 				{
   447 				iStopBits=SerialPort.STOPBITS_2;
   448 				}
   449 			else
   450 				{
   451 				iLogger.severe("setStopBits(" + aValue + ")");
   452 				}
   453 			}
   454 		catch (Exception e)
   455 			{
   456 			iLogger.severe("setStopBits(" + aValue + "):exception" + e);
   457 			}
   458 		}
   459 
   460 	/*
   461 	 *	Process a command from the input
   462 	 */
   463 	private void processCommand() throws Exception
   464 		{
   465 		final String	values[] = iCommand.split(BLOCK_SEPERATOR);
   466 
   467 		iSerialPort.getOutputStream().flush();
   468 		for ( int i=0; i<values.length; i++ )
   469 			{
   470 			/*
   471 			 *	Commands should be of the type variable=value
   472 			 */
   473 			final String	parts[]=values[i].split(BLOCK_ASSIGN);
   474 
   475 			if ( parts.length==2 )
   476 				{
   477 				/*
   478 				 *	Set baud rate command
   479 				 */
   480 				if ( parts[0].compareTo(CMD_BAUD_RATE)==0 )
   481 					{
   482 					setBaudRate(parts[1]);
   483 					iSerialPort.setSerialPortParams(iBaudRate, iDataBits, iStopBits, iParity);
   484 					}
   485 				/*
   486 				 *	Set data bits command
   487 				 */
   488 				else if ( parts[0].compareTo(CMD_DATA_BITS)==0 )
   489 					{
   490 					setDataBits(parts[1]);
   491 					iSerialPort.setSerialPortParams(iBaudRate, iDataBits, iStopBits, iParity);
   492 					}
   493 				/*
   494 				 *	Delay command
   495 				 */
   496 				else if ( parts[0].compareTo(CMD_DELAY)==0 )
   497 					{
   498 					final int	delay=Integer.parseInt(parts[1]);
   499 					Thread.sleep(delay);
   500 					}
   501 				/*
   502 				 *	Disconnect command
   503 				 */
   504 				else if ( parts[0].compareTo(CMD_DISCONNECT)==0 )
   505 					{
   506 					iRestartingDelay=Integer.parseInt(parts[1]);
   507 					iRestarting=true;
   508 					}
   509 				/*
   510 				 *	Echo command
   511 				 */
   512 				else if ( parts[0].compareTo(CMD_ECHO)==0 )
   513 					{
   514 					final int		length=parts[1].length();
   515 					for ( int index=0; index<length; ++index )
   516 						{
   517 						final byte	out=(byte)(parts[1].charAt(index));
   518 						iLogger.finest("<< " + out);
   519 						iSerialPort.getOutputStream().write(out);
   520 						}
   521 					}
   522 				/*
   523 				 *	Set flow control command
   524 				 */
   525 				else if ( parts[0].compareTo(CMD_FLOW_CONTROL)==0 )
   526 					{
   527 					setFlowControl(parts[1]);
   528 					iSerialPort.setFlowControlMode(iFlowControl);
   529 					}
   530 				/*
   531 				 *	Log command
   532 				 */
   533 				else if ( parts[0].compareTo(CMD_LOG)==0 )
   534 					{
   535 					iLogger.info(parts[1]);
   536 					}
   537 				/*
   538 				 *	Set parity command
   539 				 */
   540 				else if ( parts[0].compareTo(CMD_PARITY)==0 )
   541 					{
   542 					setParity(parts[1]);
   543 					iSerialPort.setSerialPortParams(iBaudRate, iDataBits, iStopBits, iParity);
   544 					}
   545 				/*
   546 				 *	Set stop bits command
   547 				 */
   548 				else if ( parts[0].compareTo(CMD_STOP_BITS)==0 )
   549 					{
   550 					setStopBits(parts[1]);
   551 					iSerialPort.setSerialPortParams(iBaudRate, iDataBits, iStopBits, iParity);
   552 					}
   553 				/*
   554 				 *	Error command
   555 				 */
   556 				else
   557 					{
   558 					iLogger.severe("Bad command(" + parts[0] + ")");
   559 					}
   560 				}
   561 			else
   562 				{
   563 				iLogger.severe("Bad data");
   564 				}
   565 			}
   566 		}
   567 
   568 	/*
   569 	 *	Process data read from input stream
   570 	 *
   571 	 *	@param	aLength	Length of the data in the buffer
   572 	 */
   573 	private void processInput(final int aLength) throws Exception
   574 		{
   575 		final String	buffer=new String(iBuffer, 0, aLength);
   576 		iLogger.finest(">> " + buffer);
   577 		for ( int index=0; index<aLength; ++index )
   578 			{
   579 			switch ( iBuffer[index] )
   580 				{
   581 			/*
   582 			 *	Ignored data
   583 			 */
   584 			case BYTE_IGNORE:
   585 				break;
   586 			/*
   587 			 *	Clear the break interrupt count
   588 			 */
   589 			case BYTE_CLEARBI:
   590 				iBI=0;
   591 				break;
   592 			/*
   593 			 *	Query the break interrupt count
   594 			 */
   595 			case BYTE_QUERYBI:
   596 				iLogger.finest("BI Count=" + iBI);
   597 				iLogger.finest("<< " + iBI);
   598 				iSerialPort.getOutputStream().write(iBI);
   599 				break;
   600 			/*
   601 			 *	Reset port setting to startup values
   602 			 */
   603 			case BYTE_RESET:
   604 				iBaudRate=iStartupBaudRate;
   605 				iDataBits=iStartupDataBits;
   606 				iStopBits=iStartupStopBits;
   607 				iParity=iStartupParity;
   608 				iFlowControl=iStartupFlowControl;
   609 				iSerialPort.setFlowControlMode(iFlowControl);
   610 				iSerialPort.setSerialPortParams(iBaudRate, iDataBits, iStopBits, iParity);
   611 				iStatus=EStatus.EStatusEcho;
   612 				break;
   613 			default:
   614 				/*
   615 				 *	If in command mode add the byte to the command buffer
   616 				 *	unless we read the end of command block character
   617 				 */
   618 				if ( iStatus==EStatus.EStatusCommandStart ) 
   619 					{
   620 					if ( iBuffer[index]==BYTE_BLOCK_END )
   621 						{
   622 						processCommand();
   623 						iStatus=EStatus.EStatusEcho;
   624 						}
   625 					else
   626 						{
   627 						iCommand += buffer.charAt(index);
   628 						}
   629 					}
   630 				/*
   631 				 *	If in echo mode, echo the character unless we read the
   632 				 *	start of command block character
   633 				 */
   634 				else if ( iStatus==EStatus.EStatusEcho )
   635 					{
   636 					if ( iBuffer[index]==BYTE_BLOCK_START )
   637 						{
   638 						iCommand="";
   639 						iStatus=EStatus.EStatusCommandStart;
   640 						}
   641 					else
   642 						{
   643 						iLogger.finest("<< " + iBuffer[index]);
   644 						iSerialPort.getOutputStream().write(iBuffer[index]);
   645 						}
   646 					}
   647 				break;
   648 				}
   649 			}
   650 		}
   651 
   652 	/*
   653 	 *	Serial port event received
   654 	 *
   655 	 *	@param	aEvent	Event received
   656 	 */
   657 	public void serialEvent(SerialPortEvent aEvent)
   658 		{
   659 		switch ( aEvent.getEventType() )
   660 			{
   661 		case SerialPortEvent.DATA_AVAILABLE:
   662 			/*
   663 			 *	Process data in input buffer
   664 			 */
   665 			try
   666 				{
   667 				final int	length = iSerialPort.getInputStream().read(iBuffer);
   668 
   669 				if ( length>0 )
   670 					{
   671 					processInput(length);
   672 					}
   673 				}
   674 			catch (Exception e)
   675 				{
   676 				e.printStackTrace();
   677 				}
   678 			if ( iRestarting )
   679 				{
   680 				iMainThread.interrupt();
   681 				}
   682 			break;
   683 		case SerialPortEvent.BI:
   684 			/*
   685 			 *	Increment break interrupt count
   686 			 */
   687 			++iBI;
   688 			iLogger.finest("Break Interrupt");
   689 			break;
   690 		case SerialPortEvent.CD:
   691 			/*
   692 			 *	Ignore
   693 			 */
   694 			iLogger.finest("Carrier Detect");
   695 			break;
   696 		case SerialPortEvent.CTS:
   697 			/*
   698 			 *	Ignore
   699 			 */
   700 			iLogger.finest("Clear To Send");
   701 			break;
   702 		case SerialPortEvent.DSR:
   703 			/*
   704 			 *	Ignore
   705 			 */
   706 			iLogger.finest("Data Set Ready");
   707 			break;
   708 		case SerialPortEvent.FE:
   709 			/*
   710 			 *	Ignore
   711 			 */
   712 			iLogger.finest("Framing Error");
   713 			break;
   714 		case SerialPortEvent.OE:
   715 			/*
   716 			 *	Ignore
   717 			 */
   718 			iLogger.finest("Overflow Error");
   719 			break;
   720 		case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
   721 			/*
   722 			 *	Ignore
   723 			 */
   724 			iLogger.finest("Output Buffer Empty");
   725 			break;
   726 		case SerialPortEvent.PE:
   727 			/*
   728 			 *	Ignore
   729 			 */
   730 			iLogger.finest("Parity Error");
   731 			break;
   732 		case SerialPortEvent.RI:
   733 			/*
   734 			 *	Ignore
   735 			 */
   736 			iLogger.finest("Ring Interrupt");
   737 			break;
   738 		default:
   739 			iLogger.finest("Unknown event");
   740 			break;
   741 			}
   742 		}
   743 
   744 	/*
   745 	 *	Application entry point
   746 	 *
   747 	 *	@param	aArgs	COmmand line arguments
   748 	 */
   749 	public static void main(String[] aArgs)
   750 		{
   751 		final Options	options = new Options();
   752 
   753 		options.addOption(new Option(OPTION_HELP, "print this message"));
   754 
   755 		OptionBuilder.withLongOpt(OPTION_PORT);
   756 		OptionBuilder.withDescription("set port COMx");
   757 		OptionBuilder.withValueSeparator( '=' );
   758 		OptionBuilder.hasArg();
   759 		options.addOption(OptionBuilder.create());
   760 
   761 		OptionBuilder.withLongOpt(OPTION_BAUD);
   762 		OptionBuilder.withDescription("set the baud rate");
   763 		OptionBuilder.withValueSeparator( '=' );
   764 		OptionBuilder.hasArg();
   765 		options.addOption(OptionBuilder.create());
   766 
   767 		OptionBuilder.withLongOpt(OPTION_DATA);
   768 		OptionBuilder.withDescription("set the data bits [" + DATA_BITS_5 +"|" + DATA_BITS_6 + "|" + DATA_BITS_7 + "|" + DATA_BITS_8 + "]");
   769 		OptionBuilder.withValueSeparator( '=' );
   770 		OptionBuilder.hasArg();
   771 		options.addOption(OptionBuilder.create());
   772 
   773 		OptionBuilder.withLongOpt(OPTION_STOP);
   774 		OptionBuilder.withDescription("set the stop bits [" + STOP_BITS_1 + "|" + STOP_BITS_1_5 + "|" + STOP_BITS_2 + "]");
   775 		OptionBuilder.withValueSeparator( '=' );
   776 		OptionBuilder.hasArg();
   777 		options.addOption(OptionBuilder.create());
   778 
   779 		OptionBuilder.withLongOpt(OPTION_PARITY);
   780 		OptionBuilder.withDescription("set the parity [" + PARITY_NONE + "|" + PARITY_EVEN + "|" + PARITY_ODD + "|" + PARITY_MARK + "|" + PARITY_SPACE + "]");
   781 		OptionBuilder.withValueSeparator( '=' );
   782 		OptionBuilder.hasArg();
   783 		options.addOption(OptionBuilder.create());
   784 
   785 		OptionBuilder.withLongOpt(OPTION_FLOW);
   786 		OptionBuilder.withDescription("set the flow control [" + FLOWCONTROL_NONE + "|" + FLOWCONTROL_RTSCTS + "|" + FLOWCONTROL_XONXOFF + "]");
   787 		OptionBuilder.withValueSeparator( '=' );
   788 		OptionBuilder.hasArg();
   789 		options.addOption(OptionBuilder.create());
   790 
   791 		final CommandLineParser	parser = new PosixParser();
   792 
   793 		try
   794 			{
   795 			// parse the command line arguments
   796 			final CommandLine	commandLine = parser.parse(options, aArgs);
   797 
   798 			if ( commandLine.hasOption(OPTION_HELP) )
   799 				{
   800 				final HelpFormatter	formatter = new HelpFormatter();
   801 				formatter.printHelp("UartEchoServer", options);
   802 				}
   803 			else
   804 				{
   805 				final UartEchoServer	echoServer=new UartEchoServer();
   806 				echoServer.Construct(commandLine);
   807 				}
   808 			}
   809 		catch ( Exception e )
   810 			{
   811 			e.printStackTrace();
   812 			}
   813 		}
   814 	}