Thanks.
I have it running now at 38400 baud. With Synaser.
Problem seemed to be port COM14:.
\\\\.\\COM14 or \\.\COM14 or \\.\COM14: did not work either.
COM1: works mostly finally. But very sometimes it does not work. I'm searching under which circumstances it is not working.....
I only use create, ser.connect, ser.config, ser.sendstring, ser.canread, ser.RecvByte and ser.free. With a string XOR of CRC check I think it is reliable enough for me. Missing strings is not a problem; they will be resent within 200 ms.
Attached the working code for Newbees....
program SerialTest; //Uses Synaser
{$mode objfpc}{$H+}
{
Test program for connection between FPC Pascal on Windows and Arduino via USB/COM1: port
The Arduino produces every 5 seconds a string of data with the time.
The Arduino reacts on this type of string ¨´e0xf´¨ with a special answer.
Sending and receiving works well during logn time.
}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, Synaser, Crt
{ add other units after this };
var
d : char = 'a';
ser : TBlockSerial;
SendTime : comp = 0 ;
function millis : comp; // time in ms like Arduino
begin
millis := DateTimeToTimeStamp(Now).time;
end;
Procedure ReceiveData;
begin
while ser.canread(0) do //reads buffer empty each cycle
Write(chr(ser.RecvByte(0))); //IntToHex for binary
end;
Procedure SendData;
begin
if SendTime < millis then // once in a second
begin
ser.sendstring( 'e0' + d + 'f'); //sends strings in format ´e0xf´; x = d loops
d := succ(d); //no buffer overflow is checked.
if d >= 'f' then //amount of data sent is too little.
d := 'a'; //otherwise use ser.canwrite : boolean;
SendTime := millis + 974;
end;
end;
Procedure writeStatus;
begin
Write('Device: ' + ser.Device +
' Windows Status: ' + ser.LastErrorDesc +
' Error Code ' + Inttostr(ser.LastError));
writeln;
end;
procedure RS232_connect;
begin
ser := TBlockSerial.Create;
try
ser.Connect('COM1:'); //ComPort <= 9 check portnumber on PC
Sleep(1000); // /dev/ttyUSB0 \\\\.\\ did not work with W10
ser.config(38400, 8, 'N', SB1, False, False);
Sleep(1000);
writeStatus;
while (not keypressed ) and (ser.LastError = 0) do
begin
ReceiveData;
SendData;
Delay(50); //cool CPU of laptop a bit.....
end; //never ending loop to receive & send data.
finally
Writeln('Serial Port will be freed...');
writeStatus;
ser.free;
Writeln('Serial Port was freed!');
writeStatus;
end;
end;
begin //main
RS232_connect();
Write('Program quit! Type Enter : ');
readln;
end.