2

I have a problem with arduino (uno) buffer overflow. I am trying to create a simple application in which I insert b (blue diode) or r (red diode) or space (space does not turn on any diode) through the PHP page into the form, send it and then the blue or red diode lights up. Arduino connected via USB (COM3) Specifically: I insert "br r b rbrb" into textarea the first 9 characters are displayed by diode light, but after the ninth character arduina buffer is probably filled and other characters are displayed (by diodes) after about 10 seconds. If anyone knew how to fix it I would like to thank you.

BTW if i use serial monitor in arduino app this problem is not and the buffer will not fill so arduino will process the whole string ("br r b rbrb") without jamming on the ninth character

Arduino code:

int red_2=4;
int blue_1=5;

void setup() {
  Serial.begin(9600);

  // set diodes for outputs
  pinMode(red_2, OUTPUT); // red
  pinMode(blue_1, OUTPUT); // blue


  //all diodes going LOW in start
  digitalWrite(red_2, LOW);
  digitalWrite(blue_1, LOW);

}

void loop() {
  char var_characters;


  if (Serial.available() > 0) {
    var_characters = Serial.read();

    if (var_characters == ' ') {
      Serial.println("Reading charakter: 'space' ");
      delay(500);
      digitalWrite(red_2, LOW);
      digitalWrite(blue_1, LOW);
    }

    if (var_characters == 'b') {
      digitalWrite(blue_1, HIGH);
      Serial.print("Reading charakter: ");
      Serial.println(var_characters);
      delay(500);
      digitalWrite(blue_1, LOW);
    }

    if (var_characters == 'r') {
      digitalWrite(red_2, HIGH);
      Serial.print("Reading charakter: ");
      Serial.println(var_characters);
      delay(500);
      digitalWrite(red_2, LOW);
    }
  }
} 

PHP code:

<html>
<head>
    <title>PHP and Arduino</title>
</head>
<body>
    <?php
        exec("mode COM3: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
        $port = fopen("COM3", "w+");
        sleep(2);
    ?>

    <center>
        <h1>PHP and Arduino</h1>

        <form method="post">
            Message: <textarea type="text" name="msg_form"></textarea>
            <input type="submit" name="send1">
        </form>

        <br>
    </center>

    <?php
        if (isset($_POST['send1'])) {

            $msg_post = $_POST['msg_form'];

            echo "<center> $msg_post </center>";
            fwrite($port, "$msg_post");
        }

        fclose($port);
    ?>
</body>
</html>
Jullidu
  • 33
  • 3

1 Answers1

0

Your delay(500); calls inhibit reading more than 2 characters per second.

There are several ways to improve this behavior.

Keep in mind that the Uno Serial input buffer is very limited (64 char).

If sending e.g. "br r b rbrb" ( 11 characters ) intends a time series of more than 5 seconds, your sender should simply wait long enough before sending the next sequence (easiest manually).

However, your overflow problem description does not really fit (you send less than buffer_size = 64 char ?). Please have a closer look.

datafiddler
  • 1,755
  • 3
  • 17
  • 30
  • delay (500) I have the dida light on for half a second and then go out. Delay is not the problem. – Jullidu Mar 11 '20 at 14:21
  • If your serial buffer is overflowing then it might be. How fast are you sending characters? When you stop and don't read from the buffer then it gets full. – Delta_G Mar 11 '20 at 16:54
  • Actually im not sure, this is my first project with Arudino and PHP. But I think that PHP sends so much data to arduino that arduino can only handle the first few characters and the other characters will begin to accumulate in the arduina buffer, so after about nine characters there is a delay – Jullidu Mar 11 '20 at 17:34
  • At 9600 there's 1 char / ms. If you do a `delay(500);` after a `Serial.read();`, you could get up to 500 characters more into the buffer, which will overflow it. – datafiddler Mar 11 '20 at 18:43
  • So they could help reduce the starting value from 9600 to 4800 ? – Jullidu Mar 11 '20 at 18:58
  • Not really. I modified my answer a bit. – datafiddler Mar 12 '20 at 13:10