Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

How to connect Java app with PHP script?

  • 14-09-2011 10:11pm
    #1
    Registered Users, Registered Users 2 Posts: 19,031 ✭✭✭✭


    Hello all,
    I developed a small PHP based web app to control my model railway. It works fine for sending straight (ascii based) commands to the serial port and and on to the railway's digital controller to control trains "manually".

    It is however now insufficient because I want to add an element of automatic train control and to do this I'll need to add hardware to the railway that detects when a block of track is occupied and which sends a message to the serial port.

    The purely PHP based app can only read the serial port on command, it cannot respond to an interrupt on the serial port as far as I know. To overcome this I'm thinking I'll write a Java app to actually talk to and more importantly listen to the serial port and to alter my PHP to communicate with this java app rather than the serial port.

    I'm not sure how to proceed though. I am wondering how I can send messages between the PHP and Java apps. Can someone confirm that a Java app can actually listen (wait for interrupt, not constantly poll) to the serial port and perform an action when something lands in the serial port buffer.

    Assuming it can, can someone also confirm that the Java app could send a message to the running PHP web app (SOAP?) and that I can get my PHP to send messages to the Java app (presently my PHP app relies on a class that uses proc_open() to write to and read from the serial port but I want to be able to send messages between a constantly running PHP web app and a constantly running Java "serial port manager" app, rather than invoking an app from PHP.

    Sorry if this post is a bit waffly, cheers for any thoughts.


Comments

  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    murphaph wrote: »
    Hello all,
    I developed a small PHP based web app to control my model railway. It works fine for sending straight (ascii based) commands to the serial port and and on to the railway's digital controller to control trains "manually".

    It is however now insufficient because I want to add an element of automatic train control and to do this I'll need to add hardware to the railway that detects when a block of track is occupied and which sends a message to the serial port.

    The purely PHP based app can only read the serial port on command, it cannot respond to an interrupt on the serial port as far as I know. To overcome this I'm thinking I'll write a Java app to actually talk to and more importantly listen to the serial port and to alter my PHP to communicate with this java app rather than the serial port.

    I'm not sure how to proceed though. I am wondering how I can send messages between the PHP and Java apps. Can someone confirm that a Java app can actually listen (wait for interrupt, not constantly poll) to the serial port and perform an action when something lands in the serial port buffer.

    Assuming it can, can someone also confirm that the Java app could send a message to the running PHP web app (SOAP?) and that I can get my PHP to send messages to the Java app (presently my PHP app relies on a class that uses proc_open() to write to and read from the serial port but I want to be able to send messages between a constantly running PHP web app and a constantly running Java "serial port manager" app, rather than invoking an app from PHP.

    Sorry if this post is a bit waffly, cheers for any thoughts.

    You could run PHP on the JVM (Quercus). That way, you could communicate directly between PHP and Java, or even write your code in PHP altogether using the Java API.

    You could use Thrift to generate a large part of the service descriptors and communication code to talk between PHP and Java.


  • Registered Users, Registered Users 2 Posts: 19,031 ✭✭✭✭murphaph


    You could run PHP on the JVM (Quercus). That way, you could communicate directly between PHP and Java, or even write your code in PHP altogether using the Java API.

    You could use Thrift to generate a large part of the service descriptors and communication code to talk between PHP and Java.
    Thanks for replying Procasinator. This is a little over my head so please bear with me..

    Am I right in assuming that I would need to install Resin instead of Tomcat to use Quercus?

    Do you have any sources for Thrift? Their homepage is rather bare of any info/tuorials.

    Can you (or someone else) confirm that a java app running under Resin (or Tomcat etc.) is actually able to access the serial port and can receive an interrupt from it? (I am almost certain that a native java app can do so using the javacomm package but an apprunning inside a servlet container?)


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    Tbh, I don't know. :P Never messed with serial ports, let alone in Java, so not sure of the state of libraries, etc. Haven't really done much more than running basic PHP in Quercus, and just too see how it performed.

    But I'd imagine you could do it. Probably something like creating a ServletContextListener, which would open the SerialPort and assign an event listener to handle events. You will probably want to create a class to handle read/writes to this serial port, and a way for PHP to access it (probably by exposing methods via AbstractQuercusModule).

    It is all a bit messy though. :P

    There is probably thousands of ways to expose each language to each other. It might be the best answer though, depending on how time you've invested into the PHP, to port the whole thing to Java or an other language you know that has the library support you need.


Advertisement