Du bist hier: Snippet-Verzeichnis » Visual Basic (78)
Sprache:

Compute ModBus LRC

Sprache: English
Programmiersprache: Visual Basic
Veröffentlicht von: rsukach [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1691


Beschreibung

Computes the LRC checksum used by the modbus protocol.

Code

1 ' <RMS> 09.21.2000 2 ' The error handler code in this routine is non-standard and will 3 ' have to be modified before inclusion in your proggie 4 ' 5 ' This routine relies on the LongToHexStr function, which I have also 6 ' posted to the vb snippets 7 ' 8 ' enjoy! 9 10 ' *********************************************************** 11 ' <RMS> 06.07.2000 12 ' This function computes the LRC checksum used to verify 13 ' that a valid modbus message has been received. 14 ' 15 ' Per the documentation, the LRC is computed by summing the 16 ' successive 8 bit bytes of the message (discarding carries) 17 ' then 2's complimenting the result. It is preformed on all 18 ' characters after the ":" and before the CrLf 19 ' 20 ' *********************************************************** 21 Public Function CalculateLRC(ByVal sMessage As String) As String 22 On Error GoTo me_err 23 24 Dim sPiece As String ' hex string 25 Dim nSum As Integer 26 Dim nStart As Integer ' start location for mid function 27 Dim nLength As Integer 28 Dim i As Integer 29 30 ' init locals 31 nSum = 0 32 nStart = 1 33 nLength = Len(sMessage) 'get length of sMessage to check for end 34 35 'summing all bytes 36 Do Until (nStart > nLength) 37 sPiece = "&H" & Mid(sMessage, nStart, 2) 'get 2 hex char and prepend &H 38 nSum = (nSum + CLng(sPiece)) And &HFF 'add to previous sum then AND with &HFF 39 nStart = nStart + 2 'to remove overflow bit 40 Loop 41 42 ' 2's compliment of result (flip the bits then add 1) 43 nSum = &HFF - nSum 44 nSum = (nSum + &H1) And &HFF 45 46 ' must be 1 byte 47 CalculateLRC = LongToHexStr(nSum, 1) 48 49 me_exit: 50 Exit Function 51 me_err: 52 HandleError Err, "modModBus:CalculateLRC", geErrSevMed 53 GoTo me_exit 54 End Function 55

Ein Kommentar

1

Welchen Browser benutzt du denn?

Ich mein, ich hätte sowas hier schonmal gelesen...

Montag, 1. Januar 0001 00:00:00 von Search engine optimization

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS