File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Opening a Serial Port Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Opening a Serial Port" Watch "Opening a Serial Port" New topic

Opening a Serial Port

Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Hi Guys,

I had a look at other threads on serial ports but I still couldn't get mine to work.
Where do I put the port number?
Assume I'm trying to open comm 1. I want to make it really straightforward and do away with assigning variables for now.

I hope someone could help me here.


amerzil co-ed student<br />"Praise be the Code"
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
What problems are you having? Are you getting compile errors? (I ask because there's a typo in your code snippet, where the 'l' is left out of "serialPort".)

Also, your import statements must come before the class declarations, not inside the class declarations...
[ July 19, 2005: Message edited by: Jeff Bosch ]

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
They're just typo errors. In my test program the import is outside the class.
In the class SerialTest, when I did this:

An exception is thrown, and the error message is: "null". So, it seems it can't see a port.I havee the in my compiler's lib directory.
This is the structure of my JBuilder, and where the API comm files are placed:

Hope you can help.
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
Instead of


That will print out the error message, if one is thrown.

-Rich, SCJP 1.4
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
The error is :
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
You haven't initialised your "portId" variable (it's still null).
You'll need something along the lines of:

portId = CommPortIdentifier.getPortIdentifier("COM1");
It is sorta covered in the JavaRanch Style Guide.
subject: Opening a Serial Port
Similar Threads
Problem with jframe, serial communication, dynamic changing value and displaying it
using javax API to send file contents to COM1
Tried for hours and hours to work this out, data buffer from one class back to my main class?
How the objects are constructed at the time of de-serialization?
Printing to COM1