We help IT Professionals succeed at work.

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

Medium Priority
33 Views
Last Modified: 2020-01-17
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.

VBA CODE RS 232

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_CmdConertJson_Click:
Exit Sub
Err_Handler:
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
Beep
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
         
            .AddNew
            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
            rs.Update
        End With
        Z = Z + 1
    Next
      
      rs.Close
      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

<Header1><Header2><CmdID><Length><Content><CRC>

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):
Comment
Watch Question

John TsioumprisSoftware & Systems Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
intPortID = 2 and you want Com4?
CERTIFIED EXPERT

Author

Commented:
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??

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

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

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

Open in new window

John TsioumprisSoftware & Systems Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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

CERTIFIED EXPERT

Author

Commented:
This also failed to work, could it be :

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

Open in new window

John TsioumprisSoftware & Systems Engineer
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Why set...strData is a string not an Object
ste5anSenior Developer
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT

Author

Commented:
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;
while(len--!=0)
{
for(i=0x80; i!=0; i/=2)
{
if((crc&0x8000)!=0)
{
crc*=2;
crc^=0x18005;
}
else
{
crc*=2;
}
if((*ptr&i)!=0)
crc^=0x18005;
}
ptr++;
}
return(crc);
}
CERTIFIED EXPERT

Author

Commented:
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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.