Configuring the serial Port in Ms Access (1 Viewer)

nector

Member
Local time
Today, 14:57
Joined
Jan 21, 2020
Messages
367
Dear All;
We have a device we want to use for our sales accounting processing for 2020 and below is the description of how it works and parameters to use to send and receive data from the same:

The contents of the interface protocol include three commands (request/response), such as Get
Status, Invoice signing and error message. All the data will be organized in JSON format starting with package header and ending with checksum. It consists of Header, Command ID, Length of data, Content and Verification Code (CRC):

Detailed description of the device:

Field
Header1 = 1 Length Byte (The first byte of package header 0x1A
Header2 = 1 Length Byte (The first byte of package header 0x5D
CmdID = 1 Command IDs : (0x01 acquire the status of the device, 0x02 invoice signing, 0x03 Error code)
Length = 4 The length of the content, big-endian
Content = ? Data to be sent
CRC = 2 (Two-Byte verification (CRC), it will be generated by bytes start from Header to Content)


In trying to accomplish that I have constructed the VBA code below which is appear not working and that is where your input is required:

Code:
Dim Start As String
Dim strTMP As String
Dim strLength As String, L As Long, L1 As Integer, L2 As Integer, L3 As Integer, L4 As Integer
Start = Chr(&H1A) & Chr(&H5D)
strData = JsonConverter.ConvertToJson(transaction, Whitespace:=3) & Chr$(13)
L = Len(strData) 'Only length of content string. May be start, length and CRC should be added          L = Len(strData) + 9 (or 7 without CRC)
L1 = L Mod 256
L2 = (L - L1) / 256 Mod 256
L3 = ((L - L1) / 256 - L2) / 256 Mod 256
L4 = (((L - L1) / 256 - L2) / 256 - L3) / 256 Mod 256
strLength = Chr(L4) & Chr(L3) & Chr(L2) & Chr(L1)    
strTMP = Start & strLength & Chr(2) & strData
strTMP = strTMP & strData
' function for CRC calculation see below
strTMP =  strTMP & CRC(strTMP)
lngStatus = CommWrite(intPortID, strTMP)’This is code that will bandle strTMP & CRC(strTMP)

Code:
CRC VBA Code:

Option Compare Database
Option Explicit

Public Function CRC8(ByVal AppID As String) As String
    Dim CRC8 As Byte
    Dim i As Integer
    Dim J As Integer
    Dim AppIDarray() As Byte  '<--- explicitly dimensioned as a Byte array to avoid confusion
    Dim aidLength As Long

    CRC8 = &HC7

    'The AppID is actually bytes stored in hexadecimal in a string. You have to convert them back to bytes before you can run a crc8 on them.
    AppIDarray = HexToByte(AppID)
    aidLength = UBound(AppIDarray)
            For J = 0 To aidLength
                CRC8 = CRC8 Xor AppIDarray(J)
                For i = 1 To 8
                    If CRC8 And &H80 Then
                     'masking off the left-most bit before shifting prevents the Overflow error.
                     CRC8 = ((&H7F And CRC8) * 2) Xor &H1D
                    Else
                     CRC8 = CRC8 * 2
                    End If
                Next i
            Next J
    CRC8 = CRC8
End Function
Public Function HexToByte(strHex As String) As Byte()
    Dim i As Integer
    Dim tempByte As Byte
    Dim outBytes() As Byte
    ReDim outBytes(Len(strHex) \ 2 - 1)
    For i = 0 To Len(strHex) \ 2 - 1
    Dim J As Integer
    Dim char As String
        For J = 0 To 1
           char = Mid(strHex, i * 2 + J + 1, 1)
            Select Case char
                Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
                    tempByte = tempByte Or (Asc(char) - 48)
                Case "A", "B", "C", "D", "E", "F":
                    tempByte = tempByte Or (Asc(char) - 55)
            End Select
            If J = 0 Then
                tempByte = tempByte * 2 ^ 4
            Else
                outBytes(i) = tempByte
                tempByte = 0
            End If
        Next
    Next
    HexToByte = outBytes
End Function

Your effort to resolve this issue will be highly appreciated.
 

jdraw

Super Moderator
Staff member
Local time
Today, 07:57
Joined
Jan 23, 2006
Messages
15,379
I have approved your post which had been in a "moderated" status. This will make it available to all forum members.
You might wish to tell readers more about your proposed application. You should also be aware that there are not many json/access/vba threads.
Isladogs has posted json related info in the forum.
Good luck with your project.
 

Users who are viewing this thread

Top Bottom