I have been looking at Delphi this afternoon and dont seem to be able to find a control for Serial Port communication.
Does Delphi have a native control or do people usually roll there own?
http://sourceforge.net/projects/comport/
yep, that's the one. Tcomport is what i use as well.
I just installed it into Delphi XE1 right now (i usually uses Delphi 7)
This should also work for Delphi XE2
Tcomport is a bit strange in its installation compared to other Delphi components.
It has a designetime and runtime package to install. but its not hard to do.
Here is how i installed it.
Download the TComport component and extract the zip to a location where you want to store 3rd party components. It's best if this isn't part of the Delphi directory. i use d:\profile\programming\delphicomponents\tcomport\
Installing the runtime package
- file - open - cportlibdxe.dpk (its in the source folder of the stuff you unzipped)
- Rightclick on the CPortLibDXE.bpl entry in the delphi project manager and select compile
- Now you need to add that 'source' folder to the delphi library source list
- click - tools - options
- Select Environment options - delphi options - library
- Click the button for library path
- Click the yellow folder icon and select the source folder for Tcomport
- Click Add and then ok
Installing the designtime package
- Click File - Open DsgnCPortDXE.dpk
- Rightclick on the DsgnCPortDXE.BPL entry in the project manager and select COMPILE
- Now Rightclick on the DsgnCPortDXE.BPL entry again and select INSTALL
- Your finished. click - File - Close all (don't have to save)
Now you should have the components installed.
You can create a new project and the Tcomport components should be in the component pallet.
Project - add new project - VCL forms application
-Drop a Tcomport on your form
-Set its comport to com1 (or whatever) and the bitrate to what you want.
-Set its connected property to true.
-Add a button that does
comport1.transmitchar( chr(50) );
chr(50) just converts 50 from a number to a character
There are a few other ways to send data in buffers and stuff for more advanced applications but TransmitChar() is good to just get stuff working quickly.
Run your program and when you click the button it should send one byte over the serial port
To read data you can use the RxChar event for Tcomport.
an event handler might look like this
procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
Str: String;
begin
ComPort1.ReadStr(Str, Count); // read count number of bytes into the string str
memo1.text := memo1.text + str; // append data to a Tmemo. (You might want to convert it to hex though otherwise any ascii commands like
// tab or new line etc.. will cause that to happen on the memo.
end;