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;
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.
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.