First of all, you have to define a communication protocol between your Arduino and the PC. This may be binary or text based. I like text based because this may be debbugged and tested easier, e.g. with a standard terminal application on the PC.
In Arduino, you have to implement a command parser accordingly, e.g. like
String currentCommand="";
String currentResult="";
String currentData="";
unsigned long lastActivity=0;
void loop() {
boolean crlf=true;
if (Serial.available())
{
lastActivity=millis();
crlf=false;
while (!crlf)
{
if (Serial.available())
{
lastActivity=millis();
char c = Serial.read();
if (c == '\n')
{
crlf=true;
doCommand();
Serial.println(currentResult);
}
else
{
currentCommand+=c;
}
}
else
{
if (millis()-lastActivity>10000) {
crlf=true;
Serial.println("Command Timeout");
currentCommand="";
}
}
}
}
}
void doCommand()
{
currentCommand.toUpperCase();
currentCommand.trim();
currentCommand=currentCommand+" ";
int spacePosition1=currentCommand.indexOf(" ");
String baseCommand=currentCommand.substring(0,spacePosition1);
baseCommand.trim();
int spacePosition2=currentCommand.indexOf(" ",spacePosition1+1);
String parameter1=currentCommand.substring(spacePosition1,spacePosition2);
parameter1.trim();
spacePosition1=currentCommand.indexOf(" ",spacePosition2+1);
String parameter2=currentCommand.substring(spacePosition2,spacePosition1);
parameter2.trim();
spacePosition2=currentCommand.indexOf(" ",spacePosition1+1);
String parameter3=currentCommand.substring(spacePosition1,spacePosition2);
parameter3.trim();
spacePosition1=currentCommand.indexOf(" ",spacePosition2+1);
String parameter4=currentCommand.substring(spacePosition2,spacePosition1);
parameter4.trim();
currentResult="Unknown command: "+currentCommand;
if (baseCommand=="IDN?")
{
currentResult="my Arduino Version";
}
if (baseCommand=="POS:SET")
{
if (parameter1!="")
{
SetPosition(parameter1);
currentResult="OK";
}
}
if (baseCommand=="POS:GET?")
{
currentResult=GetPosition();
}
currentCommand="";
}
This is a very simple SCPI-like parser (SCPI="Standard Commands for Programmable Instruments" introduced many years ago to control measurement equipment via the IEEE488 standard).
You can connect to Arduino via COM (a virtual COM when using USB) using a terminal application, e.g. of the Arduino IDE, and test the commands IDN?, POS:SET and POS:GET?. A command is terminated by a NewLine character and parameters are separated by space.
The sample code is not very nice but it may be easily expanded by adding other commands. The functions SetPosition() and GetPosition() have to be implemented by you, of course.
If your Arduino code is running well, you just have to replace the teminal application by your Lazarus programm and send the strings via the Lazarus serial communication component.
As mentioned above, you may also use a binary protocol that my be implemented with less code and also less traffic over the interface, but that is a bit harder to debug. In any case, it is necessary to call serial.read() somewhere in the Arduino loop to receive any data that was sent from the PC and act according the content of the received data.
I do not know the SdpoSerial Lazarus component, but I think there will be some methods like WriteData() and ReadData() to send and receive data as well as some properties defining the COM parameters as e.g. Baudrate. Just use the same COM parameters as with the terminal application used for testing before.