Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do I configure Tomcat to write to a Serial Port?

Reply
Thread Tools

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

 
 
D
Guest
Posts: n/a
 
      10-02-2004
Has anyone managed to get Tomcat to send to a serial port?

I've downloaded and installed the Java Communications API (version 2)
and Tomcat 5.0.25. Both work separately. The whole lot uses Java
1.4.0.

My servlet is below, but when I invoke it, I get a log and an
exception:
"Serial port is already in use."
javax.servlet.ServletException
comms.Send.init(Send.java:55)
javax.servlet.GenericServlet.init(GenericServlet.j ava:211)
org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:117)
org.apache.coyote.tomcat5.CoyoteAdapter.service(Co yoteAdapter.java:160)
org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:793)
org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:702)
org.apache.tomcat.util.net.TcpWorkerThread.runIt(P oolTcpEndpoint.java:571)
org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.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 for
this servlet to 1 only too.


Cheers
D.

--------------------------------
Source:
package comms;



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;
try
{
serialPort = (SerialPort)(portId.open("SendApplet",
2000));
out = new PrintWriter(serialPort.getOutputStream());
serialPort.setSerialPortParams(9600,

SerialPort.DATABITS_8,

SerialPort.STOPBITS_1,

SerialPort.PARITY_NONE);
}
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)
{
System.out.println("doGet.");
doWork(req, resp);
}

public void doPost(HttpServletRequest req, HttpServletResponse
resp)
{
System.out.println("doPost.");
doWork(req, resp);
}

public void doWork(HttpServletRequest req, HttpServletResponse
resp)
{
// Take the msg param and send it to COM1.
String msg = req.getParameter("msg");
System.out.println("Sending["+msg+"].");
if (msg != null)
{
out.println(msg);
}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to configure Cisco routers/switches from new PCs that do not have serial ports. astra Cisco 5 07-16-2008 04:11 PM
How to configure E1 controler and serial interface on two routers ? handa Cisco 0 05-09-2005 06:39 PM
How to Configure new cisco device with no serial port Laptop chairuou Cisco 2 03-06-2005 12:43 PM
different between configure libraries and configure JDKs HS1 Java 0 11-18-2004 03:40 AM
Trying to configure serial interfaces John Cisco 6 12-10-2003 02:17 PM



Advertisments