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.
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 IfExit_CmdConertJson_Click:Exit SubErr_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 ThenBeepMsgBox "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
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):
Microsoft AccessVBAGolang
Last Comment
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
8/22/2022 - Mon
John Tsioumpris
intPortID = 2 and you want Com4?
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
ASKER
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??
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.
Unlimited question asking, solutions, articles and more.
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
ASKER
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);
}
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
ASKER
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
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.