Question

Serial port read/write

Asked by: calinutz

I need complete code of writing to serial port.
This is what I need:

I need to send a string of hexa to the serial port and receive another string of hexa from there (and translate it later)
The string I need to send is:
\$f0\$41\$30\$4f\$f7
The received data will also be hexa, and I need to append it to a table in specific columns of a table (but that is not a problem for me, so don't bother)
The sending of the data will happen periodically (but that's also my problem to solve, not yours)

Please do not point me to various other links in experts-exchange or links to components... I've been there already. But sadly I have no experience in communicating with the serial port (not even using any third party component to do it).
So please give me working code (perhaps using a specific third party component)

I must give you here a list of freeware components that seem to work just fine with the serial port (but obviously they work for someone that has some experience with serial ports):
http://www.torry.net/pages.php?id=198
http://www.domis.de/cms/index.php?module=ContentExpress&func=display&ceid=8
http://www.ciemmesoft.com/release3/Forum_Componenti/Topic.asp?ID=972
http://sourceforge.net/projects/tpapro/



This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-04-05 at 15:43:59ID24296656
Tags

serial port

,

port communication

,

port

Topic

Delphi Programming

Participating Experts
1
Points
20
Comments
22

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Serial port
    How can I check the status of serial port whether it is open or closed?
  2. How to detect serial ports
    I want to populate a combo box with a list of serial ports actually available on a system. Any ideas? I thought about just attempting to open COM1-COM8 and looking at return code, but I also want to show any ports that have a friendly name.
  3. serial port
    I want to send signals to my pc's serial port. Is this possible in java ?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: MerijnBPosted on 2009-04-05 at 22:32:26ID: 24074515

What port, baud rate, handshake settings?

 

by: calinutzPosted on 2009-04-06 at 01:35:08ID: 24075261

port - COM1,
baudrate - 9600,
FlowControl - XOnXOff,
parity - none
StopBits - 1
ByteSize - 8

I am confused... I have no ideea how to manage this. I happen to have another software (without source) that does this communication but I do not seem to be able to make it work, and to get responses from the other side.

Maybe it's the way I send the data through, or maybe the way I read it... But I think mainly the problem it's my sending the string of hexa...

So another program called ZeitControl Windowed Einzel works like a charm for sending and receiving the hexa information... But I can't seem to get it done.
As I already said... the string to send looks like this:\$f0\$41\$30\$4f\$f7, and I don't even know if the sent text is normal to look like this... Because I don't know what the serial port wants to receive and what transmits forward, what does it look like...

All I know is that when I send this text to the serial port, on the terminal, only the hexa appears without "$" and "\", so:
I send: \$f0\$41\$30\$4f\$f7, and in the terminal appears:F0 41 30 4F F7, and then come the response of the machine on the other side of the serial port... also in hexa...
And I also have an option to see the communication (not hexa) and the response looks more intelligible.

The software I am talking about I believe is made and given by the manufacturer of the hardware, but we do not get any other support to develop our own communication software. So I am stuck here.
This is how it looks like:

http://www.softwarex.ro/zeitControl.JPG

I need to be able to do the same in my software. Send a hexa string and receive another hexa string.
As you can see the blue string is the sent string and the rest is the response. Also I need to send all those 6 strings, and receive responses for each one of them, then save them to a database table. All this mus be in a timer. That's all... I should not be so complicated...

Please help.

 

by: MerijnBPosted on 2009-04-06 at 02:54:50ID: 24075645

So you don't have any documentation on the protocol? Do you know how to parse the result?

 

by: calinutzPosted on 2009-04-06 at 03:54:11ID: 24075959

Nothing... I just know how the result should look like...
Like you saw there(in the picture) in hexview... I am sending a string of 5 hexa numbers... and I receive 73 hexa numbers.
If I uncheck the hexview, then intelligible information is visible...
I figure... if I know this, I could decode the hexa.
Is there any way of sending 5 hexa chars.... and just read the response? Is it more complicated than that?
If it is then tell me what should I be looking for...
But if it is possible to do the sending and receiving without anything else there... please tell me how can I do that?
I believe there must be a way of reading the response of the serial port... no matter what that would be... The problem is that I could not manage to get a response from the serial port... with any component...
Maybe it's the way I am sending the data to the serial port...? Maybe I do not know how to read from it?


Cand you give me a code that will send the above string to the serial port using any component  (freeware) ?
 And then receive the response from the serial port?
I made a scrrenshot of the Einzel software when sending the same string of hexa... viewed in hexa then viewed not in hexa...:
http://www.softwarex.ro/zeitControl2.JPG

So it is pretty clear to me that I must send a string of 5 hexa numbers to the serial port...
Then the serial port will reply with another string of hexa numbers. And when I convert the hexa cu ASCII, I will obtain my needed values.
Watch this:
In hex view
Send string:
F0 41 30 4F F7
Receive:
F0 61 20 30 20 30 2E 30 30 30 20 30 30 30 30 2E 30 30 20 30 30 30 30 30 30 30 31 2E 39 31 20 30 30 30 30 32 37 36 33 2E 34 38 20 2E F7

Conversion to ASCII:
Sent string:
ðA0O÷
Received string:
ða 0 0.000 0000.00 00000001.91 00002763.48 .

The interesting part is that when I use the zeitcontrol software the "ð" char is replaced with "-". That-s all the difference.

So the zeitcontrol software non-hex terminal display looks like this:
Sent string:
-A0O,
Received string:
-a 0 0.000 0000.00 00000001.91 00002763.48 .,

Basically I converted manually the hexa to ASCII and confronted with the non-hexview of the zeitcontrol software display

So actually I need to take the numbers generated after the conversion and enter them into a database.
But please help me obtain the result hex string, after sending the initial string...

It's like this: on the other side of the serial port there are 6 weighing machines that send back their measurements. So in the initial string that I am sending... there is probably an id of the machine and the command that tells it to reply with the measurements needed. The ID of the machine is probably the third and fourth hexa numbers from the string that I am sending (because only they differ)
Same way... in the result obtained, as you can see (in the translated string)
-a 0 0.000 0000.00 00000001.91 00002763.48 .,
"-a" stays the same, and the next number (in this case 0 ) represents the ID of the weighing machine... because for the other 5 input strings the resulted number will be 1,2,3,4,5

So this is what I discovered.
I am 99.99% sure that the reply from the machines is formated to use the exact same number of hexa numbers to represent their measurements (hence "0000.00").
So can you help me building the code that sends the string of hexa and receive the other string of hexa?
All the rest of the translation and the database is piece of cake.

Thank you so far

 

by: calinutzPosted on 2009-04-06 at 03:57:30ID: 24075975

Correction:
Conversion to ASCII:
Sent string:
ðA0O÷
Received string:
ða 0 0.000 0000.00 00000001.91 00002763.48 .÷

And the zeitcontrol software replaces 2 ASCII chars:
The "ð" with "-"
and the "÷" with ","

Not that this is important... i just corrected myself so you don't see incomplete information above

 

by: MerijnBPosted on 2009-04-06 at 04:36:39ID: 24076169

OK, I don't have a complete working example, but some quite useful stuff.

First of all, this is all quite simple, nothing to worry about.

I suggest you use this open source component: https://sourceforge.net/projects/comport/

See below a chunk of code which probably works all the way, but I can't test it (I don't have your device).
Come back with questions if you have any!

unit main;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, CPort, StdCtrls;
 
type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    fCPort: TComPort;
 
    procedure OnRxBuf(Sender: TObject; const Buffer; Count: Integer);
  public
    { Public declarations }
  end;
 
var
  Form2: TForm2;
 
implementation
 
{$R *.dfm}
 
procedure TForm2.Button1Click(Sender: TObject);
begin
 // write the 'magic string'
 fCPort.WriteStr(#$F0#$41#$30#$4F#$F7);
end;
 
procedure TForm2.FormCreate(Sender: TObject);
var s: string;
begin
 // create the comport component
 fCPort := TComPort.Create(Self);
 
 // configure it
 fCPort.BaudRate := br9600;
 fCPort.Parity.Bits := prNone;
 fCPort.DataBits := dbEight;
 fCPort.StopBits := sbOneStopBit;
 fCPort.FlowControl.XonXoffOut := true;
 fCPort.FlowControl.XonXoffIn := true;
 fCPort.Port := 'com1';
 
 // set the rx data event handler
 fCPort.OnRxBuf := OnRxBuf;
 
 // all set, now open the port
 fCPort.Open();
end;
 
procedure TForm2.OnRxBuf(Sender: TObject; const Buffer; Count: Integer);
begin
 // here you receive data, you can check if Count = 73 so you know you're data is all there
end;
 
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:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:

Select allOpen in new window

 

by: calinutzPosted on 2009-04-06 at 04:38:08ID: 24076175

Apparently I wasn't doing the job correctly.
I had to convert the sent string to a Binary Array and convert it to AnsiString before sending it.
Now it works like a charm.
Thank you for your time.
I will ask for deletion of the question and a refund.

 

by: calinutzPosted on 2009-04-06 at 04:39:58ID: 24076182

Sorry... I did not see your answer when I posted my comment.
If your code works I'll award you with the points even if I found my solution. I think it's only fair.
But only if your solution works ;)
Thank you again.

 

by: MerijnBPosted on 2009-04-06 at 04:42:04ID: 24076203

> But only if your solution works ;)

Big guess since I couldn't test it :p

 

by: calinutzPosted on 2009-04-06 at 04:45:39ID: 24076221

Any suggestions on reading the result string in the OnRxBuf event?
I would appreciate it.
I cannot test your solution without it. (I really don't know how to convert the serial port response to hexa or ascii)
I solved my problem using another freeware component called serialPortNG... and it returns a string already converted from hexa to ASCII... so all I need to do now is split it into pieces and write it in the database.

I am open to use your solution, or at least award you with the points for your time, but I will need a full solution for sending the input string and receiving the result string.

 

by: calinutzPosted on 2009-04-06 at 04:51:40ID: 24076281

Here's my code... very easy (I'm angry with myself for not solving it sooner)
// all the settings of my port are being done in Object inspector since I only need this code to work on a specific port, with specific settings, so I do not setup the components properties in the code.

procedure TForm1.Button1Click(Sender: TObject);
var
  BinArr: array of Byte;
begin
  SetLength(BinArr, 5);
  BinArr[0]:=240;
  BinArr[1]:=65;
  BinArr[2]:=48;
  BinArr[3]:=79;
  BinArr[4]:=247;
//240 65 48 79 247 that represent the decimal values of the hexa string
  SerialPortNG1.Active:=True;
  SerialPortNG1.SendString(AnsiString(BinArr));
  SerialPortNG1.ReadRequest:=True;
end;

procedure TForm1.SerialPortNG1RxClusterEvent(Sender: TObject);
begin
Memo1.Lines.Add(SerialPortNG1.ReadNextClusterAsString)
end;



And this is all... My memo receives the result like this
ða 0 0.000 0000.00 00000001.91 00002763.48 .÷

So it's perfect for me now.
But I repeat... If you give me the code that works with the other component... I will reward you with the points

 

by: MerijnBPosted on 2009-04-06 at 05:06:13ID: 24076392

Since you already receive a string, so this should do:

procedure TForm2.OnRxBuf(Sender: TObject; const Buffer; Count: Integer);
var Data: string;
begin
 SetLength(Data, Count);
 CopyMemory(@Data[1], @Buffer, Count);
 Memo1.Lines.Add(Data);
end;
                                              
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window

 

by: calinutzPosted on 2009-04-06 at 05:45:18ID: 24076733

Your code shows me this error...:

"COM port error. Unable to get async status on COM1."

?!?!?!?

Actually I had to modify the code a little because it's not working as you wrote it. Here is the code changed:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ComPort1.WriteStr(#$F0#$41#$30#$4F#$F7);
end;
 
procedure TForm1.ComPort1RxBuf(Sender: TObject; const Buffer: PAnsiChar;
  Count: Integer);
var Data: string;
begin
 SetLength(Data, Count);
 CopyMemory(@Data[1], @Buffer, Count);
 Memo1.Lines.Add(Data);
end;

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window

 

by: calinutzPosted on 2009-04-06 at 05:47:22ID: 24076747

Having the component set up on design time...
This is the dfm

object Form1: TForm1
  Left = 366
  Top = 178
  Width = 524
  Height = 398
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 56
    Top = 60
    Width = 457
    Height = 305
    Lines.Strings = (
      'Memo1')
    TabOrder = 0
  end
  object Button1: TButton
    Left = 56
    Top = 32
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object ComPort1: TComPort
    BaudRate = br9600
    Port = 'COM1'
    Parity.Bits = prNone
    StopBits = sbOneStopBit
    DataBits = dbEight
    Events = [evRxChar, evTxEmpty, evRxFlag, evRing, evBreak, evCTS, evDSR, evError, evRLSD, evRx80Full]
    FlowControl.OutCTSFlow = False
    FlowControl.OutDSRFlow = False
    FlowControl.ControlDTR = dtrEnable
    FlowControl.ControlRTS = rtsDisable
    FlowControl.XonXoffOut = True
    FlowControl.XonXoffIn = True
    OnRxBuf = ComPort1RxBuf
    Left = 172
    Top = 8
  end
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:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:

Select allOpen in new window

 

by: MerijnBPosted on 2009-04-06 at 05:52:16ID: 24076791

Ah, that's probably since I don't use the last version on the website :)

As far as I'm concerned you don't have to continue your search if you already have a working solution, only if you're interested in it yourself...

 

by: calinutzPosted on 2009-04-06 at 06:36:50ID: 24077136

If you still want the points I can ask you to help me forward with my code and give you the points for the next help...
That is... I am struggling now with sending and receiving.
So In the end I need to do the following:
STEP1: Send hex string \$f0\$41\$30\$4f\$f7
STEP2: Receive the answer and place it somewhere
STEP3: Send another hex string \$f0\$41\$31\$4e\$f
STEP4: receive the answer and place somewhere
... and so on.
(I have 6 hex strings to send, so I need to obtain 6 results in connection with the sent string)
Basically I have a stringgrid with 2 columns I need to place the input string on first column and the received string on the second column, for each 6 input strings.
I have difficulties (probably setting up the component) with the received string.
I need to wait until a full response has arrived before sending the next string.

Can you help here?
I'll attach the project in a zip file and you-ll get in the zip file a picture of the result of the execution.
Are you interested?




These steps must

 

by: MerijnBPosted on 2009-04-06 at 07:11:15ID: 24077430

You said you have this working with the other serial component, didn't you?

So it's just a matter of keeping track where you are in the process, or am I mistaken?

 

by: calinutzPosted on 2009-04-06 at 08:35:58ID: 24078417

That is my problem actualy. Since I don't seem to know when is the received string complete... I mean when to start the next sending of string.
Can you assist me? I could send you my sourcecode and you could look at it. Especially at the component... there has to be a way to know when the receiving is over.
I even tried to stop the port and start it again for the next string, but I am doing something wrong. Perhaps you have a more rested mind.

The components  use are: KStringGrid and SerialPortNG. I'll include them also in the zip, together with a screenshot of the result. Give me an email address

 

by: MerijnBPosted on 2009-04-06 at 08:43:00ID: 24078504

It's not really useful to sent me the sources since I can't test anything here (I don't have your device).

But I don't really see the problem though: you know how much data you expect back after each string you sent.
You can simply wait until you receive all 73 bytes before you sent the next command?

 

by: calinutzPosted on 2009-04-06 at 09:02:35ID: 24078706

:(
This is exactly where I need your help.
How to wait until I receive all the bytes?
Considering that the component receives the response through this procedure:

SerialPortNG1.ReadNextClusterAsString

?
Perhaps you can see something I don't...
Maybe your solution could do the work better than my solution.
All you need to do is analyze my code (meybe find something I did not), and maybe suggest something... maybe another approach. Perhaps your component works better than mine, offers more info to work with.
I would attach the code here in a zip file but it does not let me, this is why I was asking for your email address.
I'm waiting... :)

 

by: MerijnBPosted on 2009-04-06 at 09:04:49ID: 24078736

send it to merijnb@xs4all.nl

 

by: calinutzPosted on 2009-04-06 at 16:09:31ID: 24082664

I solved my problem using a timer.
Thank you anyway.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...