reading from COM1
Hello!
I have been able to open the COM1 port and (i think ) write to it. However I don't quite understand how to receive data from the connected device (when it sends data). I have looked at the simpleread code, but don't understand some of its content. It had certain variables about sizes that got me a little confused. I need help. Here is my code of how I got to writing to the port:
import java.util.*;
import javax.comm.*;
import java.io.*;
import java.awt.Toolkit;
publicclass Wavetronix
{
static CommPortIdentifier portID;
static Enumeration portlist;
static SerialPort serialport;
static InputStream inputstream;
static OutputStream outputstream;
static String cmdstring ="XD\r";
static DataOutputStream datafile;
byte [] data;
int datasize;
publicstaticvoid main(String args[])
{
portlist = portID.getPortIdentifiers();
while (portlist.hasMoreElements())
{
portID = (CommPortIdentifier) portlist.nextElement();
if (portID.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println(portID.getName());
if(portID.getName().equals("COM1"))
{
//setting up ouput Stream;
try
{
datafile =new DataOutputStream(new FileOutputStream("dodger.txt"));
}catch(FileNotFoundException e){}
//setting up input stream;
try
{
serialport = (SerialPort) portID.open("WavetronixApp", 2000);
System.out.println("did portID.open return null?" + (serialport ==null));
}catch(PortInUseException e){}
try
{
outputstream = serialport.getOutputStream();
}catch (IOException e){}
try
{
serialport.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}catch (UnsupportedCommOperationException e){}
try
{
outputstream.write(cmdstring.getBytes());
}catch (IOException e){System.out.println("unsuccessful write");}
try
{
inputstream = serialport.getInputStream();
}catch (IOException e){}
/*try
{
if(SerialPortEvent.DATA_AVAILABLE)
{
datasize = inputstream.available();
data = new byte [datasize];
int i =datasize;
while (i > 0)
{
inputstream.read(data, 0, i);
}
}
}*/
}
}
}
}
}

