Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need advice on COM port Programming 1

Status
Not open for further replies.

Reaper2050

Programmer
May 25, 2012
12
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.


COMMAND 6 : OBTAIN LAST TRANSACTION DETAILS

COMMAND WORDS INTERPRETATION

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 :

REPLY WORDS INTERPRETATION
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.

Regards,
Adriaan
 
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
END

! 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
Q#=INSTRING('~y',CLIP(loc:DataBuffer),1,P#)
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'
IF S# THEN
loc:SerialNumber = loc:DataBuffer[P#+2:S#-1] ! get all values in buffer between ~x and , (will get 12345678)
END

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

etc..

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

Easy .... hope it helps.

Regards.

 
Thank you so much. It helps me alot.

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

Regards,
Adriaan Du Plessis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top