PHP comunication A9 <->M4 not work

Discussion in 'UDOO NEO' started by fastecnology, Jan 15, 2017.

  1. fastecnology

    fastecnology New Member

    Joined:
    Jun 4, 2016
    Messages:
    7
    Likes Received:
    0
    this is my code to parse a datagram : "@9,1$"
    header "@" , trailer "$", data "9,1"-> PIN=9 and state=1, but M4 not responding properly
    the code Arduino:
    Code:
    
    #define RCVSIZE 10
    char inByte;
    char msg[RCVSIZE];
      int count = 0;
      int pin=0;
      int count2=0;
      int PIN=0;
      int stato=0;
      char *header;
      char *trailer;
      char *buff=msg;
      int indice=0;
      int pin8,pin9,pin10;
      String result;
    boolean received = false;   // store if Arduino received something
    
    // the setup routine runs once when you press reset:
    void setup() {
      // initialize serial port at a baud rate of 19200 bps
      Serial.begin(115200);
      delay(100);
      //Serial.println("start");
    }
    
    
    
    void loop() {
      // put your main code here, to run repeatedly:
     
      while (Serial.available() > 0) {
      
        // get the new byte:
        inByte = Serial.read();
        msg[count++] = inByte;
        if(count>9) count=0;
        if(inByte=='@')
        {
          header = buff + count;
        }
        else if (inByte=='$')
        {
          trailer = buff + count;
        
          indice = header - buff;
          if( *(buff + (indice + 2)%RCVSIZE)==',')
          {
             PIN =(*(buff + (indice)%RCVSIZE)-48)*10 + *(buff + ((indice + 1)%RCVSIZE) - 48);
              stato=*(buff + (indice + 3)%RCVSIZE) - 48;
          }
          else
          {
             PIN =*(buff + (indice)%RCVSIZE) -48;
             stato=*(buff + (indice + 2)%RCVSIZE) - 48;
          }
          received=true;
          count=0;
        }
      
      
      }
     
      if(received) {
        //Serial.print(PIN);
        if(PIN==0 && stato==0)
        {
          pin8=digitalRead(8);
          pin9=digitalRead(9);
          pin10=digitalRead(10);
          result=String(pin8) + "," + String(pin9) + "," + String(pin10);
          if (Serial.available())
            Serial.println(result);
        
        
        }
        else
        {
            pinMode(PIN, OUTPUT);
            digitalWrite(PIN, stato);
            if (PIN ==  8) {
              if (Serial.available())
                Serial.println("8 ok");
            }
            else if (PIN == 9) {
              if (Serial.available())
                Serial.println("9 ok");
            }
            else if (PIN == 10) {
              if (Serial.available())
                Serial.println("10 ok");
            }
            else
            {
              if (Serial.available())
                Serial.println("ERROR");
            }
        }
      
      
        received = false;
      }
     
      delay(100);
    }
    
    Code:
    and PHP code:
    
    <?php
    
    session_start();
    include "php_serial.class.php";
    define("PORT","/dev/ttyMCC");
    
    if (!$_SESSION['loaded'])
    {
      
    }
    $_SESSION['loaded'] = true;
    
    if (isset($_GET['action'])) {
        $serial = new phpSerial;
        $serial->deviceSet(PORT);
        $serial->confBaudRate(115200);
        $serial->confParity("none");
        $serial->confStopBits(1);
        $serial->confFlowControl("none");
        $serial->deviceOpen();
        $portaStato=$_GET['action'];
        $serial->sendMessage($portaStato);
    
        if ($portaStato == "@8,1$") {
            $_SESSION['$ledOn8'] = true;
            $ledOn = $_SESSION['$ledOn8'];
        }
        elseif ($portaStato == "@8,0$") {
            $_SESSION['$ledOn8'] =false;
            $ledOn = $_SESSION['$ledOn8'];
        }
        else if ($portaStato == "@9,1$") {
            $_SESSION['$ledOn9'] = true;
            $ledOn = $_SESSION['$ledOn9'];
        }
        elseif ($portaStato == "@9,0$") {
            $_SESSION['$ledOn9']= false;
            $ledOn = $_SESSION['$ledOn9'];
        }
        else if ($portaStato == "@10,1$") {
            $_SESSION['$ledOn10'] = true;
            $ledOn = $_SESSION['$ledOn10'];
        }
        elseif ($portaStato == "@10,0$") {
            $_SESSION['$ledOn10']=false;
            $ledOn = $_SESSION['$ledOn10'];
        }
        elseif($portaStato == "@0,0$") {
            }
        $read = $serial->readPort();
    }
    else
       serialStart();
    function serialStart(){
        $serial = new phpSerial;
        $serial->deviceSet(PORT);
        $serial->confBaudRate(115200);
        $serial->confParity("none");
        $serial->confStopBits(1);
        $serial->confFlowControl("none");
        $serial->deviceOpen();
        $serial->sendMessage("@0,0$");
        $readRx = $serial->readPort();
        $pieces = explode(",", $readRx);
         //echo $readRx;
        $_SESSION['$ledOn8']= $pieces[0]; // piece1
        $_SESSION['$ledOn9']= $pieces[1]; // piece2
        $_SESSION['$ledOn10']= $pieces[2]; // piece1
    }
    ?>
    
     
  2. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    You say it is not responding properly. What is not working? Do you get errors?

    What if you print the result of your PIN and stato to Serial? Do you get what you expected?
     
    fastecnology likes this.
  3. fastecnology

    fastecnology New Member

    Joined:
    Jun 4, 2016
    Messages:
    7
    Likes Received:
    0
    Dear,
    From minicom M4 responding properly. I
    nstead of php never answers.
    I've read about the communication problems between the M4 and A9 and I put the delays in Arduino.
    Since communication with minicom works, I do not understand why it does not work with php.
    Best regards
     
  4. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
  5. fastecnology

    fastecnology New Member

    Joined:
    Jun 4, 2016
    Messages:
    7
    Likes Received:
    0
    Code:
    <!DOCTYPE html>
    <html>
        <head>
            <title>RTUFast</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
           
            <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>
        </head>
        <body>
        <div class="container-fluid">
         <div class="row">
         <div class="jumbotron">
            <h1>PANELLO DI CONTROLLO</h1>
            <p><a class="btn btn-primary btn-lg" href="?action=@0,0$" role="button">Aggiorna lo stato attuale dei led</a></p>
            <div class="bg-default text-white">
             <p class="text-muted"  ><b >RISPOSTA: </b><?php echo json_encode( $ledOn ); ?>,<?php echo json_encode( $portaStato ); ?>,<?php echo json_encode( $read ); ?></p>
            </div>
         
         </div>
         </div>
         <div class="row">
           
         <div class="col-md-4">
           
           <div class="panel panel-default">
             <div class="panel-heading">
               <h3 class="panel-title">GPIO21/pin8</h3>
             </div>
             <div class="panel-body">
               <?php if ($_SESSION['$ledOn8']) { ?>
                <img src="light.png" style="width:100px;"><br>
                <a class="btn btn-default" href="?action=@8,0$"><span class="tag tag-pill tag-default">ACCESO, PREMI PER SPEGNERE</span></a>
                <?php } else { ?>
                <img src="light.png" style="width:100px; filter:grayscale(100%); -webkit-filter: grayscale(100%);"><br>
                <a class="btn btn-default" href="?action=@8,1$"><span class="tag tag-pill tag-default">SPENTO, PREMI PER ACCENDERE</span></a>
                <?php } ?>
             </div>
            </div>
           
           
         </div>
         <div class="col-md-4">
            <div class="panel panel-default">
             <div class="panel-heading">
               <h3 class="panel-title">GPIO19/pin9</h3>
             </div>
             <div class="panel-body">
               <?php if ($_SESSION['$ledOn9']) { ?>
                <img src="light.png" style="width:100px;"><br>
                <a class="btn btn-default" href="?action=@9,0$"><span class="tag tag-pill tag-default">ACCESO, PREMI PER SPEGNERE</span></a>
                <?php } else { ?>
                <img src="light.png" style="width:100px; filter:grayscale(100%); -webkit-filter: grayscale(100%);"><br>
                <a class="btn btn-default" href="?action=@9,1$"><span class="tag tag-pill tag-default">SPENTO, PREMI PER ACCENDERE</span></a>
                <?php } ?>
             </div>
            </div>
         </div>
         <div class="col-md-4">
            <div class="panel panel-default">
             <div class="panel-heading">
               <h3 class="panel-title">GPIO1/pin10</h3>
             </div>
             <div class="panel-body">
               <?php if ($_SESSION['$ledOn10']) { ?>
                <img src="light.png" style="width:100px;"><br>
                <a class="btn btn-default" href="?action=@10,0$"><span class="tag tag-pill tag-default">ACCESO, PREMI PER SPEGNERE</span></a>
                <?php } else { ?>
                <img src="light.png" style="width:100px; filter:grayscale(100%); -webkit-filter: grayscale(100%);"><br>
                <a class="btn btn-default" href="?action=@10,1$"><span class="tag tag-pill tag-default">SPENTO, PREMI PER ACCENDERE</span></a>
                <?php } ?>
             </div>
            </div>
         </div>
        </div>
        <div class="row">
         
        </div>
        </div>
            <script type="text/javascript">
        </script>
        </body>
    </html>
    
     
  6. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Just start with the beginning: does the Udoo php example work?
     
  7. fastecnology

    fastecnology New Member

    Joined:
    Jun 4, 2016
    Messages:
    7
    Likes Received:
    0
    I have not tried it, because I took example from it, I have only improved the part of parser
     
  8. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Please try it.
     
  9. fastecnology

    fastecnology New Member

    Joined:
    Jun 4, 2016
    Messages:
    7
    Likes Received:
    0
    Dear waltervl

    Dear, Unfortunately, the php udoo example does not work properly, it is not stable, it works for the first few minutes and then it stops. This problem is serious, for a board that was created to communicate two processors. I also have a board QUAD / DUAL and I tried the same code, on this board (DUAL) work correctly. I have always checked the codes on both boards, and QUAD / DUAL always works.
    I hope udoo, draws up a kernel that allows reliable and robust communication, passed that hurdle, then udooneo could really be a great board.
     
  10. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
  11. fastecnology

    fastecnology New Member

    Joined:
    Jun 4, 2016
    Messages:
    7
    Likes Received:
    0
    Dear,
    But in your ex. u use python not php. My problem is php.
     
  12. Maurice

    Maurice Active Member

    Joined:
    Oct 13, 2015
    Messages:
    394
    Likes Received:
    87
    I'm using Java serial communication and I haven't noticed any problem lately.

    edit: If your program makes some memory error on the M4 the typical behaviour is to lock up completely.
     
    Last edited: Jan 17, 2017
  13. fastecnology

    fastecnology New Member

    Joined:
    Jun 4, 2016
    Messages:
    7
    Likes Received:
    0
    Dear Maurice,

    I believe that the problem is with php,
    at this point I think the php serial library is not optimized for this platform,
    but I'm not sure.
     
  14. waltervl

    waltervl UDOOer

    Joined:
    Dec 12, 2015
    Messages:
    2,314
    Likes Received:
    580
    Found it! The problem is php_serial.class.php
    One problem is that the baudrate cannot be set by Linux command stty (that is what the php serial class is using). Probably because it is a virtual device in the Neo and not a physical one as on the Dual/Quad.
    Another problem is that the php serial function sendmessage has a wait (usleep) in it that messes things up. The pradeep processes are stacking up then. That messes the Neo up I suppose.

    I made the following changes in various files to get things working properly. I hope it helps you with setting up a gooed php website. If you have it running pleas give us a demonstration :)

    in Arduino sketch set baudrate to 38400:
    Serial.begin(38400);

    In php_serial.class.php out comment line 460:
    //usleep((int) ($waitForReply * 1000000));

    And not really needed but I felt a better response: In index.php moved the serial port definition higher so it will not be called at every action but only at the beginning. I also outcommented the confbaudrate line to prevent errors.
    Code:
        define("PORT","/dev/ttyS0");
        $ledOn = false;
        include "php_serial.class.php";
            
        $serial = new phpSerial;
        $serial->deviceSet(PORT);
        //$serial->confBaudRate(115200);
        $serial->confParity("none");
        $serial->confStopBits(1);
        $serial->confFlowControl("none");
        $serial->deviceOpen();
        if (isset($_GET['action'])) {
        .............
    
     

Share This Page