Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help writing to a file

Status
Not open for further replies.

bannor

Technical User
Jan 5, 2005
1
CA
Hi all. I have a program that reads data from COM Port 2. For every incoming event reported by the COM port one byte of data is printed to the screen. I want to write this information to a file on the fly. The file I want to write to would be: c:\games\warp13\bike.txt The code for the program is below. Any help would be appreciated.

Regards,
Dennisimport java.io.*;
import java.util.*;
import javax.comm.*;

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

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

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

while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();

if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
// System.out.println(portId.getName());
if (portId.getName().equals("COM2")) {
// if (portId.getName().equals("/dev/term/a")) {
COMRead reader = new COMRead();
}
}
}
}

public COMRead() {
try {
serialPort = (SerialPort) portId.open("COMReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
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(20000);
} catch (InterruptedException e) {}
}

public void serialEvent(SerialPortEvent 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[1];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}

 
Thats easier than the stuff youve already done I think (its been a while since I programmed in Java).

Youll need this i think.

DataOutputStream dout=new DataOutputStream (new FileOutputStream("bike.txt"));

dout.writeln("the string you have from data input")

Hope this helps, it may just be an outputstream thinking about it, as I say its been a while!

Philyrobo ;-)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top