Com Serial Port Challenges (1 Viewer)

nector

Member
Local time
Today, 21:24
Joined
Jan 21, 2020
Messages
368
I need help to convert the below information into VBA , I tried to fit the information into the two codes below but no lucky.

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



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

Field Length(Bytes) Descriptions

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 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);

}



I have already converted my data in Json format using the Json serializer and also Deserialized will be used for data that will be received.


Code:
  Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4

    Dim lngStatus As Long
    Dim strError  As String
    Dim strData   As String
    ' Initialize Communications
    lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), _
        "baud=9600 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.
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> lngSize Then
                ' Handle error.
    End If


    ' Read maximum of 64 bytes from serial port.
    lngStatus = CommRead(intPortID, strData, 64)

    If lngStatus > 0 Then

        ' Process data.

    ElseIf lngStatus < 0 Then

        ' Handle error.

    End If

    ' Reset modem control lines.

    lngStatus = CommSetLine(intPortID, LINE_RTS, False)

    lngStatus = CommSetLine(intPortID, LINE_DTR, False)


    ' Close communications.

    Call CommClose(intPortID)
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:24
Joined
Feb 28, 2001
Messages
27,146
You should be able to search this forum for the subject JSON because I recall seeing it pop up several times. I cannot help you personally because I did not deal with JSON before I retired. Thus I have no expertise in it. But I do remember seeing the topic.
 

vba_php

Forum Troll
Local time
Today, 13:24
Joined
Oct 6, 2019
Messages
2,880
nector,

can I ask what exactly you're doing here? Based on the title of this thread, I'm confused a bit. I would suspect that you could google search for code converters that do what you want to do. Also, this little snapshot of my website explains how virtually everything, regardless of language and file format, are very similar in this programming world of ours. Maybe it can help out a little, in your effort of conversion?

 

Attachments

  • explanation_of_json_and_xml.jpg
    explanation_of_json_and_xml.jpg
    80.7 KB · Views: 495

nector

Member
Local time
Today, 21:24
Joined
Jan 21, 2020
Messages
368
Sorry Sir;

May be I'm not clear here my apology sorry. I'm here to seek help on how to interface the access database with the rs 232 virtual serial port by using the Rs 232 Bas attached with also the two command VBA codes inserted below:

The problem here is that the device require the data to be sent to it in the format like below:

<Header1><Header><CmdID><Length><Content><CRC>

For Json I have already done it in advance its fine is only this interface issue which is giving some problems, I'm sorry to disturb you again.

Regards

Chris
 

Attachments

  • RS232 Bas.txt
    28.4 KB · Views: 448
  • Rs232 commands.txt
    1.2 KB · Views: 526

vba_php

Forum Troll
Local time
Today, 13:24
Joined
Oct 6, 2019
Messages
2,880
first of all Chris, regarding your comment:
I'm sorry to disturb you again.
would you please read this? https://www.access-programmers.co.u...icrosoft-access-2013-from.309139/post-1665267

that will tell you exactly why saying that is unproductive and invalid. Just like NauticalGent once here told me not to sing the "sad song" when answering questions. He was saying not to be hard on myself. Same thing applies to you when you think you're bothering people. So now, to the point....

your text files basically contain constants, UDT's and APIs. and I think I saw in there the code that was similar to what you posted originally in this thread via your question. But honestly man, I still am not sure what you're doing. You say in your question:
I need help to convert the below information into VBA , I tried to fit the information into the two codes below but no lucky.

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):
I can follow most of that, and the categorization you're referring to. For instance, I did CRC work at Collins Aerospace years ago. But what about this conversion you're talking about? a snippet from your question, in terms of description was:
<Header1><Header2><CmdID ><Length ><Content><CRC>

Field Length(Bytes) Descriptions

Header 1 1 The first byte of package header 0x1A

Header 2 1 The second byte of package header Ox5D
are you asking for THAT to be converted into VBA code? if so, I don't think I would have any clue how to do it because VBA is *English words* and *symbols* that is, at runtime, compiled into 1's and 0's so the machine can execute it, whereas your description seems like you're asking for the VBA equivalents of stuff like this:
Code:
0x1A
0x1B
so I apologize if what I'm saying isn't clear, but this stuff you're delving into is pretty involved, and there aren't a lot of questions on this forum that are this involved, in terms of complexity. Also, if what I've said here doesn't make any sense to you, it's probably best that you seek help from someone else or go elsewhere on the internet for help because the last thing you want to do is confuse yourself by reading anyone else's words here, or your own for that matter. When dealing with stuff like this, its extremely easy for people to get confused, especially when no one involved knows the purpose of what's trying to get accomplished.

<edit>
Chris, I do see that there is a direct connection between the 2 text (bas) files you posted, as in that it seems like 1 of them is a source format and the other one is the target VBA format you're wanting. Correct? If that is true, personally I still wouldn't know how.
 
Last edited:

Users who are viewing this thread

Top Bottom