Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

building a checksum for serial connection

221 views
Skip to first unread message

Jason Lee

unread,
Mar 13, 2001, 8:19:24 PM3/13/01
to

I'm trying to build a checksum for a string I send over a serial port
connection (using MSComm). The checksum is suppose XOR every byte sent.
The data sent is suppose to contain both ascii characters (the command part)
and numeric data (the argument part:single and integer variables).

I have 2 questions:

- is it possible to send both string and numeric data? MSComm seems to send
everything as a string?

- how do I break a single (4 bytes) or integer (2 bytes) variable into 1
byte segments to do the Xor?

Jason.


Randy Birch

unread,
Mar 14, 2001, 12:00:44 AM3/14/01
to
I don;t know diddly about checksums, but we use a VB com routine to talk to
a lab testing instrument via the comm port, and the routine we successfully
use is:

Public Function SerialCommCalculateChecksum(InputStr As String) As String

'Sum the entire message block
Sum = 0
For n = 1 To Len(InputStr) Step 1
Sum = Sum + Asc(Mid(InputStr, n, 1))
Next

'Create first calculated checksum byte
SerialCommCalculateChecksum = Hex(ShiftRightInt((Sum And &HF0), 4))

'Create second calculated checksum byte
SerialCommCalculateChecksum = SerialCommCalculateChecksum & Hex(Sum And &HF)

End Function

Then its used in the code as:

send:
'Create a frame to transmit

'Add frame number to beginining of message string
framestr = Trim(Str(Framenumber)) & tmpstr

'Add ETX or ETB character to end of message string
If LastFrameFlag = True Then
framestr = framestr & ETX
Else
framestr = framestr & ETB
End If

'Calculate checksum on above string from F# to ETX/ETB character
'Append two byte checksum to string
tmpstr2 = SerialCommCalculateChecksum(framestr)
framestr = framestr & tmpstr2

'Add STX character to beginining of string
'Append CRLF characters to string to complete the frame
framestr = STX & framestr & vbCr & vbLf

'Transmit frame
SerialCommPort.Output = framestr


Seems to work ... to me its magic.


--

Randy Birch
MVP Visual Basic

Take the vb.net poll at:
http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/

Please respond only to the newsgroups so all can benefit.

"Jason Lee" <jaso...@iname.com> wrote in message
news:wGzr6.375021$Pm2.5...@news20.bellglobal.com...
:
: I'm trying to build a checksum for a string I send over a serial port

:
:


Dick_Grier

unread,
Mar 14, 2001, 12:17:17 PM3/14/01
to
Hi,

>>
- is it possible to send both string and numeric data? MSComm seems to send
everything as a string?
<<

By numeric, do you mean binary? If so, then you can convert binary data to
a string using the Chr function (use the Asc function to convert the
individual receive characters back to a byte). Or, you can set the
InputMode property to comInputModeBinary and use arrays of type Byte to
buffer transmit and receive data.

>>
- how do I break a single (4 bytes) or integer (2 bytes) variable into 1
byte segments to do the Xor?
<<

Rather than go into this here (I'd be guessing at what you really want to
do), I'd suggest that you might benefit from getting a copy of my book. I
show this sort of thing in detail. And, I have source code in the book for
my XMComm.ocx control, which implement XMODEM/checksum file transfers. You
can download the control itself from my homepage. See below for
information.

--
Richard Grier (Microsoft Visual Basic MVP)
Hard & Software
12962 West Louisiana Avenue
Lakewood, CO 80228
303-986-2179 (voice)
303-986-3143 (fax)
Author of Visual Basic Programmer's Guide to Serial Communications, 2nd
Edition ISBN 1-890422-25-8 (355 pages).
For information look on my homepage at http://www.hardandsoftware.net.
Use the Books link to order. For faster service contact the publisher at
http://www.mabry.com/vbpgser.


0 new messages