wood burning stoves
The moose likes Tomcat and the fly likes How do I configure Tomcat to write to a Serial Port? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "How do I configure Tomcat to write to a Serial Port?" Watch "How do I configure Tomcat to write to a Serial Port?" New topic

How do I configure Tomcat to write to a Serial Port?

Don Horrell

Joined: Oct 29, 2004
Posts: 4
Does anyone know how to make a Servlet runnimg under Tomcat write to a serial port?

I've downloaded and installed the Java Communications API (version 2) and
Tomcat 5.0.25. The whole lot uses Java 1.4.0., running on Windows XP Professional.

Tomcat works fine and the comms stuff works fine.

My servlet is below, but when I invoke it, I get a log and an exception:
"Serial port is already in use."
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:793) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:702) org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:571) org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:644) java.lang.Thread.run(Thread.java:536)

At some point, I'm going to need to find a way of limiting the poolsize forthis servlet to 1 only too.



package comms;

* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) Donald Horrell 2004</p>
* <p>Company: </p>
* @author Donald Horrell
* @version 1.0

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.comm.*;
import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.*;

public class Send extends HttpServlet implements SingleThreadModel

/** The output stream which writes to the Com port. */
PrintWriter out = null;

public void init() throws ServletException
// Open the serial port.
Enumeration portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements())
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
//if (portId.getName().equals("/dev/term/a")) {
if (portId.getName().equals("COM1"))
SerialPort serialPort = null;
serialPort = (SerialPort)(portId.open("SendApplet", 2000));
out = new PrintWriter(serialPort.getOutputStream());
catch (PortInUseException e)
System.out.println("Serial port is already in use.");
throw(new ServletException());
catch (IOException e)
System.out.println("Failed to connect to output stream.");
throw(new ServletException());
catch (UnsupportedCommOperationException e)
System.out.println("Failed to set comms params.");
throw(new ServletException());

public void destroy()
// Release the serial port.

public void doGet(HttpServletRequest req, HttpServletResponse resp)
doWork(req, resp);

public void doPost(HttpServletRequest req, HttpServletResponse resp)
doWork(req, resp);

public void doWork(HttpServletRequest req, HttpServletResponse resp)
// Take the msg param and send it to COM1.
String msg = req.getParameter("msg");
if (msg != null)
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035
The first thing to look for is "what might be using that port" - I had a problem with comm API one time due to a Palm desktop app automatically grabbing the serial port when the system booted.
In any case, I would move any port communication code to a "bean" type helper object that can be tested outside the servlet environment. Once you get that working it will be easy to give your servlet a singleton instance of the helper class and synchronize access to it.
I agree. Here's the link: http://aspose.com/file-tools
subject: How do I configure Tomcat to write to a Serial Port?
It's not a secret anymore!