# Java and I/O thru a COM port

**URL:** <https://boards.straightdope.com/t/java-and-i-o-thru-a-com-port/375413>\
**Category:** Factual Questions\
**Created:** [October 5, 2006, 6:30pm UTC](https://boards.straightdope.com/t/java-and-i-o-thru-a-com-port/375413 "2006-10-05T18:30:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![VunderBob](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@VunderBob](https://boards.straightdope.com/u/VunderBob)\
**Post date:** [October 5, 2006, 6:30pm UTC](https://boards.straightdope.com/t/java-and-i-o-thru-a-com-port/375413/1 "2006-10-05T18:30:09Z")

</div>

I’ve had a project dumped in my lap that was started in Java. I dream in C/C++…

Most of what I see I understand, but I’m kind of flying blind because I don’t yet know where to find the Java documentation to the level I’m accustomed to in C/C++.

What is the proper way to set up I/O through a serial COM port? The easiest way I know is to define the port as a file, and read or write using the old file procedures or the file stream stuff. There is also the primitive way by messing with buffers, pointers, and device registers.

How do I accomplish the same thing in Java? I assume that I can make use of the _[java.io](http://java.io)_ package to do the file type of port manipulation.

---

<div class="post-metadata">

**Author:** ![larsenmtl](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@larsenmtl](https://boards.straightdope.com/u/larsenmtl)\
**Post date:** [October 5, 2006, 8:10pm UTC](https://boards.straightdope.com/t/java-and-i-o-thru-a-com-port/375413/2 "2006-10-05T20:10:11Z")

</div>

I had a similar project dumped in my lap a while back. I wrote one end of the connection in C for an embedded instrument and the “receiving” end in Java. Sun has the [Java Communications API](http://java.sun.com/products/javacomm/) but it’s only available for Solaris SPARC, Solaris x86, and Linux x86. Since I was writing for windows the alternative that I ended up using is [RXTX](http://www.rxtx.org/). If I recall correctly, I essentially ended up writing a wrapper class for RXTX which opens the port, listens for incoming reads and places them in a BlockingQueue. It also has methods for writing to the port and closing it. My main app starts a seperate thread which waits for reads to enter the BlockingQueue and then does something with them.

Take a look at RXTX and see if it helps.

LarsenMTL

---

<div class="post-metadata">

**Author:** ![VunderBob](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@VunderBob](https://boards.straightdope.com/u/VunderBob)\
**Post date:** [October 6, 2006, 11:47am UTC](https://boards.straightdope.com/t/java-and-i-o-thru-a-com-port/375413/3 "2006-10-06T11:47:20Z")

</div>

Thanks, **Lars** …
