Knight of Ni
Knight of Ni

Reputation: 1830

Serial port communication and web application

I need to integrate my REST (Jersey) and serial port communication. I have now problem with handling server actions. Basically I am using the RXTXcomm.jar library and open port directly in (parameters for serial port are hardcoded):

public class ApplicationConfig extends Application {
      public ApplicationConfig () {
            // init serial port here
      }
}

My problems:

  1. The init class is instantiated with first call to my API. Can I somehow force the initialization earlier just after server start?
  2. Second is port closing. When I stop the server I need to close my port. How to detect server stop event?
  3. Do you see some problems with this approach? (this is optional question).

Thanks.


I don't want to pass a data via HTTP requests. Just use some parameter, and later fill serial data internally inside REST resource. Request will be very rare and from one 'client' only.

Upvotes: 0

Views: 1310

Answers (2)

Pradeep Pati
Pradeep Pati

Reputation: 5919

If you implement a ServletContextListener you can initialize your init class just as the server starts.

  1. ServletContextListener has a contextInitialized(javax.servlet.ServletContextEvent) method which gets called when the server starts, which you can override to initialize your init class.
  2. ServletContextListener has a contextDestroyed(javax.servlet.ServletContextEvent) which gets called when the server stops, and you can override it to close your port.
  3. Yours seems like a hobby project, so I would suggest you to make it work first, and think about optimizing / cleansing later.

Upvotes: 1

Marko H.
Marko H.

Reputation: 11

I would imagine there could be a potential problem, if second request is made while the first is still writting to the serial port.

You could create a standalone application that would receive data over tcp/ip (or named pipes or other means of ipc), buffer it in queue and send it forward to serial port.

Upvotes: 1

Related Questions