jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Printing to COM1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing to COM1" Watch "Printing to COM1" New topic

Printing to COM1

Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91

I have used javax API to write a java program which takes input from a flat file, and prints it on COM1.
Now if i use Windows HyperTerm, everything is fine, but when i use a properitory receiver i get garbage , is there anything i need to be aware of while sending data over Serial port ?

Here is my main()

package esecurity.tools.SerialTester;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;

import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class SerialTest {
public static void usage()
System.out.println("usage: SerialTester <filename>");

public static void main(String args[]) {

BufferedReader in = null;
CommPortIdentifier portId = null;

SerialPort sPort = null;
String fileName = null;
String str;
OutputStream os = null;

// Command Line Help
if ((args.length > 0)
&& (args[0].equals("-h")
|| args[0].equals("-help"))) {

// Command Line Arguments
if (args.length == 0) {
if (args.length > 0)
fileName = args[0];
if (args.length > 1) {
fileName = args[1];

try {
portId = CommPortIdentifier.getPortIdentifier("COM1");
sPort = (SerialPort) portId.open("SerialDemo", 30000);
sPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
os = sPort.getOutputStream();
} catch (NoSuchPortException e1) {
// TODO Auto-generated catch block
} catch (PortInUseException e) {
// TODO Auto-generated catch block
} catch (UnsupportedCommOperationException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block

try {
in = new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e) {
try {
while ((str = in.readLine()) != null) {
//char data[]=str.getBytes();
for (int i = 0;i< str.length();i++){



} catch (IOException e4) {

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Hmmm...this doesn't seem like a "beginner" question. Perhaps you should try at least the intermediate forum, and probably the advanced one. There may even be other more specialized forums here that will be more appropriate for your question. If so, you will more likely find the answers there.


Java API Documentation
The Java Tutorial
Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
Thanks, I did post in Advaced Java , but no response
It is sorta covered in the JavaRanch Style Guide.
subject: Printing to COM1
Similar Threads
using javax API to send file contents to COM1
Cannot read the data from the serial port on the second time
JTable serialization problem
NullPointerException: name can't be null
how to append the contents of an ArrayList