I recently used VC to write an ActiveX plug-in. In fact, it is to operate serial data communication, read serial data through a hardware, and then upload the data to the B/S platform. The underlying hardware layer of the instrument is written in C language. How to Write it is not my focus. I am mainly responsible for the development of the instrument and PC interface.
Each source device has a set of protocols for serial communication, which are provided by the hardware.
Disclaimer: Here, I only want to summarize and record some things through the logging method of my blog, so that I can summarize it easily. I hope all the experts will not make fun of me. If you have better suggestions, please leave a message for me, thank you very much!
The following plug-in reads data for a simple analysis,
The data read from the serial port is as follows:
0003fe4500000000004000f81e
A8000000
838be614
0080000000000000
018000004a9a10100
02803a0733f10200
0380000000000000
0480000000000000
0580000000000000
204e0000e8030000
0d0a00005733d320000283130305229
From this data string, I don't know what it is or why.
Compare the hardware protocols.
Unpackage data packets (defined by hardware)
0003fe45000000000000004000f81e indicates the header.
Where
0003fe: The identifier and direction of the package, from the lower computer to the upper computer.
4500: represents a command
00000000: represents the Command Parameters
4000: indicates the package length.
F8: indicates the Baotou test.
1e: indicates partial packet data inspection
Package Data Analysis
A8: column number
00: Check project code
0000: blank contrast value
838be614: Detection Date and Time
0080000000000000: First-Channel Data
018000004a9a10100: Second-Channel Data
02803a0733f10200: Third-Channel Data
0380000000000000: Fourth Channel Data
0480000000000000: Fifth-Channel Data
0580000000000000: Sixth Channel Data
204e0000e8030000: supplement data (supplement data at the end of the package)
0d0a00005733d320000283130305229 is useless data. Equivalent to the pattern returned by Invalid Data
This analysis seems clearer. However, you still cannot know what the specific data represents.
The next step is data conversion and processing. To put it bluntly, it is string analysis.
Because Data Storage UsesSmall-end ModeThat is, put the low position at the front and the high position at the back.
There is no need to handle too many headers, but data can be verified and there is no problem.
The focus is on analysis. The data part of the data packet is the effective data we want and focuses on processing.
According to the string placeholder, we can see the date and time string: 838be614, which is defined as the date daytime string. First of all, this time string is compressed and put into the register.
A substring should be interpreted as a valid and easy-to-understand Date and Time string, for example, 2010-01-01 30: 20: 12.
Let's take a look at the processing section below.Code.
// Time and date Processing code snippet
Cstring checkdate; // store the date and time string
Cstring str1, str2, str3, str4, str5, str6; // temporary variable during intermediate processing
Uint utdate = 0;
Byte by [4]; // convert to a byte array
For (int K = 0; k <checkdate. getlength (); k ++)
{
Str1 = checkdate. mid (K * 2, 2 );
Sscanf (str1, "% 02x", & by [k]); // convert to by []
}
Utdate = by [0] | (by [1] <8) | (by [2] <16) | (by [3] <24 ); // high/low conversion
Str1.format ("% d", (utdate> 25) & 0x7f); // year
Str2.format ("% d", (utdate> 21) & 0x0f); // month
Str3.format ("% d", (utdate> 16) & 0x1f); // day
Str4.format ("% d", (utdate> 11) & 0x1f); // hour
Str5.format ("% d", (utdate> 5) & 0x3f); // minute
Str6.format ("% d", (utdate <1) & 0x3f); // second
Checkdate = str1 + "-" + str2 + "-" + str3 + "" + str4 + ":" + str5 + ":" + str6; // obtain the result, for example, 10-3-2 30: 1: 33.
Coledatetime dtstart;
Dtstart. parsedatetime (checkdate); // standard format Date and Time
Checkdate = dtstart. Format ("% Y-% m-% d % H: % m: % s"); // get the final standard output format
It seems like a simple string of things. To turn it into useful data, it's still a few twists and turns ....
VC is a cainiao, and many things cross the river by feeling the stones. After several twists and turns, the task can finally be completed...