Host Engineering Forum
General Category => Do-more CPUs and Do-more Designer Software => Topic started by: DoMore4WxStations on February 10, 2016, 09:13:22 PM
-
Currently I am working to receive in a Serial String from a depth
sensor and calculate the checksum.
9600 Baud 8N1
The packet is a fixed length of 18 bytes as follows
02 30 30 3B 31 39 39 34 3B 32 30 30 3B 36 41 0D 0A 03
the 31 39 39 34 represents a measurement of 1994 mm
the 36 41 represents a 2 character checksum of the data packet that
is a two's complement of the data packet sum including the control characters
the Least significant byte is used resulting in a 2 character checksum
02 + 30 + 30 + 3B + 31 + 39 + 39 + 34 + 3B + 32 + 30 + 30 + 3B + 0D + 0A + 03 = 0x296
Use last byte only ( 96 ) and calculate the two's complement
100 - 96 = 6A
the 6A is represented in the packet in Ascii format as the 36 41
What I am looking to do is to have the Do-More receive the data in on
intserial port, wait till the 18 byte packet arrives, verify the checksum,
then convert the 4 byte ASCII represented distance measurement into an integer
value.
I have a few years of embedded systems programming experience in C but
am new to the Do-More PLC as of Saturday morning.
Is there a C compiler for the Do-More as I am a lot more experienced than with
Ladder Logic.
The Do-More works really well for the application and I am blown away
with how powerful it is as compared to the SLC-500 or PLC5 that I worked
with a few years back
Thanks
-
In ASCII form, it appears your packet is in the form
<STX>00;1994;200;<chk1><chk2><CR><LF><ETX>
Framing is important. Luckily, this protocol has a very good set of delimiters for both framing and field parsing.
Each packet/frame starts with <STX> (0x02) and ends with <ETX> (0x03) (actually <CR><LF><ETX> (0x0D 0x0A 0x03))
The fields are delimited by semicolons (0x3B), and your value is in the 2nd field.
Does this make sense?
-
It's also important to point out that the checksum is a 2 digit hexadecimal value in its ASCII form, not binary form. You did a great job of ascertaining that CRITICAL tidbit.
-
Here's the pieces parts you could use to do the work. Because of the STX/ETX framing I'm assuming the packet can be variable length, so the safe way forward is to find the end delimiter and work backwards through the bytes. And because there's a very sequential nature to this work, using Stages will help you maintain your sanity.
STREAMIN will get the serial port data into a String in the PLC.
STRFIND can locate the <CR><LF><ETX>.
use STRSUB to extract the previous two bytes into a String.
use STR2INT to stash the checksum from that String in an integer.
use STRDEL to remove the two byte checksum from the String.
use CHECKSUM w/ CheckSum:8 on the String to calculate the checksum value.
compare checksum values.
use STRFIND to locate the first semicolon;
use STRSUB to get the data bytes
use STR2INT to convert that string to an integer.
-
If it sends continually, do this...
Create a program block.
Create an unsigned char data block..Buff
Stage 0 - Idle
if IntSerial.InQueue != 0 JMP Stage 1
Stage 1 - Read Packet
STREAMIN to Buff, using exact match delimiter of 0x0D 0x0A 0x03, JMP Stage 2 on Success, JMP to Stage 10 on Error
Stage 2 - Calc Checksum
MATH Buff20 = 0 - (SUMR(Buff0, 13) + SUMR(Buff14, 3))
JMP Stage 3
Stage 3 - Extract Data
MEMCLEAR SS0, 4 elements
STRPUTB SS0 Index = 0, Count = 2, Buff1
STR2INT Dec V0 SS0 (if decimal...)
STRPUTB SS1 Index = 0, Count = 4, Buff4
STR2INT Dec V1 SS1 (if decimal)
STRPUTB SS2 Index = 0, Count = 3, Buff9
STR2INT Dec V2 SS2 (if decimal)
STRPUTB SS3 Index = 0, Count = 2, Buff13
STR2INT Hex Buff21 SS3 (appears to be hex)
If Buff20 == Buff21 JMP to Stage 4 else Stage 10
Stage 4
Data is good...stored in V0, V1, and V2...use and JMP Stage 0
Stage 10
Ring up an error and JMP Stage 0
-
Thank you everyone for helping out and getting back so quickly.
I will try to get the data parsing and checksum calculations running
this week.
Regards :)