Go Down

Topic: Delphi serial communication (Read 28111 times) previous topic - next topic

Pakura

hello.
sorry for my bad english. :smiley-roll: I'm from Georgia.
I am a beginner programmer.
i want write delphi app for arduino serial com port communication. I searched example code for delphi and comport library, http://sourceforge.net/projects/comport/ but i don't know how to path library in delphi 7 or delphi xe3 and DCU.
plz. plz plz give me step by step instructions for this. Thank you in advance.  :(

3dprinter

Here is something I coded a while back - Delphi 5 and using the synaser library.

The little delphi program allowed me to send a single line to the Arduino or a whole file. The Arduino send XON/XOFF to slow down the pc.

NB: This just for you to look at (copy if you want to) and experiment with, but I dont have time to explain it all.

Pakura

plz Tell me how to install Synaser library in delphi 5 or delphi 7, delphi xe2

3dprinter


(copy if you want to) and experiment with, but I dont have time to explain it all.
The link to the synaser library is in the comments : http://synapse.ararat.cz/files/synaser.zip

Allie

Hi There,

This is what i have created with delphi xe3 and TComPort component.

Check this out : http://forums.about.com/n/pfx/forum.aspx?nav=messages&webtag=ab-delphi&tid=19931

The comport component is here :

http://sourceforge.net/projects/comport/


I think i understand you have trouble installing the component, just follow the instructions, and the only "paths" i added are these :


Lets hope more people are going to help us out with the Delphi side of it :)

Good luck.
Alphons

cjdelphi

It's pretty straight forward... i'm not at my desktop so i can't provide code atm...

You open up a handle to a file (which happens to be the com port) and off it goes... (read my id lol)

aarondc

You own both Delphi 7 and XE3 but don't know how to set library paths?

Seems legit.

cjdelphi

Code: [Select]

var
  Form1: TForm1;
    ComFile: THandle;

implementation

{$R *.DFM}


function OpenCOMPort: Boolean;
var
  DeviceName: array[0..80] of Char;
begin
   { First step is to open the communications device for read/write.
     This is achieved using the Win32 'CreateFile' function.
     If it fails, the function returns false.

     Wir versuchen, COM1 zu öffnen.
     Sollte dies fehlschlagen, gibt die Funktion false zurück.
   }
  StrPCopy(DeviceName, 'COM1:');

  ComFile := CreateFile(DeviceName,
    GENERIC_READ or GENERIC_WRITE,
    0,
    nil,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);

  if ComFile = INVALID_HANDLE_VALUE then
    Result := False
  else
    Result := True;
end;


function SetupCOMPort: Boolean;
const
  RxBufferSize = 256;
  TxBufferSize = 256;
var
  DCB: TDCB;
  Config: string;
  CommTimeouts: TCommTimeouts;
begin
   { We assume that the setup to configure the setup works fine.
     Otherwise the function returns false.

     wir gehen davon aus das das Einstellen des COM Ports funktioniert.
     sollte dies fehlschlagen wird der Rückgabewert auf "FALSE" gesetzt.
   }

  Result := True;

  if not SetupComm(ComFile, RxBufferSize, TxBufferSize) then
    Result := False;

  if not GetCommState(ComFile, DCB) then
    Result := False;

  // define the baudrate, parity,...
  // hier die Baudrate, Parität usw. konfigurieren

  Config := 'baud=9600 parity=n data=8 stop=1';

  if not BuildCommDCB(@Config[1], DCB) then
    Result := False;

  if not SetCommState(ComFile, DCB) then
    Result := False;

  with CommTimeouts do
  begin
    ReadIntervalTimeout         := 0;
    ReadTotalTimeoutMultiplier  := 0;
    ReadTotalTimeoutConstant    := 1000;
    WriteTotalTimeoutMultiplier := 0;
    WriteTotalTimeoutConstant   := 1000;
  end;

  if not SetCommTimeouts(ComFile, CommTimeouts) then
    Result := False;
end;


{
  The following is an example of using the 'WriteFile' function
  to write data to the serial port.

  Folgendes Beispiel verwendet die 'WriteFile' Funktion, um Daten
  auf den seriellen Port zu schreiben.
}


procedure SendText(s: string);
var
  BytesWritten: DWORD;
begin
   {
     Add a word-wrap (#13 + #10) to the string

     An den übergebenen String einen Zeilenumbruch (#13 + #10) hängen
   }
  s := s + #13 + #10;
  WriteFile(ComFile, s[1], Length(s), BytesWritten, nil);
end;


{
  The following is an example of using the 'ReadFile' function to read
  data from the serial port.
 
  Folgendes Beispiel verwendet die 'ReadFile' Funktion, um Daten
  vom seriellen Port zu lesen.
}


Function ReadText: string;
var
  d: array[1..80] of Char;
  s: string;
  BytesRead, i: Dword;
begin
  Result := '';
  if not ReadFile(ComFile, d, SizeOf(d), BytesRead, nil) then
  begin
    { Raise an exception }
  end;
  s := '';
  for i := 1 to BytesRead do s := s + d[I];
  Result := s;
end;


procedure CloseCOMPort;
begin
  // finally close the COM Port!
  // nicht vergessen den COM Port wieder zu schliessen!
  CloseHandle(ComFile);
end;


end.


Change the com port and other details to match your own, then you can control it as though you're using the serial console..

ps to set the paths (Menu > Tools > Enviroment Options > Library, then add the path or your components wont be seen at compile time)

aarondc

Was under the impression the format for the com port file name was

Code: [Select]
\\.\<COM PORT>
eg:

\\.\COM2



aarondc

To OP: if you want to see the origin of cjdelphi's source code, have a look here: http://www.swissdelphicenter.ch/en/showcode.php?id=841

You'll also probably need to Use Windows at the very least to get access to Windows function calls.



Paliarg

I have a GPS receiver that outputs RTCM correctins over a serial port but only handles CTS-RTS flow control. I need to transform this flow control into an Xon Xoff type so I can connect it to another instrument that only works with Xon Xoff. I was told using an Arduino board could be possible to do it. Any suggestions?

Paliarg

I have a GPS receiver that outputs RTCM correctins over a serial port but only handles CTS-RTS flow control. I need to transform this flow control into an Xon Xoff type so I can connect it to another instrument that only works with Xon Xoff. I was told using an Arduino board could be possible to do it. Any suggestions?

Go Up