1
votes

I'm trying to read serial data from an Arduino to my Mac (10.12.6). I have downloaded the Synapse library for FreePascal (Lazarus v.2.0.8) from here but I run into an error...

The Arduino is programmed using the Arduino IDE and sends random numbers (between 0 and 255) as a string to the serial port. I am trying to read these strings using FreePascal so that I can plot the values.

Following instructions here I have downloaded and used the Synapse library in the following way:

1) Uncompress the library folder 2) In Lazarus goto 'Project' -> 'Project Inspector' -> 'Add Files from File System' -> select 'synaser.pas'. 3) Add the following code to the form button event:

procedure TForm1.Button1Click(Sender: TObject);
var
  ser: TBlockSerial;
begin
  ser := TBlockSerial.Create;
  try
    ser.Connect('my-com-port'); // write here Arduino COM port number (on linux it's something like '/dev/ttyUSB0')
    Sleep(250);
    ser.config(9600, 8, 'N', SB1, False, False);
    ser.SendString('on'); // button 2 should have 'off' here
  finally
    ser.free;
  end;
end;

4) Press run.

An error message appears in the synaser.pas file:

Error incompatible types: got "ShortInt" expected "Pointer"..

Here's the part of the synaser.pas file referred to:

{$IFNDEF MSWINDOWS}
procedure TBlockSerial.Purge;
begin
  {$IFNDEF FPC}
  SerialCheck(ioctl(FHandle, TCFLSH, TCIOFLUSH));
  {$ELSE}
    {$IFDEF DARWIN}
    SerialCheck(fpioctl(FHandle, TCIOflush, TCIOFLUSH)); { <------ here*******}
    {$ELSE}
    SerialCheck(fpioctl(FHandle, TCFLSH, Pointer(PtrInt(TCIOFLUSH))));
    {$ENDIF}
  {$ENDIF}
  FBuffer := '';
  ExceptCheck;
end; 

I am using a Mac and this error seems to be related to a windows system?

1
Just guessing from the error message the following line gives you a clue: Pointer(PtrInt(TCIOFLUSH)) Have you tried?tonypdmtr
Also, completely aside, 'my-com-port' won't work, obviously. You have to replace this with a valid serial port identifier.J...

1 Answers

1
votes

Thanks @tonypdmtr.

I had to change the 'synaser.pas' file in the following way to get it to work. Bit of a hack I feel:

  1. Change line 1939 to the following:

    SerialCheck(fpioctl(FHandle, TCIOflush, Pointer(PtrInt(TCIOFLUSH))));

  2. Comment out lines 2201, 2202 and 2204 in the same. This sounds like a bad way of doing it but I got it to work.

Changing the button event to the following code allows me to read a single line of data from the Arduino with each button click:

procedure TForm1.Button1Click(Sender: TObject);
var
  ser: TBlockSerial;
begin
  ser := TBlockSerial.Create;
  try
    ser.Connect('/dev/cu.wchusbserial1420'); // write here Arduino COM port number (on linux it's something like '/dev/ttyUSB0')
    Sleep(250); 
    ser.config(9600, 8, 'N', SB1, False, False);
    Label1.Caption := ser.RecvString(100);
  finally
    ser.free;
  end;
end;

enter image description here

I feel the library should just work without having to change it.