Hi
I have been following your quality answers on the serial virtual port for some time now, though based in excel I though that may be one can assist me also sorting out a similar serial virtual port based in Ms Access 2016.
The interface follows the same Module created by Mr David H.M, but the only difference is the requirement of using the checksum (CRC) in the send string. That is where I got complete stuck, below are the two VBA code to be used. For example, if I want to send the text called “Christopher” I will do it like below:
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 strDataToSend As String
Dim lngSize As Long
intPortID = 2
' 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)
strData = “Christopher”
' Write data to serial port.
lngStatus = CommWrite(intPortID, strData)
lngSize = Len(strData)
If lngStatus <> lngSize Then
On Error Resume Next
' Handle error.
End If
' Read maximum of 64 bytes from serial port.
Dim JSONS As Object
Dim Itemiz As Object
lngStatus = CommRead(intPortID, strData, 14400)
‘This is the table ( where “tblEfdReceiptsPOS”) I want to store the data received from the device
Set rs = db.OpenRecordset("tblEfdReceiptsPOS")
If lngStatus > 0 Then
Set JSONS = 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 Assuming I want to store in table tblEfdReceiptsPOS looks like the data below
For Each Itemiz In JSONS
With rs
.AddNew
![TPIN] = Itemiz("TPIN")
![TaxpayerName] = Itemiz("TaxpayerName")
![Address] = Itemiz("Address")
![ESDTime] = Itemiz("ESDTime")
![TerminalID] = Itemiz("TerminalID")
![InvoiceCode] = Itemiz("InvoiceCode")
![InvoiceNumber] = Itemiz("InvoiceCode")
![FiscalCode] = Itemiz("FiscalCode")
![TalkTime] = Itemiz("TalkTime")
![Operator] = Itemiz("Operator")
![Taxlabel] = Itemiz("TaxItems")("TaxLabel")
![CategoryName] = Itemiz("TaxItems")("CategoryName")
![Rate] = Itemiz("TaxItems")("Rate")
![TaxAmount] = Itemiz("TaxItems")("TaxAmount")
![VerificationUrl] = Itemiz("TaxItems")("VerificationUrl")
![INVID] = Me.ItemSoldID
.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
Display More
The above VBA Code is fine, no issues, the main challenges are on the device which we are now required to use. It has the following mandatory requirements:
Details of the device
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
Final String to be sent to this device
<Header1><Header2><CmdID><Length><Content><CRC>
Any idea on this people?