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

PHP and the serial port...

Options
  • 09-09-2010 7:00pm
    #1
    Registered Users Posts: 19,025 ✭✭✭✭


    Dear All,
    Further to my current goal of learning PHP, and wanting to build something useful to me, I am going to write an app that will communicate over the serial port to a microcontroller driven device (it's actually a DCC emulator for running model trains, for those that care!).

    I want to write the app in PHP because I want to learn the language and because this app lends itself well to using a database backend (recording running hours of locos for example) and a slick web based graphical interface (for example manually switching points or slowing a loco down with a slider bar etc.). Web based so that ultimately I can control the model railway from a smart phone (way down the line).

    The first hurdle I have come across seems to be PHP's inability to natively speak to the com port under windows. I have found a PHP class from a guy called Remy Sanchez which (apparently) lets PHP write to the com port BUT NOT read from it (no good for me, I need feedback from lots of sensors running into the DCC emulator box and back to the PC). He HAS developed a class which apparently is bidirectional under Linux (probably because Linux mounts such devices to the filesystem, thus simplifying things somewhat when it comes to reading/writing to peripherals).

    I have found 2 third party commercial packages that theoretically allow PHP to communicate fully under windows, but obviously don't feel like paying for this!

    My question boils down to this: Does anyone know if it's possible to communicate with the serial port (read >AND< write) under PHP/windows? If not, can someone suggest a straightforward way to do this?

    If it gets complicated I am going to try doing it under Linux but would rather do it on my windows laptop if at all possible.

    Cheers for any input!


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    If you aren't particularly invested in learning PHP over another language yet, then I'd suggest to just pick another language that does what you need. Bodging things togather and making things work in ways they were never meant to is an invaluable ability for a programmer, but it's definitely better to avoid it where possible.


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    It would probably be better to write an application/service that runs in windows, reading in the sensor data and storing the raw data into your database. Then PHP can get at the data in the database and operate on it from there.

    You don't really want to have the PHP scripts for your site needing to access the hardware. This approach also is flexible, you can multiple banks of sensors pushing into the one database, then your PHP scripts can be running on a separate web-server anywhere on the internet.

    If your writing a windows service C# would probably be the way to go, as it has the comport handling and service controller classes.


  • Registered Users Posts: 19,025 ✭✭✭✭murphaph


    Thanks very much for the input guys. For the moment I have found a way using Javascript to access the serial port (http://www.walking-productions.com/jsserial/new/JSSerial.html) so that would suite me better as PHP would have direct access to the data in the web app this way.

    I had thought about the service->db->PHP approach but the problem (correct me if I'm wrong) is that I will need to see certain ack(knowledgment) strings/bits on ther serial port when I send a command out to the train control unit (this thing in turn encrypts the commands by modulating the power to the model train in the tracks and thus a signal can be sent). I need confirmation back and perhaps more (transponding for example).

    I'm not at all sure this would be easy to accomplish by pushing everthing through a database. I'll see how I get on with the above javascript/applet thing.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    That's not JavaScript, it's a Java applet, and from what I gather it's designed to run client-side. That means when you navigate to your web server, that applet gets installed in your browser and doesn't have access to the serial ports on the server. But maybe I'm hearing you wrong, it sounds to me that you want a application, running on a web server on your laptop that will allow you to control the trains in real time. Is this correct?


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    You can just have a commands table in the database which you "insert" the "commands" into from PHP. You would need a field to say if a particular command has been dealt with. The service then picks up any commands that haven't been dealt with and contacts the serial port. Any response can then be inserted by the service back into the database.

    If you run it on Linux can you not just open the serial port from PHP as a file? Then read/write to it using file i/o?? I've never done this but think with a bit of messing around it should work.


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    You can just have a commands table in the database which you "insert" the "commands" into from PHP. You would need a field to say if a particular command has been dealt with. The service then picks up any commands that haven't been dealt with and contacts the serial port. Any response can then be inserted by the service back into the database.

    If you run it on Linux can you not just open the serial port from PHP as a file? Then read/write to it using file i/o?? I've never done this but think with a bit of messing around it should work.
    While that's definitely a workable solution, it does then require the learning of multiple languages and technologies, i.e. PHP for the web front end, and a different system for the windows service. There's then also the added complexity of using the DB as a message passing system between the two.

    Considering the op is a beginner programmer*, I'd suggest it would be far better to simply do everything on one platform that supports it. For example, using ASP.net with c# or VB.net the OP could do the web interface which communicates with the com ports and easily work with a DB, which then only needs to be used as a data store for train info. If some longer running process was required form monitoring ongoing train activity to write to the DB, then writing a windows service could be done in the same language with the same technologies (and code re-use). It'd just be a simple matter of using a different project template.

    I'd imagine Java and Java server pages could probably do the same, but I'm not sure.




    *this is my understanding from the first post, apologies if I'm wrong


  • Registered Users Posts: 19,025 ✭✭✭✭murphaph


    Evil Phil wrote: »
    That's not JavaScript, it's a Java applet, and from what I gather it's designed to run client-side. That means when you navigate to your web server, that applet gets installed in your browser and doesn't have access to the serial ports on the server. But maybe I'm hearing you wrong, it sounds to me that you want a application, running on a web server on your laptop that will allow you to control the trains in real time. Is this correct?
    That's correct Phil.

    I know it's an applet, but once installed it allows javascript to access the serial port. I see what you mean though....down the road I would be limited to running everything on the server...not ideal and sort of defeats the purpose of using a web application! :(


  • Registered Users Posts: 19,025 ✭✭✭✭murphaph


    stevenmu wrote: »
    I'd imagine Java and Java server pages could probably do the same, but I'm not sure.




    *this is my understanding from the first post, apologies if I'm wrong
    I am a novice programmer so no need for apologies. Actually, using JSPs in Tomcat (already have that installed and working) sounds like a really good idea. The only downside is that I wanted to learn PHP (no java is used at work at all, just php, javascript and mysql stuff).

    As long as java can work with mysql it should be possible to do everything in Java


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    murphaph wrote: »
    Dear All,
    My question boils down to this: Does anyone know if it's possible to communicate with the serial port (read >AND< write) under PHP/windows? If not, can someone suggest a straightforward way to do this?

    Investigated this as part of a project 3 or 4 years ago (not train hardware, but same idea). In the end, we wrote a simple driver in Visual Basic to send and receives messages over a network port. It was simple, and took about 3 days (including bug fixes), despite none of us ever having written in VB before.


  • Registered Users Posts: 7,412 ✭✭✭jmcc


    Tcl would be a useful language for this. Not so sure about PHP as I've never done anything like it in PHP as PHP is not the kind of language for bare metal programming. Java might also be useful as there's a pile of smartcard related stuff that could be of use.

    Regards...jmcc


  • Advertisement
  • Closed Accounts Posts: 585 ✭✭✭MrDarcy


    Hi OP, I had given very similar thought a while ago to building a solution that would allow for the exact same functionality that you are trying to implement here except that it would be for controlling a model aircraft.

    I've a background in C and C++ so if I was going to get into it i'd probably use C# but like youself, I wanted to be able to run instructions to a control system all from a webpage. I can't contribute much as I didn't ever go at it as you have here but I'll watch this thread with interest!

    What I was trying to build was automated sequences for taking off a model aircraft, pre-designed flying formations, automated landing sequences, etc...


  • Registered Users Posts: 19,025 ✭✭✭✭murphaph


    MrDarcy wrote: »
    Hi OP, I had given very similar thought a while ago to building a solution that would allow for the exact same functionality that you are trying to implement here except that it would be for controlling a model aircraft.

    I've a background in C and C++ so if I was going to get into it i'd probably use C# but like youself, I wanted to be able to run instructions to a control system all from a webpage. I can't contribute much as I didn't ever go at it as you have here but I'll watch this thread with interest!

    What I was trying to build was automated sequences for taking off a model aircraft, pre-designed flying formations, automated landing sequences, etc...
    Hi Mr. Darcy

    I've decided to try a couple of routes:
    1) Try to get it running under PHP on Linux (because there ARE classes available to access the serial port (both read and write) for PHP under Linux only.

    2) If the above seems to be heading nowhere I will seriously investigate JSP as a method to get this done in a web application.

    It'll be a long enough road though...not a lot of time to work on it.


  • Closed Accounts Posts: 585 ✭✭✭MrDarcy


    murphaph wrote: »
    Hi Mr. Darcy

    I've decided to try a couple of routes:
    1) Try to get it running under PHP on Linux (because there ARE classes available to access the serial port (both read and write) for PHP under Linux only.

    2) If the above seems to be heading nowhere I will seriously investigate JSP as a method to get this done in a web application.

    It'll be a long enough road though...not a lot of time to work on it.

    Hi Murph, it sounds like a cool little project to have there on the back burner to mull over every now and again...

    You know the way some house alarms and security products now are set up to work on a "web based" basis, maybe there is some sort of a concept there that could be investigated a bit more, as far as I know, the data transfer there is 2-way in nature, for example if an alarm (sensor) goes off you get notified and you can also login to your facility via the web and moderate your settings from there...


Advertisement