Link to home
Start Free TrialLog in
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScFlag for Zambia

asked on

Corrections required in order to communicate with RS 232 with MS Access VBA

The communication code VBA for RS 232 has failed, it cannot transmit any data to the Rs 232 gadget kindly help me to locate where the problem is. There are no errors whatsoever during transmit ion but the data does not go or written to the com port 4. The json part is okay and confirmed by the users I’m also able to see the results by using text file instead of on an Immediate window. The requirement here is to write data, send and receive data from rs 232 gadget.


Dim json As String
    Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
    Dim lngStatus As Long
    Dim strError  As String
    Dim strData   As String
    Dim lngSize As Long
    intPortID = 4
    ' Initialize Communications
    lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), _
        "baud=115200 parity=N data=8 stop=1")
    If lngStatus <> 0 Then
    ' Handle error.
        lngStatus = CommGetError(strError)
    MsgBox "COM Error: " & strError
    End If

    ' Set modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, True)
    lngStatus = CommSetLine(intPortID, LINE_DTR, True)

    ' Write data to serial port.
    strData = JsonConverter.ConvertToJson(transaction, Whitespace:=3)
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> lngSize Then
    ' Handle error.
    End If
Exit Sub
Resume Exit_CmdConertJson_Click

' Read maximum of 64 bytes from serial port.

Dim JSONS As Object

    lngStatus = CommRead(intPortID, strData, 14400)

Set rs = db.OpenRecordset("tblEfdReceipts")
    If lngStatus > 0 Then
' Process data.
Set JSONS = JsonConverter.ParseJson(strData)
    ElseIf lngStatus < 0 Then
MsgBox "There is no data to updated
        ' Handle error.
        On Error Resume Next
    End If
        ' Process data.
    Z = 2
  For Each item In JSONS
           With rs
            rs![TPIN] = item("TPIN")
            rs![TaxpayerName] = item("TaxpayerName")
            rs![Address] = item("Address")
            rs![ESDTime] = item("ESDTime")
            rs![TerminalID] = item("TerminalID")
            rs![InvoiceCode] = item("InvoiceCode")
            rs![InvoiceNumber] = item("InvoiceNumber")
            rs![FiscalCode] = item("FiscalCode")
            rs![TalkTime] = item("TalkTime")
            rs![Operator] = item("Operator")
            rs![Taxlabel] = item("TaxItems")("TaxLabel")
            rs![CategoryName] = item("TaxItems")("CategoryName")
            rs![Rate] = item("TaxItems")("Rate")
            rs![TaxAmount] = item("TaxItems")("TaxAmount")
            rs![VerificationUrl] = item("TaxItems")("VerificationUrl")
            rs![INVID] = Me.InvoiceID
        End With
        Z = Z + 1
      Set rs = Nothing
      Set db = Nothing
      Set JSONS = Nothing
    'Reset modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, False)
    lngStatus = CommSetLine(intPortID, LINE_DTR, False)

    'Close communications.
    Call CommClose(intPortID)

End Sub

Open in new window

Now I have this gadget with me .

Data Structure as per gadget


Field                                       Length(Byte)   Description
Header 1                                                   1       The first byte of package header 0x1A

Header 2                                                   1       The second byte of package header Ox5D

CmdID                                                           1       Command IDs:
                                                                    0x01 acquire the status of ESD
                                                                    0x02 invoice signing
                                                                    0x03 Error code

Length                                                     4       The length of the content, big-endian

Content                                                    ?       The Json based business data

CRC                                                     2       Two-Byte verification (CRC), it will be generated by bytes start from Header 1 up to content

The guide went on to say:

The contents oftheinterfaceprotocol includethreecommands (request/response), such as Get Interface Instruction for ESD and POS  Status, Invoice signing and error message. All the datawill beorganized in JSON formatstartingwith package header and endingwith checksum.It consists of Header, Command ID, Length of data, Content andVerification Code (CRC):
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

intPortID = 2 and you want Com4?
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc


Sorry about that still it does not send , help me to understand this part:

Kindly note here I'm trying to put the data into the port which of the above code is correct??

trData = JsonConverter.ConvertToJson(transaction, Whitespace:=3)

JsonConverter.ConvertToJson(transaction, Whitespace:=3) =  "COM" & CStr(intPortID)

JsonConverter.ConvertToJson(transaction, Whitespace:=3)=strData

Open in new window

I assume that it should be something like the original code you posted
strData = JsonConverter.ConvertToJson(transaction, Whitespace:=3)
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)

Open in new window

This also failed to work, could it be :

Set strData = JsonConverter.ConvertToJson(transaction, Whitespace:=3)

Open in new window

Why set...strData is a string not an Object
Use PortMon to check what is send forth and back to your device.

The description of the data structure shows, that you shouldn't send JSON to your device, but a TLV packet with a checksum.
Furthermore, what CRC exactly? Cause there are lot of different ones. Important piece is the IV.
Kindly see what I'm given below:

The CRC algorithm used here is follows:
unsigned short int cal_crc(unsigned char *ptr, unsigned int len)
unsigned char i;
unsigned int crc=0;
for(i=0x80; i!=0; i/=2)
Here I'm now advised to insert the starting position & ending charactor otherwise the device will not know where to start from . I have also confirmed its actually failing on the below code:

' Write data to serial port.
    strData = JsonConverter.ConvertToJson(transaction, Whitespace:=3)
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> lngSize Then
    ' Handle error.
    End If

Open in new window

Its able to open the gadget but writing is a problem because it wants the following:

Starting Data point
Ending Data point 

Open in new window

The length is allready there. This is where I have the challenges to expand the code
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Flag of Zambia image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial