Serial Data Communication Program Implementation (SerialPort class method)

Source: Internet
Author: User

 

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.

  1. /*-------------------------------------
  2. * Serialporttestdlg. h file
  3. *-------------------------------------*/
  4. Class cserialporttestdlg: Public cdialog
  5. {
  6. // Construction
  7. Public:
  8. Cserialport m_serialport; // cserailport Class Object
  9. Bool m_bserialportopened; // indicates whether the serial port is enabled.
  10. // Omitting the automatically generated code...
  11. Protected:
  12. // Generated message map Functions
  13. // {Afx_msg (cserialporttestdlg)
  14. // Omitting the automatically generated code...
  15. /*----------------------------------------
  16. * Oncomm function declaration, which is used to "receive characters" on the serial port"
  17. * Handle the message and display the problem.
  18. *----------------------------------------*/
  19. Afx_msg long oncomm (wparam CH, lparam port );
  20. Afx_msg void onbuttonopen ();
  21. Afx_msg void onbuttonclose ();
  22. Afx_msg void onbuttonsend ();
  23. //} Afx_msg
  24. Declare_message_map ()
  25. };

2. In the serialporttestdlg. cpp file, perform message correspondence (wm_comm_rxchar ).

  1. /*----------------------------------------------
  2. * Serialporttestdlg. cpp File
  3. *----------------------------------------------*/
  4. Begin_message_map (cserialporttestdlg, cdialog)
  5. // {Afx_msg_map (cserialporttestdlg)
  6. On_wm_syscommand ()
  7. On_wm_paint ()
  8. On_wm_querydragicon ()
  9. /*----------------------------------------------
  10. * Wm_comm_rxchar message <----> oncomm function ing Declaration
  11. *----------------------------------------------*/
  12. On_message (wm_comm_rxchar, oncomm)
  13. On_bn_clicked (idc_button_open, onbuttonopen)
  14. On_bn_clicked (idc_button_close, onbuttonclose)
  15. On_bn_clicked (idc_button_send, onbuttonsend)
  16. //} Afx_msg_map
  17. End_message_map ()

3. Add the oncomm function implementation to the serialporttestdlg. cpp file.

  1. /*------------------------------------------------------
  2. * Oncomm function implementation
  3. * Trigger condition: a message is triggered when the input buffer contains characters.
  4. * Execution result: receives the characters and displays them in the text box m_streditreceivemsg.
  5. *------------------------------------------------------*/
  6. Long cserialporttestdlg: oncomm (wparam CH, lparam port)
  7. {
  8. M_streditreceivemsg + = CH; // receives characters
  9. Updatedata (false); // display the received characters in the receiving edit box
  10. Return 0;
  11. }

Ii. Data Transmission

  1. /*-----------------------------------------------------------
  2. * Function: Send characters
  3. * Execution condition: When you click "send" (buttonsend)
  4. * Execution result: the text in the text box m_streditsendmsg is sent.
  5. *----------------------------------------------------------*/
  6. Void cserialporttestdlg: onbuttonsend ()
  7. {
  8. // Todo: add your control notification handler code here
  9. If (! M_bserialportopened) return; // check whether the serial port is enabled. If not, exit
  10. // Send Information Processing
  11. Updatedata (true); // read the data in the edit box
  12. M_serialport.writetoport (lpctstr) m_streditsendmsg); // send data
  13. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.