Pascal and Arduino Serial communication

Hey! So im trying to send some data to my Arduino through Serial communication on the port 7 (I have an Arduino mega 2560). But it doesent read anything. Arduino code:

void setup() {
    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
        Serial.println(input);
    }
}

Pascal code:

procedure TForm1.Button1Click(Sender: TObject);
var
  ser: TBlockSerial;
begin
  ser := TBlockSerial.Create;
  try
    ser.Connect('COM7');
    Sleep(250);
    ser.config(9600, 8, 'N', SB1, False, False);
    ser.SendString('Test message');
  finally
    ser.free;
  end;
end;  

Can any1 help me? Thank you!

How can you be sure that the Arduino port stays always the same?

Test: make the Arduino send a start message in setup() and show it on the form.

why port 7?
which com port does the Arduino IDE indicate the Arduino is connected to?

That will send whatever you managed to receive(*) back to the sender where there is no code for listening…

You might need a SoftwareSerial port and an usb to serial TTL adapter if you want to use the serial monitor as well.

And opening the serial line will also reboot your arduino so it’s likely that it will miss part of the incoming message (the flush is totally useless)

(*) your receiver code is really poor. I would suggest to study Serial Input Basics to handle this

Maybe you can start by testing the complete round trip:

program Ping;

uses
  SysUtils, synaser;

var
  ser: TBlockSerial;
  i: Integer;

begin
  ser := TBlockSerial.Create;
  ser.LinuxLock := False;       // Only needed on Linux.

  ser.Connect('/dev/ttyUSB0');  // COMx on Windows.
  ser.config(9600, 8, 'N', SB1, False, False);
  Sleep(2000);

  while True do
  begin
    ser.SendString('ping ');
    for i := 1 to 5 do
    begin
      Write(chr(ser.RecvByte(1)));
    end;
    Sleep(1000);
  end;

  ser.free;
end.

and for the Arduino:

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

void loop() {
  while (not Serial.available());
  Serial.print((char)Serial.read());
}

Its connecting to the COM7.

This gives me RunError(103): File not open. After this, ElnOutError: File not open.

Did you edit these two lines?

[edit]

I.e., remove the first line and edit the second one.

Yes I did.

My code now:

procedure TForm1.Button1Click(Sender: TObject);
var
  ser: TBlockSerial;
  i: Integer;
begin
  ser := TBlockSerial.Create;
  ser.Connect('COM7');  // COMx on Windows.
  ser.config(9600, 8, 'N', SB1, False, False);
  Sleep(2000);

  //try
    //ser.Connect('COM7');
    //Sleep(250);
    //ser.config(115200, 8, 'N', SB1, False, False);
    //ser.SendString('Test message');
  //finally
   // ser.free;
   while True do
     begin
       ser.SendString('ping ');
       for i := 1 to 5 do
       begin
         Write(chr(ser.RecvByte(1)));
       end;
       Sleep(1000);
     end;
end; 

And the Write(chr(ser.RecvByte(1))); throws the error.

What do you see when you insert the following line before and after ser.Connect and after ser.config?

Writeln('Device: ' + ser.Device + '   Status: ' + ser.LastErrorDesc + ' ' + Inttostr(ser.LastError));

Before:
Device: Status: OK 0

After:
Device: \.\COM7 Status: OK 0

After ser.config:
Device: \.\COM7 Status: OK 0

Who programs in Pascal anymore?

The company I am working for.

He who wants to write safe code easily.

Hmm, strange. That same code works fine on my Linux machine (tested with an Arduino Pro mini).

Are you absolutely sure that this board is on COM7 and that it is not being used by an other application (is the serial monitor closed)?

Yes, the board is on COM7 and nothing uses that, i see that from the process explorer.

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