dcsimg
Results 1 to 2 of 2

Thread: Serial port com Rs 232 challenges in configurations

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2020
    Posts
    1

    Serial port com Rs 232 challenges in configurations

    I need help on how to send data to the serial port Rs 232 with the following features using VBA ( visual basic for application in Ms Access database)


    Below are the details of this machine and requirements for data packaging:

    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

    Below is my attempted VBA which failed to work


    Here is what I have done so far but still it does not work , but your help was very good and highly appreciated as it lead to the near solution , below is what I have done :

    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 strDataToSend As String
    Dim lngSize As Long
    intPortID = Forms!frmLogin!txtFinComPort.Value
    ' 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
    ElseIf lngStatus = 0 Then
    Beep
    MsgBox "The port is Open"
    End If


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

    ' Write data to serial port.
    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)
    L = Len(strData) 'Only length of content string. May be start, length and CRC should be added
    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)
    'next string is filled with command 2 - I'm not sure is it correct or not
    strTMP = Start & strLength & Chr(2) & strData
    strData = JsonConverter.ConvertToJson(transaction, Whitespace:=3) & Chr$(13)
    lngStatus = CommWrite(intPortID, strData)
    lngSize = Len(strData)
    If lngStatus <> lngSize Then
    Beep
    MsgBox "No data found"
    ' Handle error.
    End If

    ' Read maximum of 64 bytes from serial port.
    Dim Customers As Collection
    Dim Itemize As Variant
    lngStatus = CommRead(intPortID, strData, 14400)

    Set rs = db.OpenRecordset("tblEfdReceiptsPOS")
    If lngStatus > 0 Then
    Set Customers = ParseJson(strData)
    Z = 2
    ElseIf lngStatus < 0 Then
    Beep
    MsgBox "Please note that there is no data to read", vbOKOnly, "The Comm Port has no data"
    ' Handle error.
    On Error Resume Next
    End If
    ' Process data.
    For Each Itemize In Customers
    With rs
    .AddNew
    rs![TPIN] = Itemize("TPIN")
    rs![TaxpayerName] = Itemize("TaxpayerName")
    rs![Address] = Itemize("Address")
    rs![ESDTime] = Itemize("ESDTime")
    rs![TerminalID] = Itemize("TerminalID")
    rs![InvoiceCode] = Itemize("InvoiceCode")
    rs![InvoiceNumber] = Itemize("InvoiceNumber")
    rs![FiscalCode] = Itemize("FiscalCode")
    rs![TalkTime] = Itemize("TalkTime")
    rs![Operator] = Itemize("Operator")
    rs![Taxlabel] = Itemize("TaxItems")("TaxLabel")
    rs![CategoryName] = Itemize("TaxItems")("CategoryName")
    rs![Rate] = Itemize("TaxItems")("Rate")
    rs![TaxAmount] = Itemize("TaxItems")("TaxAmount")
    rs![TotalAmount] = Itemize("TaxItems")("TotalAmount")
    rs![VerificationUrl] = Itemize("TaxItems")("VerificationUrl")
    rs![INVID] = Me.ItemSoldID
    .Update
    End With
    Z = Z + 1
    Next

    rs.Close
    Set rs = Nothing
    Set db = Nothing
    Set Customers = Nothing

    ' Reset modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, False)
    lngStatus = CommSetLine(intPortID, LINE_DTR, False)

    ' Close communications.
    Call CommClose(intPortID)
    End Sub


    I have also failed to interpret or convert the given Code from the gadget manual for CRC see below:

    CRC details

    You also wanted the details for the CRC , kindly see below:
    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);
    }

  2. #2
    New Member
    Join Date
    Jun 2014
    Posts
    1

    Re: Serial port com Rs 232 challenges in configurations

    [QUOTE=nector;5448183]I need help on how to send data to the serial port Rs 232 with the following features using VBA ( visual basic for application in Ms Access database)

    See you are quite involved with EFD HAVE YOU TRIED THE V-EFD OPTIONS FROM INSPUR?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width