(b) In the process of programming.
Author: Dan 20065438+0 August 25th
It can be reproduced, but the author and description /www.gjwtech.com "goal =" _ blank "> must be indicated, and it shall not be used for business.
Form content:
1。 Create a project
This project inserts an MSComm control.
Add a control to the dialog box.
OnComm serial event message processing functions (5 in total).
Open and set serial port parameters
7。 TXD
Send hexadecimal characters
Hexadecimal display of receiving box
How to set the 10 automatic file?
1 1 VARIANT data type? How to use VARIANT data type?
Serial debugging assistant source program detailed programming process (1) continued, thank you for your support and encouragement.
8。 Send hexadecimal characters
In the main dialog box, add a check, check the button IDIDC _ check _ hexsend title:10 hexadecimal send, and then use ClassWizard to add a control variable: m_ctrlHexSend.
& lt/ Add the SCommTestDlg class public member function in ClassView, and enter the corresponding code;
//The format conversion function of the hex send blank box is limited, and one character should be inserted between every two characters.
//For example: A 1 23 45 0B 00 29
//CByteArray is a dynamic byte array, which can be found in MSDN Help.
Interpretation of CSCommTestDlg: (String2Hex in string2 hex, SendData in CByteArray)
{
Explain that hexdata is lowhexdata
Explain that hexdatalen = 0;;
Explain that Ryan = str. GetLength(); ....../& gt; Send data. SetSize(LEN/2);
(INT I = 0; I
{
Characters LSTR,hstr = STR[I];
(hstr == ' ')
{
+ +;
Continue;
}
+ +;
(& gt= LEN)
Breakthrough;
LSTR = STR[I];
hex data ConvertHexChar(hstr);
lowhexdata ConvertHexChar(LSTR);
((hex data = = 16)| |(lowhexdata = = 16))
Breakthrough;
other
hex data = hex data * 16+low hex data
i++;
a send data[hexdatalen]=(character)hexdata ";
hexdatalen++;
}
Return hexdatalen
}
//This is a function that converts characters into corresponding hexadecimal values.
//There are many books in C language, which can be found in.
//function: Convert characters between 0 and f into corresponding hexadecimal characters, or return-1.
CHaracter CSCommTestDlg::ConvertHexChar(CHaracter ch)
{
((CH & gt= ' 0 ')& amp; & amp(CH & lt= '9' ))
Return ch-0x30;
Otherwise, if ((ch> =' a')&; & amp(CH & lt='F '))
Return ch-'a'+10; BR/>; Otherwise, if ((ch> =' a')&; & amp(CH & lt='F '))
Return ch-'a'+10;
Otherwise return (-1); ....../& gt; }
The cost of cscommtestdlg:: onbuttonmanualsend () is as follows:
Invalid cscommtestdlg:: onbuttonmanualsend ()/> {
//TODO: Add the control notification handler code here.
update data(TRUE); //Read the contents of the edit box
(m _ ctrlHexSend。 get check())/& gt;
CByteArray hexdata
Interpret Ryan? (m _ strTXData hex data); //Returns the hexadecimal number that can be used to calculate the sent number.
M _ ctrl comm.setoutput (hexadecimal data of cole variant)//send hexadecimal data.
}
other
M _ ctrlcomm set output of cole variant ((m _ strtdata));); //Send
ASCII character data of {
Now, connect the first serial port, open the serial port debugging assistant V2. 1, select the hexadecimal display, set the corresponding serial port, then run this program, send the box, enter the hexadecimal characters of 00 0 1 02 03 A 1 CC, choose to send the hexadecimal serial port debugging assistant to the receiving box, and click "Manually send 0060".
9。 Receive box, displayed in hexadecimal.
This is very simple: in the main dialog box, a complex election button IDC_CHECK_HEXDISPLAY explains: hexadecimal display, and then use the class wizard to add a control variable: m_ctrlHexDiaplay. Then use CSCommTestDlg:: command in OnComm () function:
Invalid CSCommTestDlg:: in OnComm ()
{
//TODO: Add the control notification handler code here.
COleSafeArray safearray_inp
Length, k;
BYTE RXDATA[2048]// Set the 8-bit integerthat of byte array, without signature.
Strtemp of CString
(m _ ctrlcomm。 Get comment()= = 2)// Characters in the event receiving buffer.
{
variant_inp = m_ctrlComm。 GetInput(); //Read buffer
Safearray _ inp variant _ inp// ColeSafeArray variable converted from variant variable.
LEN = safearray_inp。 GetOneDimSize(); //Get the valid data length
(k = 0; & ltLEN++ variable)
safearray_inpGetElement(&K,RXDATA+K); //Convert to byte array
(k = 0; & ltLEN++)// array is converted to CSTRING.
{
Byte Bt = (char *) of (RXDATA+K); //character
If (m_ctrlHexDisplay。 GetCheck())
strtemp。 f? Format ("%02X", bt); //The temporary variable strtemp stores characters in hexadecimal mode. Please note that a space has been added here.
other
strtemp。 f? format(" % C ",BT); //characters are sent to the temporary variable strtemp storage area.
m _ str xdata+= strtemp; //The string corresponding to the edit box of the receiving control.
}
} update data(FALSE); //Update the contents of the edit box
}
Test: 00 0 1 02 03 A 1 CC hexadecimal character box, enter hexadecimal serial port debugging assistant to send, and select send. After the selected program runs, click "Manually Send Hexadecimal Serial Debugging Assistant" and click Manual Transmission or Automatic Transmission. The receiving box should be able to see 00 10203A 1 cc in the program.
10。 How to set the simplest () function to set the automatic transmission period SetTimer, and the transmission control instructions useful for data acquisition can be used for timed transmission.
: select MessageMap card in ClassWizard, then select CSCommTestDlg class as the object ID, then select WM_TIMER message in the message box, and click OnTimer(UINT nIDEvent) function of CSCommTestDlg::, which is invalid in ADD_FUNCTION. This function is placed at the specified time to process the code:
Invalid ontime (uinnid event) of CSCommTestDlg:
{
//TODO: Add message handler code here and/or call default.
OnButtonManualsend();
On timer (NID event) of CDialog:
}
Add an ID IDC_CHECK_AUTOSEND title of the check button in the main dialog box: automatic sending (period 1 second), and then use ClassWizard to add the BN_CLICK message processing function Void CSCommtestdlg:: oncheckautosend ():
Invalid cscommtestdlg:: oncheckautosend () {
//TODO: Add the control notification handler code here.
m_bAutoSend =! m _ bAutoSend
(m _ botson)
{
SetTimer's (1, 1000, null); //The time is1000 ms.
}
other
{ BR/& gt; KillTimer function (1); //Cancel the timer
}
}
Add the CSCommTestDlg class in the ClassView: m_bAutoSend BOOL variable and initialize it in the constructor:
m _ bAutoSen = FALSE
Now, you can run the program to test.
1 1。 What about variable data types? How to use VARIANT data type?
Not knowing how to use VARIANT data type, many friends have a headache about the new data type VARIANT. The SetOutput () function requires a VARIANT parameter, or you can use COleVARIANT's class to construct a simple generator set. Now it is variant type of return value of the GetInput () function, so how to extract useful content from the return value? Cole VARIANT's main ole automatic transmission data comes from VARIANT. In fact, VARIANT is just a new definition, which fills the structure, including a union, with a variable as the main member. A consortium of various data and variables used to specify data types are currently composed of members in the consortium. Our concern is to store the received data in the member countries of the data federation. Associations contain many types of data, from simple variables to very complex arrays and pointers. What is received through the serial port is often a byte string, and we will use arrays or pointers to access the received data. What I recommend to you here is that a type variable points to a SAFEARRAY(COleSafeArray). As the name implies, the new data type SAFEARRAY is a "safe array", which can automatically adjust its definition of 16 or 32-bit according to the system environment, instead of the conversion between OLE changes (some types, such as BSTR, are corrupted binary data in 16 or 32-bit OLE applications). We don't know the specific definition of SAFEARRAY, except that it is a structure, which contains a pointer (void *) to pvData, pointing to the memory that stores useful data. In a word, the VARIANT variable returned by the GetInput () function identifies the pointer of Parry, and the pointer identifying pvData accesses the array to obtain the data received by the pointer variable of SAFEARRAY. Please refer to the OnComm () function of invalid CSCommTestDlg for specific application::.
Maybe I can't explain this problem myself. From the first time I came into contact with this thing, I said that others still don't know.
BR/>; Another question: For the setting of binary transceiver, please refer to the description of MSComm control.
Please help me, I would be very grateful~~