Communicating with COM Ports

I am not much that familiar with Delphi but I was wondering if any of you could help me out in this problem.

I was wondering if I could be detailed about how I can connect to the COM ports of the computer. For example I have a router connected to a COM Port. How can I connect to that port and read/send data and so on. Or I have a software that communicates with the COM port which is connected to an equipment. How can I connect to that port and read/send data through it.

I know this is going to be a little complex one. But if it isn't a bother, could you kind of help me out with some explanations.

To connect via serial port (commport), first you need to get a serial port component, and add that to your project. I'm sorry, but I can't find a link to one right now...(I could send one to you if you would like that)

When a serialport component is available, use these steps to connect/send ansd read.
- Initialize the serialport component, set the baudrate, commport to use, parity type, stopbits, databits. (If you don't know what they are, please do some research on the serialport part)
- Open the commport (this should be a procedure native to the serialport component)

- when the commport is open, you can start sending data. Usually this happens by sending the pointer to a datastream to a sendata procedure, along with the size of the data to send.
- The receiving of data is almost the same, but now you receive a pointer to the start of the received data, and the SerialPort.BytesReceived property will tell you how many bytes have been received. So it's just a matter of "walking" through the string, until you've read all bytes.

- You should of course check the data before you do anything with it, so it would be good to check if the amount of bytes received is the amount you expected, you can verify if the checksum is good, and you can verify the control part of the data (if available).

Hope this was any help, there is a lot to tell, but these were the bare essentials I guess...

{---------------------------------------------------------------------------
Name : TSerialPort.GetData: PChar;
Function : Gets 'BytesInQueue' bytes of data from the commport. To do this
First the comm-errors are cleared, then it checks to see if there
are bytes waiting in the queue. Then the length is determined.
Then it actually reads the data.
Input : -
Output : PChar - Character pointer pointing to the memory location where
the read character was stored (read buffer)
---------------------------------------------------------------------------}
function TSerialPort.GetData: PChar;
var
BytesInQueue: longint;
oStatus: TComStat;
NumBytesRead, dwErrorCode: DWord;
begin
fByteReceive := 0;
Result := nil;
if hCommPort = INVALID_HANDLE_VALUE then exit;
// Get the total number of bytes that
// are waiting to be read from the
// input buffer.
ClearCommError(hCommPort, dwErrorCode, @oStatus);
BytesInQueue := oStatus.cbInQue;
if BytesInQueue > 0 then
begin
SetLength(ReadBuffer, BytesInQueue); // + 1 is deleted 18-1-2002
ReadFile(hCommPort,
PChar(ReadBuffer)^,
BytesInQueue,
NumBytesRead,
nil);
SetLength(ReadBuffer, BytesInQueue);// // + 1 is deleted 18-1-2002
end;
fByteReceive := BytesInQueue;
Result := PChar(ReadBuffer);
end;

{---------------------------------------------------------------------------
Name : TSerialPort.Create();
Function : Creates the serialport object, and sets all parameters and
properties to their default settings.
Input : -
Output : -
---------------------------------------------------------------------------}
constructor TSerialPort.Create();
begin
inherited;
// Initalize the handle to the port as
// an invalid handle value. We do this
// because the port hasn't been opened
// yet, and it allows us to test for
// this condition in some functions,
// thereby controlling the behavior
// of the function.

{---------------------------------------------------------------------------
Name : TSerialPort.Initialize_DCB;
Function : Initialize the device control block. Sets all properties needed
for serial communication. Every serialport settings change calls
this procedure
Input : -
Output : -
---------------------------------------------------------------------------}
procedure TSerialPort.Initialize_DCB;
var
MyDCB: TDCB;
begin
// Only want to perform the setup
// if the port has been opened and
// the handle assigned.
if hCommPort = INVALID_HANDLE_VALUE then exit;

// The GetCommState function fills in a
// device-control block (a DCB structure)
// with the current control settings for
// a specified communications device.
// (Win32 Developers Reference)
// Get a default fill of the DCB.
GetCommState(hCommPort, MyDCB);

// The 'flags' are bit flags,
// which means that the flags
// either turn on or off the
// desired flow control type.
case fFlowControl of
// first clear all the RTS,DTR,XOFF bit and the set it on again!
fcHARDWARE: MyDCB.Flags := (MyDCB.Flags and $FFFFCC03) or
(DTR_CONTROL_ENABLE shl 4) or (RTS_CONTROL_TOGGLE shl 12);
fcNone:; // Do not change a bit.
else
raise Exception.Create(' No case else exception');
end;

if fStripNullChars then inc(MyDCB.Flags, $0022);

MyDCB.XONChar := char(fXONChar);
MyDCB.XOFFChar := char(fXONChar);

// The XON Limit is the number of bytes that the data in the receive buffer
// must fall below before sending the XON character, there for resuming the
// flow of data
MyDCB.XONLim := fXONLim;
// The XOFF limit is the max number of bytes that the receive buffer can
// contain before sending the XOFF character, therefore stopping the flow
// flow of data
MyDCB.XOFFLim := fXOFFLim;

// Character that signals the end of file.
if fEOFChar <> 0 then MyDCB.EOFChar := char(EOFChar);

{---------------------------------------------------------------------------
Name : TSerialPort.SetXONChar(Value: byte);
Function : Set the XON Char. The XON character is the character that is
generated to start the flow of data when the buffer is ready
to receive more data
Input : value - the new XON character
Output : -
---------------------------------------------------------------------------}
procedure TSerialPort.SetXONChar(Value: byte);
begin
if fXONChar <> Value then
begin
fXONChar := Value;
Initialize_DCB;
end;
end;

{---------------------------------------------------------------------------
Name : TSerialPort.SetXOFFChar(Value: byte);
Function : Set the XOFF Char. The XOFF character is the character that is
generated to stop the flow of data, because the buffer is full
Input : Value - the new XOFF character
Output : -
---------------------------------------------------------------------------}
procedure TSerialPort.SetXOFFChar(Value: byte);
begin
if fXOFFChar <> Value then
begin
fXOFFChar := Value;
Initialize_DCB;
end;
end;

{---------------------------------------------------------------------------
Name : TSerialPort.SetXONLim(Value: word);
Function : Set the XON Limit. The XON Limit is the number of bytes that
the data in the receive buffer must fall below before sending
the XON character, there for resuming the flow of data.
Input : Value - the new limit for XON
Output : -
---------------------------------------------------------------------------}
procedure TSerialPort.SetXONLim(Value: word);
begin
if fXONLim <> Value then
begin
fXONLim := Value;
Initialize_DCB;
end;
end;

{---------------------------------------------------------------------------
Name : TSerialPort.SetXOFFLim(Value: word);
Function : Set the XOFF Limit. The XOFF limit is the max number of bytes
that the receive buffer can contain before sending the XOFF
character, therefore stopping the flow of data.
Input : Value - the new limit for XOFF
Output : -
---------------------------------------------------------------------------}
procedure TSerialPort.SetXOFFLim(Value: word);
begin
if fXOFFLim <> Value then
begin
fXOFFLim := Value;
Initialize_DCB;
end;
end;

{---------------------------------------------------------------------------
Name : TSerialPort.PortIsOpen: boolean;
Function : Public function to check if the commport is open. Checks if the
handle to the commport is initialized ok
Input : -
Output : true - port is open if the hCommPort (handle) is something
other then INVALID_HANDLE_VALUE
false - hCommPort (handle) is same as INVALID_HANDLE_VALUE
---------------------------------------------------------------------------}
function TSerialPort.PortIsOpen: boolean;
begin
Result := hCommPort <> INVALID_HANDLE_VALUE;
end;

A lot of questions regard threads in Delphi.
One of the more specific questions is how to show progress of the thread.
Updating a progressbar from inside a thread is a mistake.
A solution to this would be to send a synchronized message to the…

This article explains how to create forms/units independent of other forms/units object names in a delphi project.
Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…

Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen.
Visualize your data! ... really see it
To use the code to create a calendar from a q…