/**
 * Author : Rishi Gupta
 * 
 * This file is part of 'serial communication manager' library.
 *
 * The 'serial communication manager' is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The 'serial communication manager' is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with serial communication manager. If not, see <http://www.gnu.org/licenses/>.
 */

package test41;

import com.embeddedunveiled.serial.SerialComManager;
import com.embeddedunveiled.serial.SerialComManager.BAUDRATE;
import com.embeddedunveiled.serial.SerialComManager.DATABITS;
import com.embeddedunveiled.serial.SerialComManager.FLOWCONTROL;
import com.embeddedunveiled.serial.SerialComManager.PARITY;
import com.embeddedunveiled.serial.SerialComManager.STOPBITS;

public class Test41 {
	public static void main(String[] args) {
		try {
			SerialComManager scm = new SerialComManager();
			String PORT = null;
			String PORT1 = null;
			int osType = SerialComManager.getOSType();
			if(osType == SerialComManager.OS_LINUX) {
				PORT = "/dev/ttyUSB0";
				PORT1 = "/dev/ttyUSB1";
			}else if(osType == SerialComManager.OS_WINDOWS) {
				PORT = "COM51";
				PORT1 = "COM52";
			}else if(osType == SerialComManager.OS_MAC_OS_X) {
				PORT = "/dev/cu.usbserial-A70362A3";
				PORT1 = "/dev/cu.usbserial-A602RDCH";
			}else if(osType == SerialComManager.OS_SOLARIS) {
				PORT = null;
				PORT1 = null;
			}else{
			}

			long handle = scm.openComPort(PORT, true, true, true);
			scm.configureComPortData(handle, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B115200, 0);
			scm.configureComPortControl(handle, FLOWCONTROL.NONE, 'x', 'x', false, false);
			long handle1 = scm.openComPort(PORT1, true, true, true);
			scm.configureComPortData(handle1, DATABITS.DB_8, STOPBITS.SB_1, PARITY.P_NONE, BAUDRATE.B115200, 0);
			scm.configureComPortControl(handle1, FLOWCONTROL.NONE, 'x', 'x', false, false);

			// send \r\n and chek they are received at other end, OS does not strip them
			System.out.println("write begin at time : " + System.currentTimeMillis());
			if(scm.writeBytes(handle, "\r\nOK\r\n".getBytes(), 0) == true) {
				System.out.println("write success");
			}
			Thread.sleep(10);
			
			// length must be 6
			while(true){
				byte[] data = scm.readBytes(handle1, 100);
				if(data != null && data.length > 0) {
					System.out.println("data length : " + data.length + " time : " + System.currentTimeMillis());
				}
				Thread.sleep(100);
			}

//			scm.closeComPort(handle);
//			scm.closeComPort(handle1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

@@ -1200,16 +1200,152 @@
 OPBITS;%0A
+import com.embeddedunveiled.serial.SerialComUtil;%0A%0A/*%0A * CR and LF character must be received as is for correct MODEM communication.%0A */
 %0Apublic 
@@ -1522,32 +1522,19 @@
 sType = 
-SerialComManager
+scm
 .getOSTy
@@ -2464,16 +2464,19 @@
 false);%0A
+%09%09%09
 %0A%09%09%09// s
@@ -2479,16 +2479,22 @@
 // send 
+OK%5Cr%5Cn
 %5Cr%5Cn and
@@ -2497,16 +2497,17 @@
  and che
+c
 k they a
@@ -2559,86 +2559,8 @@
 hem%0A
-%09%09%09System.out.println(%22write begin at time : %22 + System.currentTimeMillis());%0A
 %09%09%09i
@@ -2589,14 +2589,14 @@
 e, %22
+OK
 %5Cr%5Cn
-OK
 %5Cr%5Cn
@@ -2691,58 +2691,16 @@
 p(10
+00
 );%0A%09%09%09
-%0A%09%09%09// length must be 6%0A%09%09%09while(true)%7B%0A%09%09%09%09
 byte
@@ -2745,153 +2745,310 @@
 %0A%09%09%09
-%09if(data != null && data.length %3E 0) %7B%0A%09%09%09%09%09System.out.println(%22data length : %22 + data.length + %22 time : %22 + System.currentTimeMillis()
+System.out.println(SerialComUtil.byteArrayToHexString(data, %22:%22));%0A%0A%09%09%09System.out.println(%22%5Cn%22);%0A%09%09%09%0A%09%09%09// send %5Cr%5CnOK%5Cr%5Cn and check they are received at other end, OS does not strip them%0A%09%09%09if(scm.writeBytes(handle, %22%5Cr%5CnOK%5Cr%5Cn%22.getBytes(), 0) == true) %7B%0A%09%09%09%09System.out.println(%22write success%22
 );%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09T
@@ -3066,19 +3066,131 @@
 (100
+0
 );%0A%09%09%09
-%7D%0A%0A//
+byte%5B%5D data1 = scm.readBytes(handle1, 100);%0A%09%09%09System.out.println(SerialComUtil.byteArrayToHexString(data1, %22:%22));%0A%0A
 %09%09%09s
@@ -3214,18 +3214,16 @@
 andle);%0A
-//
 %09%09%09scm.c

update test for CR and LF characters