I. Receiving and displaying data
1. Add the oncomm function declaration in the serialporttestdlg. h file. This function is processed after the serial port "receive messages with characters" and the characters are displayed.
- /*-------------------------------------
- * Serialporttestdlg. h file
- *-------------------------------------*/
- Class cserialporttestdlg: Public cdialog
- {
- // Construction
- Public:
- Cserialport m_serialport; // cserailport Class Object
- Bool m_bserialportopened; // indicates whether the serial port is enabled.
- // Omitting the automatically generated code...
- Protected:
- // Generated message map Functions
- // {Afx_msg (cserialporttestdlg)
- // Omitting the automatically generated code...
- /*----------------------------------------
- * Oncomm function declaration, which is used to "receive characters" on the serial port"
- * Handle the message and display the problem.
- *----------------------------------------*/
- Afx_msg long oncomm (wparam CH, lparam port );
- Afx_msg void onbuttonopen ();
- Afx_msg void onbuttonclose ();
- Afx_msg void onbuttonsend ();
- //} Afx_msg
- Declare_message_map ()
- };
2. In the serialporttestdlg. cpp file, perform message correspondence (wm_comm_rxchar ).
- /*----------------------------------------------
- * Serialporttestdlg. cpp File
- *----------------------------------------------*/
- Begin_message_map (cserialporttestdlg, cdialog)
- // {Afx_msg_map (cserialporttestdlg)
- On_wm_syscommand ()
- On_wm_paint ()
- On_wm_querydragicon ()
- /*----------------------------------------------
- * Wm_comm_rxchar message <----> oncomm function ing Declaration
- *----------------------------------------------*/
- On_message (wm_comm_rxchar, oncomm)
- On_bn_clicked (idc_button_open, onbuttonopen)
- On_bn_clicked (idc_button_close, onbuttonclose)
- On_bn_clicked (idc_button_send, onbuttonsend)
- //} Afx_msg_map
- End_message_map ()
3. Add the oncomm function implementation to the serialporttestdlg. cpp file.
- /*------------------------------------------------------
- * Oncomm function implementation
- * Trigger condition: a message is triggered when the input buffer contains characters.
- * Execution result: receives the characters and displays them in the text box m_streditreceivemsg.
- *------------------------------------------------------*/
- Long cserialporttestdlg: oncomm (wparam CH, lparam port)
- {
- M_streditreceivemsg + = CH; // receives characters
- Updatedata (false); // display the received characters in the receiving edit box
- Return 0;
- }
Ii. Data Transmission
- /*-----------------------------------------------------------
- * Function: Send characters
- * Execution condition: When you click "send" (buttonsend)
- * Execution result: the text in the text box m_streditsendmsg is sent.
- *----------------------------------------------------------*/
- Void cserialporttestdlg: onbuttonsend ()
- {
- // Todo: add your control notification handler code here
- If (! M_bserialportopened) return; // check whether the serial port is enabled. If not, exit
- // Send Information Processing
- Updatedata (true); // read the data in the edit box
- M_serialport.writetoport (lpctstr) m_streditsendmsg); // send data
- }
Iii. Summary:
After the serial port is successfully connected, if the input buffer contains characters, it will be displayed in the Receiving text box. If you click the send button, the data in the input text box will be sent to the output buffer of the serial port. Data will be communicated with another connected serial port through this serial port.