New to Java, communicating through a Com port

Hi,

I got an idea from an electronics magazine that I’d like to try.

Basically, take an old exercycle, non-electronic, and hook a hall-effect sensor and magnet to get RPM readings. I built the sensor, and the cable plugs into my com port, which supplies the electricity for it. It works!

The code that the magazine supplies is some custom IDE that appears to be related to .NET (the instructions say that .NET must be loaded. However, I want to learn Java and create a game interface. The sample program is pretty basic; it has a racetrack and moves the player and three computer players around the track.

I’ve got two older books on Java, and neither has any information specific to Com ports.

Here’s the code for the test program:

'Exercise Bike Sensor Test
func main()

gconst chBike 1
dim Rot as integer
clearall

if ComOpen(chBike,baud=9600,port = 1) = 0 then
MsgBox(“Error Opening Com Port”)
end
endif

'Power up the connector
ComDTR chBike,1

Loop:
if ComBuff(chBike) > 0 then
Rot = Rot + 1
Print val(ComInput(chBike)),“Count=”+Rot
endif

DoEvents
goto Loop

endfunc

If anyone knows what the specifics are for communicating through the Com port and could share, I’d appreciate it.

I’ve downloaded NetBeans 5.0 IDE. If there’s a better IDE, I’d like your opinion.

Take a look at the javacomm API, it’s outdated and has annoying habits (like forcing files in java.ext.dirs)

You can take out the annoying bit by decompiling, changing, recompiling, but… that’s illegal… suit yourself.

Anyway, with that API you shuold get it running in notime, it handles I/O with the usual java.io.InputStream/OutputStream’s

NetBeans is fine.

What the serial port connection concerns. You need to download the “Java Communications API” to connect to the serial port. It’s not included with the Java Standard Edition. Go to: http://java.sun.com/products/javacomm/.

Version 2.0 was not very much to my liking. However, 3.0 is out now. It’s really easy to use. There is docu on that web site too.

About V3.0:

“Implementations of the API are currently available for Solaris SPARC, Solaris x86, and Linux x86”

::slight_smile:

How hard can it be to add support for win32/64

I was about to ask, which of the three will work for me. Oh, well. ::slight_smile:

Not exactly what people consider ‘the big 3’ :slight_smile:

Is somewhere a Iink to download a version (V2.x) for windows??
The Sun Page only has V3.0 for download.

 Rafael.-

I had this issue when trying to figure out a way to sync a christmas light display using 16 SSRs with a computer. I ended up using the parallel port, but I found the Java communications API to rather unsuitable. Even though it for the parallel port, it does show you how to use some simple JNI if you didn’t know before:

http://www.hytherion.com/beattidp/comput/pport.htm

I’m sure you could adapt that design, find some C code that’ll ouput to a serial port on a Windows machine, and voila: You’re in business.

I found a copy here:
http://wind.lcs.mit.edu/download/java/javacomm20-win32.zip

However, 2.0 is rather unstable. Sometimes connecting to the serial port does not work for no reason. Trust me, I tried!!!

We actually bought the SerialIO library which works without problems. It’s $50 for Windows:
https://serialio.com//store/index.php?cPath=21_29

I would suggest try out JavaComm 2.0. If you have problems, download the SerialIO library. You won’t need to change any code. You just switch javacomm.jar with serialio.jar and it works like a dream! Note: I am not affiliated with SerialIO.

Well you could write it, then… :wink:

I’m not sure how easy it will be to do in Java, as this type of application often tends to abuse the comm port, using some of the handshake lines as direct polled I/O (I don’t know whether this is the case here). This requires some quite low level programming, which I guess is where you question comes from. The windows API does support this kind of thing. In the worst case, this might need a bit of JNI programming. Alternatively you could use a PIC (www.microchip.com) to measure the frequency & communicate it via proper RS232 protocol, but this is quite complex to do. (You can get simple programmer kits from maplin). The JNI approach (if necessary) would be cheaper.

However, I haven’t checked whether you can do this with the standard Java API, so best to take a longer look at that first :slight_smile:

Alan

/Edited as I didn’t read the question properly the first time.