not able to read all the records from a hardware device using javax.comm serialport
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   anthony_prasanna
Posted On:   Sunday, June 30, 2002 09:44 PM

i have some 100 records in a hardware device and i am trying to read them from a java program.This device is connected using RS232.I am able to read all the records once in a while when i run the program.For most of the time the program is stuck inbetween some number of records ehen i run it.please try to help me out.I have to retrieve 36 bytes per record.and i am stuck between records.Hope i am clear. i am sending code import java.io.*; import java.util.*; import javax.comm.*; import java.lang.*; import java.math.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputS   More>>
i have some 100 records in a hardware device and i am trying to read them from a java program.This device is connected using RS232.I am able to read all the records once in a while when i run the program.For most of the time the program is stuck inbetween some number of records ehen i run it.please try to help me out.I have to retrieve 36 bytes per record.and i am stuck between records.Hope i am clear.

i am sending code
			
import java.io.*;
import java.util.*;
import javax.comm.*;
import java.lang.*;
import java.math.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
OutputStream outputStream;
public static char a = (char)95;
public static char b = (char)81;
public static char c = (char)0;
public static char d = (char)176;
public static int k;
SerialPort serialPort;
Thread readThread;
public static byte[] result=new byte[36];



public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM2")) {
SimpleRead reader = new SimpleRead();
}
}
}
}


public SimpleRead() {
String messageString = null;
messageString = a + "" + b + "" + c +"" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + c + "" + d;

try {
serialPort = (SerialPort) portId.open("Simple"+String.valueOf(k), 0);
} catch (PortInUseException e) {System.out.println("port in use");}
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println("toooo many");}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}


public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
}


protected int conver(String s ){
int a=Integer.valueOf(s,16).intValue();
return a;
}


protected float fourbycur(String s ){
float a=Integer.valueOf(s,16).floatValue();
float b= a/100;
return b;
}


public void serialEvent(SerialPortEvent event) {
System.out.println("seraial 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:
byte[] readBuffer = new byte[8];

try {


while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);


for (int i =0 ; i < 8 ; i++)
{
result[k]=readBuffer[i];
k++;


if (k==36)
{
BigInteger bi=new BigInteger(result);
String ptr =bi.toString(16);
System.out.println(ptr);
System.out.println(ptr.length());
String two=ptr.substring(4,8);
int x = conver(two);
if (x==0)
{
inputStream.close();
serialPort.close();
System.exit(0);
}
System.out.println("The Record Number is: " + conver(two));

System.out.println("The Date is: " + ptr.substring(8,10) + "-" + ptr.substring(10,12)+ "-" + ptr.substring(12,14));
System.out.println("The Time is: " + ptr.substring(14,16) + ":" + ptr.substring(16,18)+ ":" + ptr.substring(18,20));
String cardId=ptr.substring(20,28);
System.out.println("The Card ID Number is: " + conver(cardId));

String transamt=ptr.substring(28,36);
System.out.println("The Transaction Amount is: " + fourbycur(transamt));
String transtype=ptr.substring(36,38);
System.out.println("The Transaction Type is:"+conver(transtype));
String cardbalance=ptr.substring(38,46);
System.out.println("The Card Balance is: " + fourbycur(cardbalance));
String cartbalance=ptr.substring(46,54);
System.out.println("The Cartridge Balance is: " + fourbycur(cartbalance));
String cartId=ptr.substring(54,62);
System.out.println("The Cartridge ID Number is: " + conver(cartId));
String cardtransno=ptr.substring(66,68);
System.out.println("The Card Transaction Num is:"+conver(cardtransno));
k=0;
//serialPort.removeEventListener();
inputStream.close();
serialPort.close();

portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
if (portId.getName().equals("COM2"))
{
try{
SimpleRead reader = new SimpleRead();
}catch(Exception e){}
}//end if
}//end if
}//end while of portList
break;
}//end if of k

}//end of for
}//end of while

break;
} catch (IOException e) {}
}//switch close
System.out.println("where is this");
}//serialevnt close
}//class close


   <<Less
About | Sitemap | Contact