Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Need advice on COM port Programming

Need advice on COM port Programming

Need advice on COM port Programming

Hi All,

I have a project where I have to communicate with a serial port. Sending and receiving to the serial port from a programming side is new to me.

I have purchased Capesoft WinEvent for sending to serial port.

What is the best why to accomplish the following.



F X Command next for MPD unit number X
Y 6 Y = Hose number, 6 = Command number
B 0 End of command

The above set of command words instructs the addressed hose on the addressed MPD unit to transmit details of the last completed transaction.

This command must be accepted for any hose status other than BUSY.

The MPD unit replies within 50 msec with the following data :

D X Reply next from MPD unit number X
E Y Y = Hose number

E P ~
E P | The price per unit in BCD format
E P | P = 0 to 9
E P ~

E M ~
E M |
E M | The money amount in BCD format
E M | M = 0 to 9
E M |
E M ~

E V ~
E V |
E V | The volume in BCD format
E V | V = 0 to 9
E V |
E V ~

E Z Z = Checksum
A 0 End of reply

The BCD digits in the above reply structure are identical to those on the MPD main display as they appeared immediately after the addressed hose completed its last transaction.

In all cases the least significant digit is transmitted first. By convention the decimal point positions are identical to those of the MPD main display.

Thanks in advance.


RE: Need advice on COM port Programming

Hello Adriaan,

Coding for Serial COM's can be difficult. I've done a lot of serial COM programming, talking to weighbridge scales, overhead projectors etc.
In my opinion (and there's probably way better coders than I) the best way to READ data from the serial port is to create a procedure for reading the serial port buffer and call the procedure in the 'Timer' embed of the window. Set the Window's timer value to 50.

Read the entire contents of the buffer on each timer event, and append the data read (if any) into a separate STRING Variable (ex. loc:DataBuffer) - leave the Winevent buffer alone, just concatenate the variable contents to your own buffer variable. You then check your buffer(loc:DataBuffer) for the beginning and end character(s) of a 'valid/complete' 'Data packet'... Since you know what the data 'packet' looks like (how many bates, where each piece of data begins and ends etc. inside your packet.)

Once you have valid packet, extract the data using INSTRING etc. and clear your buffer.
Simple example code:

! Some local Variables
loc:BytesRead Byte ! Just a byte type variable to catch the returnvalue of Winevent's 'ReadPort'
loc:RXBuffer STRING(1000) ! WinEvent's buffer which Readport will fill with whatever it gets from the serial port
loc:DataBuffer STRING(1000) ! My own data buffer

! Code to run on timer event

loc:BytesRead = ReadPort(PortId,loc:RXBuffer,0) ! Read EVERYTHING from serial port (returns QTY bytes read in loc:BytesRead variable)

IF loc:BytesRead THEN ! If P:bytesRead has a value then concatenate read data to my own buffer
loc:DataBuffer = CLIP(loc:DataBuffer) & CLIP(loc:RXBuffer) ! just add whatever is read from port to whatever I have in my buffer

! Inspect 'My Buffer' loc:Databuffer and see if you have a valid and complete packet.
! For example, say my packet start is '~x' and the packet end is '~y' : my packet ~x12345678,123,12345678,abc,123~y

IF LEN(CLIP(loc:DataBuffer)) THEN ! If there's data in 'my' buffer
P#=INSTRING('~x',CLIP(loc:DataBuffer),1,1) ! Search for beginning of packet.
IF P# THEN ! great, found beginning
IF Q# THEN ! great, found end
! Now you have the start position and end position of a packet.
! You can now slice out whatever data you want from the 'packet'
! For example: to cut the serialnumber out of the example packet:
S# = INSTRING(',',loc:DataBuffer,1,P#+2) ! look for comma, starting at character just after '~x'
loc:SerialNumber = loc:DataBuffer[P#+2:S#-1] ! get all values in buffer between ~x and , (will get 12345678)

P# = INSTRING(',',loc:DataBuffer,1,S#+1) ! look for next comma starting at character just after previous comma
loc:SerialNumber = loc:DataBuffer[S#+1:P#-1] ! get all values in buffer between , and , ( will get 123 )


! Once you have 'sliced' all the data from the packet, clear your buffer (loc:DataBuffer)

Easy .... hope it helps.


RE: Need advice on COM port Programming

Thank you so much. It helps me alot.

Hope you have a great day further and thanks again for the example.

Adriaan Du Plessis

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close