Some days ago I wrote about Comport for Firemonkey by WinSoft but even if you are not planning to use FireMonkey we still have a great component for you: Comport for Windows (TComport) by WinSoft.
Let’s download the trial version from here and install it (don’t forget to close RAD Studio before).
Features of TComport
- It’s easy to use and very powerful
- Can communicate with any device connected to the serial port using RS232 protocol
- It uses multithreading and overlapping for maximum performance
- Source code included in the registered version
- It’s part of Communication Protocol Suite
- Available for Delphi/C++ Builder 5 – 10.4 and Lazarus 2.0.10
Usage
The makers offer a good example of use but as always we will extend it a little and communicate with a GSM phone by reading full information using the phone internal modem.
I will be using a Samsung J3 for our tests and the phone will be connected to the computer by USB cable. After drivers install is finished we will see a modem in devices manager, in my case COM4, this being the serial port I will use to communicate with the phone.
Check out this step-by-step guide on how to download and use the ComPort DLL Library to make your developer life easy and increase your efficiency.
Let’s start the RAD Studio, open the demo project and run it. In order to communicate with the phone, we have to set up communications parameters and open communication.
Let’s add a TSpeedButton which will be used to send commands for reading the phone extended informations.
TComport has AfterOpenand AfterCloseevents, which are very useful in case we want to do something when the port is open or closed. The demo already contains a procedure named UpdateComInfo which enables or disables the buttons according to the Active property of the port. We will add our SpeedButtonPhoneInfo inside this procedure too.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
procedure TFormComPort.UpdateComInfo; begin if Visible then with ComPort do begin if Active then SpeedButtonOpenClose.Caption := 'Close' else SpeedButtonOpenClose.Caption := 'Open'; Memo.Enabled := Active; if Active then Memo.SetFocus; ComboBoxDeviceName.Enabled := not Active; SpeedButtonConfig.Enabled := not Active; SpeedButtonSetRTS.Enabled := Active; SpeedButtonClearRTS.Enabled := Active; SpeedButtonSetDTR.Enabled := Active; SpeedButtonClearDTR.Enabled := Active; SpeedButtonPhoneInfo.Enabled := Active; end; end; |
Now on the OnClick event of our SpeedButtonPhoneInfo, we will send to the phone all the commands that are needed to read the full info.
1 2 3 4 5 6 7 |
procedure TFormComPort.SpeedButtonPhoneInfoClick(Sender: TObject); begin ComPort.Write('AT'+#13#10,TEncoding.ANSI); Memo.Lines.Add('AT Command sent'); ComPort.Write('AT+DEVCONINFO'+#13#10,TEncoding.ANSI); Memo.Lines.Add('DEVCONINFO Command sent'); end; |
And here is the result:
But we see something strange, we see both commands sent but no answer and then we see answers to both commands, not very neat, so let’s correct this by adding a variable and two functions.
1 2 3 4 5 6 |
public { Public declarations } HaveAnswer : Boolean; function WaitForAnswer(aInterval:integer):Boolean; function SendCommand(aCommand:string; waitInterval:cardinal):Boolean; end; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
function TFormComPort.WaitForAnswer(aInterval:integer):Boolean; var aTick : cardinal; begin aTick:=TThread.GetTickCount; while TThread.GetTickCount-aTick<aInterval do begin if HaveAnswer then Break; TThread.Sleep(1); Application.ProcessMessages; end; Result:=HaveAnswer; end; function TFormComPort.SendCommand(aCommand:string; waitInterval:cardinal):Boolean; begin Result:=False; Memo.Lines.Add('---------------------------------'); HaveAnswer:=False; try ComPort.Write(aCommand+#13#10,TEncoding.ANSI); Memo.Lines.Add(aCommand+' Command sent'); Result:=WaitForAnswer(waitInterval); if not Result then Memo.Lines.Add('Phone not answer'); except on E:Exception do Memo.Lines.Add(E.Message); end; end; |
TComport has OnRxCharevent where we will set the HaveAnswer variable to true and in this way, we will know that phone has answered to our command (and before sending any command to the phone we will set the HaveAnswer variable to false).
1 2 3 4 5 6 7 8 |
procedure TFormComPort.ComPortRxChar(Sender: TObject); var Text: AnsiString; begin Text := ComPort.ReadAnsiString; Memo.SelText := string(Text); AddReadBytes(Length(Text)); HaveAnswer:=True; end; |
Now let’s update OnClick event for our SpeedButtonPhoneInfo with the new functions. I changed the Memo font size as the text was too big and set Memo.WordWrap to true, to can see the full phone info.
1 2 3 4 5 6 |
procedure TFormComPort.SpeedButtonPhoneInfoClick(Sender: TObject); begin HaveAnswer:=False; if SendCommand('AT',5000) then SendCommand('AT+DEVCONINFO',10000); end; |
And here is the new result:
So, if ComPort for Windows is the tool you need just go to WinSoft and and check out the full version!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Very very good but too expensive (the cheapest license cost is 120 US$).
Understood. I think, like anything, you have to balance the quality of a product against the cost and examine if it delivers value for money. To me, the price seems reasonable balanced against how long it would take me to write similar code, the fact the existing solution is probably well tried and tested, and any issues with it would be addressed by the supplier who has already done the research and become expert in the field. ¯\_(ツ)_/¯