Hi, I am trying to take data from the serial port and put it into a text file. I found something similar online that puts it into a data file but I need the text file. I heard that I have to use a lookup table but I am not sure how to go about doing this, does anyone know anything that can help me out. This is the code I found that puts it into a data file. I tried just changing the file to log.txt but it displays for example 25[]356[], not exactly but something like that, I dont want those square characters in there.
Thanks, Antonio
import java.util.*;
import javax.comm.*;
import java.io.*;
import java.awt.Toolkit;
public class Main{
public static final int BufferByteSize = 1440; //pretty big
static CommPortIdentifier portId;
static Enumeration portList;
public static void main(
String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
//System.out.println("Before While");
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println("Found [" + portId.getName()+ "]");
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a"))
Pinglog Pingloger = new Pinglog(portId,portList,BufferByteSize);
Thread readThread = new Thread(Pingloger);
readThread.setPriority(Thread.MAX_PRIORITY);
readThread.start();
}
}
}
}
}
class Pinglog implements Runnable, SerialPortEventListener {
//Buffer buffer;
DataOutputStream out;
CommPortIdentifier portId;
Enumeration portList;
byte[] readBuffer;
public int numBytes=0;
ByteArrayOutputStream buffer;
InputStream inputStream;
SerialPort serialPort;
Toolkit toolkit = Toolkit.getDefaultToolkit();
boolean firstBeepFlag=true;
int byteSize;
public Pinglog(CommPortIdentifier cpid, Enumeration plist, int byteSize) {
buffer = new ByteArrayOutputStream();
this.byteSize=byteSize;
readBuffer = new byte[byteSize];
//buffer =b;
portId=cpid;
portList=plist;
//set up data output stream
try{
out = new DataOutputStream(
new FileOutputStream("log.dat"));
}catch (FileNotFoundException e) {}
//set up input stream
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
System.out.println("did portId.open return null?" + (serialPort == null));
System.out.println("Com Buffer size: " + serialPort.getInputBufferSize());
} catch (PortInUseException e) {
System.out.println("portId.open threw exception " + e);
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
serialPort.notifyOnFramingError(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {}
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
//while(true){}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
System.out.println("Framing error");
break;
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:
if(firstBeepFlag){
toolkit.beep();
firstBeepFlag=false;
}
readBuffer = new byte[byteSize];
int i=0;
try {
while ( (i = inputStream.available()) > 0 ){
inputStream.read(readBuffer, 0, i);
buffer.write(readBuffer, 0, i);
buffer.writeTo(out);
buffer.reset();
}
}catch (IOException e)
{
e.printStackTrace();
}
break;
}
}
}
[ September 08, 2005: Message edited by: Antonio Oroz ]