Lazarus get data from Arduino

I was able to get Lazarus (pascal) working with Arduin. I can send a command to Arduino to turn on several LEDs. I just add several Hall Effect Sensors on Arduino. I was able to get the data from Arduino on Serial Monitor from Arduino IDE but I could not figure out how to get the code from Arduino to Lazarus. I need some source code for Lazarus and Arduino that Lazarus can get the data from Arduino.

Reid

Show the code you used for sending data to the Arduino.

Here are the codes for Arduino.

int led = 13; // Pin 13

void setup()
{
    pinMode(led, OUTPUT); // Set pin 13 as digital out

    // Start up serial connection
    Serial.begin(9600); // baud rate
    Serial.flush();
}

void loop()
{
    String input = "";

    // Read any serial input
    while (Serial.available() > 0)
    {
        input += (char) Serial.read(); // Read in one char at a time
        delay(5); // Delay for 5 ms so the next char has time to be received

        if (input == "on")
        {
            digitalWrite(led, HIGH); // on
        }
        else if (input == "off")
        {
            digitalWrite(led, LOW); // off
        }
    }
}

Show your Lazarus code that sends data to the Arduino.

Here is the code for Lazarus.

procedure TForm1.FormCreate(Sender: TObject);
begin
  ser := TBlockSerial.Create;
  Sleep(25); //250
  ser.Connect('COM5'); // write here Arduino COM port number (on linux it's something like '/dev/ttyUSB0')
  Sleep(25);  //250
  ser.Config(9600, 8, 'N', SB1, False, False);
  ser.RTS := false; // comment this if needed
  ser.DTR := false; // comment this if needed
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Ser.SendString('off1');
 end;  

Is 'COM5' the actual serial port you have, or is that just the example name in the code?

TBlockSerial has methods for checking the serial buffer for data such as CanRead().
Perhaps the folks over at https://forum.lazarus.freepascal.org/ can help with a simple send and receive example.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.