Java applet to control serial port from web browser?

Posted on 2011-09-29
Last Modified: 2012-05-12
I realize there have been several posts in the last few years about this topic, but after reviewing them all I have not found a concrete solution.

I have already concluded that it is not possible to access a local PC's serial ports using Javascript running on a browser, unless there is a new JQuery or something else I overlooked that is capable of it (if so, please enlighten me).  So I am looking for a Javascript to Java applet solution.  I CANNOT USE ACTIVEX controls, since the application will be running on Firefox or Chrome only.  Please do not suggest that I try using IE with ActiveX.

The application is simple; I need to read an RS-232 postal scale and put the value in a Javascript variable.  There may also be the possibility of utilizing the USB port as well, so a Java applet that can be loaded into the browser for USB communication would be an acceptable solution as well.

One requirement: I do not want to install anything on the local PC; if there is anything to download, it must be done natively through the browser (as in a Java applet).  I want to be able to walk up to a new PC on our LAN that has Firefox installed but has never run the application and be able to open it instantly.

For reference, here is a similar question that was asked: 23176331.  The response mentions "a Java applet with the right permissions" but then refers to an ActiveX without following up with the Java suggestion.

Any thoughts?
Question by:RareSeeds
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
LVL 86

Expert Comment

ID: 36815838
You'd probably be better off with a Web Start app. It would need to be signed

Author Comment

ID: 36815923
A JAWS would first require the installation of JRE (requiring administrator permissions on our domain; the average user will not be able to do that).  Also, how would I get the returned value back to Javascript?

The process would need to be transparent to the user.  When they click the WEIGH button on the browser, the weight needs to appear in a text box.  Just like that; no popup, click another button, etc.
LVL 47

Expert Comment

ID: 36816536

You'll need to write a signed applet to access the serial port - look here for instructions:
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Expert Comment

ID: 36816602
>>A JAWS would first require the installation of JRE (requiring administrator permissions on our domain; the average user will not be able to do that).

That would also be the case for a Java applet.

LVL 47

Expert Comment

ID: 36816616

Still, I would suggest not to deal with Applets in this case.

Look, JDK or JRE in fact do not require any installation. Moreover, you can even use it from network share.
So waht you do - you just copy the java home directory to your fileshare and have path incluse fileshare/jdk/bin
folder. Then java cmmand will work and execute your classes.

Then yuou write your program as normal java application and put your classes also
to some folder on fileshare

then you make a .bat file in that folder and make sure that it has a path referencing your java on fileshare/bin
folder (and of course required classpath)

Then each user at his/her PC have only to map the fileshare (if they already don't have it mapped) and
then create shortcut on their desktop to that .bat file
You can even arrange it so, that you send them an email, from which they can drag the shortcut to their desktop

This system worked very well for me for many site-wide applications

LVL 47

Expert Comment

ID: 36816642
The great advantage of having java on the filesahre is that you will not depend on the java
they have on their machine - and you'll never have any issues when they install a new java

It is true, that application may open a little bit slower than when java is local, but
that applies only to the opening and if network is more or less OK - then the difference is not that
LVL 86

Expert Comment

ID: 36817836
>>You'll need to write a signed applet to access the serial port - look here for instructions:

>>Still, I would suggest not to deal with Applets in this case.


Author Comment

ID: 36904149
So exactly how does the batch file get called from the browser when the WEIGH button is clicked?  Your suggestion may work well for you, but honestly that's by far more work-around that I am looking for.  If JRE does not require installation privileges and can run off a share, then why not just go back to the applet idea?  Because JRE needs to be installed locally to access the local ports?

What I am really looking for is a simple solution, whether Java applet or Flash.  What that means is either a product name or an actual code sample.  Like I mentioned before, it needs to be a simple click on the web interface that queries the scale and puts the weight in the text box (or Javascript variable, etc.).

If anyone can provide a ready-to-go solution (not just suggestions or theories or "try this"), that would be the ideal answer.

@for_yan: I'm not bashing your suggestions, your idea seems like it would work for some situations.  It's just not what I really need for this case.  If there IS no real answer, I may have to do it that way in the end.

Accepted Solution

RareSeeds earned 0 total points
ID: 36942405
I have finally found a working solution for this long-standing problem.  There is a Java applet that communicates with the serial ports here:

The code and several examples are provided.  If a person knows Java programming, the applet can be customized for your specific application.  My modified applet is called and receives parameters (baud, parity, etc.) from a JavaScript function, and in turn calls a specified JS function with the returned data from the serial device (or a different function if an error occurs).

There is much documentation about JavaScript-Java interaction, but this is the resource I used as a guide:

This approach is completely transparent to the user.  As mentioned before, the applet will need to be signed, or the browser settings changed to allow it to load without prompting.

Author Closing Comment

ID: 36978251
This solution has the potential to help many of the other questions asked along the same lines over the past few years.

Expert Comment

ID: 37898297
I'm also trying to achieve the same result, a java applet that reads the data from a serial port and then passes that data through to the javascript. I'm currently trying the accepted solution posted by RareSeeds, but i'm experiencing some issues. First i followed the documentation about Javascript-Java interaction with some basic examples (without making use of the classes in the jssc library yet). It works fine in both ways (java <-> javascript)

Now my problem is the following, when i want to make use of the classes provided by the jssc library i get the following exception in my java console log.

This exception occurs when i want to get a list of available ports on my system by using the getPortsNames function provided by the SerialPortList class. It works fine when i run the applet in the Applet Viewer but when i integrate the applet in the html file it doesn't work anymore.

I tried to use other functions from other classes in the jssc jar and keep getting the same NoClassDefFoundError, so it seems to me every time i use a class from the jar the interaction between my java applet and javascript drops the moment when i use these functions.

considering you managed to get things working, RareSeeds, i was hoping you could help me out here.

Download link to the application:
LVL 86

Expert Comment

ID: 37899733
I'm also trying to achieve the same result

You need to open a new question

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javascript form - error when hitting enter 3 28
Why is my $_POST not going to results page 10 41
title attribute 5 26
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question