Jump to content

controlling serial port over web


Recommended Posts

I am working on my own version of the web rover, and I am trying to figure out a way to talk to the serial port from online. I have looked into php, but I haven't been able to get it to work. I am also looking at using cgi or python. I would prefer to use php, because it's a little easier for me. Is anyone willing to write a little sample code that will let me send an ascii character to a com port?

Link to comment
Share on other sites

Thanks, I managed to get that working. I would like to be able to control the rover with keystrokes though. So far, I have been able to detect the keystrokes with javascript and have the javascript send a popup telling the key that was pressed. I don't know AJAX and I am beginning to learn PHP and Javascript to get this thing working. I was wondering if someone could help me integrate the PHP and Javascript so that PHP can look at the variable javascript has (that has the key pressed in it) and then do stuff based on that. I think it may involve using GET or POST, but I'm not sure. I would prefer to keep from having the page reload each time a key was pressed. Here is what I have so far:

 <?php

$verz="0.0.2";

$comPort = "/dev/ttyUSB0"; /*change to correct com port */


if (isset($_POST["rcmd"])) {

    $rcmd = $_POST["rcmd"];

switch ($rcmd) {

    case Stop:

        $fp =fopen($comPort, "w");

        fwrite($fp, 1); /* this is the number that it will write */

        fclose($fp);

        break;

    case Go:

        $fp =fopen($comPort, "w");

        fwrite($fp, 2); /* this is the number that it will write */

        fclose($fp);

        break;

    default:

        die('???');
    }
}

?>

<html>
<body>
<script type="text/javascript">
function displayunicode(e){
var unicode=e.keyCode? e.keyCode : e.charCode
switch(unicode){
case 38: alert("up arrow");
//window.location.href = "http://localhost/rover/simpler version.php?key=" + unicode;
break;
case 40: alert("down arrow");
//window.location.href = "http://localhost/rover/simpler version.php?key=" + unicode;
break;
case 39: alert("right arrow");
//window.location.href = "http://localhost/rover/simpler version.php?key=" + unicode;
break;
case 37: alert("left arrow");
//window.location.href = "http://localhost/rover/simpler version.php?key=" + unicode;
break;
    }
}
</script>
<form>
<input type="text" size="30" maxlength="1" onkeyup="displayunicode(event); this.select()" value="Click here, then press a key"/>
</form>

<center><h1>Barbie Rover Control</h1><b>Version <?php echo $verz; ?></b></center>
<form method="post" action="<?php echo $PHP_SELF;?>">
<table border="0">
    <tr>
        <td></td>
        <td>
        </td>
        <td></td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Stop" name="rcmd"><br/>
        </td>
        <td></td>
        <td>
            <input type="submit" value="Go" name="rcmd"><br />
        </td>
    </tr>
    <tr>
        <td></td>
        <td><br><br><br><br><br>
        </td>
        <td></td>
    </tr>
</table>
</form>
</body>
</html>

I would really appreciate any help!

Link to comment
Share on other sites

I wanted to do something similar (have button/keypresses control something without having to reload the page). I played around a little bit with some AJAX stuff, in fact some of it is very simple. (much easier than I had expected it to be)

Here's part of a page I made that uses AJAX to send button presses to the server. The ajax() function will do a POST to "buttonpress.php" with the variable buttonaction= whatever we pass ajax(). The buttonpress.php will execute the requested action depending on what buttonaction was passed to it. (so there are two .php files - the main one for the page, and buttonpress.php to take action on the pressed button)

(in this example anything output by buttonpress.php will be ignored - the http request is sent, but no attempt to receive a response is made)

Javascript for AJAX:

<script type="text/javascript">
function ajax(buttonaction)
{
  var request =  new XMLHttpRequest();
  request.open("POST", "buttonpress.php", true);
  request.setRequestHeader("Content-Type",
                           "application/x-www-form-urlencoded");
  request.send("buttonaction="+buttonaction);
}
</script>

HTML that uses the above ajax() function:

<form>
 <button name="buttonaction" onclick="ajax('1');">1</button>
 <button name="buttonaction" onclick="ajax('2');">2</button>
 <button name="buttonaction" onclick="ajax('3');">3</button><br>
 <button name="buttonaction" onclick="ajax('4');">4</button>
 <button name="buttonaction" onclick="ajax('5');">5</button>
 <button name="buttonaction" onclick="ajax('6');">6</button><br>
 <button name="buttonaction" onclick="ajax('7');">7</button>
 <button name="buttonaction" onclick="ajax('8');">8</button>
 <button name="buttonaction" onclick="ajax('9');">9</button><br>
 <button name="buttonaction" onclick="ajax('0');">0</button>
</form>

Link to comment
Share on other sites

So should buttonpress.php should use a $_REQUEST to get the POST from the code you wrote? And then I will have the variable that contains the button that was pressed? I actually wanted to implement this so that the page would detect keys on the keyboard being pressed, and I think if I replaced the button form on your page with the javascript I posted earlier to do that, then it should work. What do you think?

Link to comment
Share on other sites

So should buttonpress.php should use a $_REQUEST to get the POST from the code you wrote? And then I will have the variable that contains the button that was pressed? I actually wanted to implement this so that the page would detect keys on the keyboard being pressed, and I think if I replaced the button form on your page with the javascript I posted earlier to do that, then it should work. What do you think?

I got the data from $_POST["buttonaction"]. I think it should work fine the way you say, using your javascript.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...