Simple usage of serial cserialport

Source: Internet
Author: User

Simple usage:

1. Define members:
Cserialport m_serialport;

2. Initialization:
M_serialport.setbuffersize );
M_serialport.setwnd (m_hwnd );
M_serialport.setpolicynum (def_in_byte_size );
If (m_serialport.isopen ())
{
M_serialport.close ();
}
M_serialport.open (1, "9600, O, 8, 1 ");

M_serialport.clearinputbuffer ();
M_serialport.clearoutputbuffer ();

3. receive data:
A. on_message (on_com_receive, rs232onreceive)

B. rs232onreceive function body:
} M_serialport.lock ();

Byte _ rxdata_array [def_in_byte_size];
Zeromemory (_ rxdata_array, def_in_byte_size );
Int nreceivedlength = 0;

Byte tempbyte [def_in_byte_size];
Zeromemory (tempbyte, def_in_byte_size );
Int _ bufferlength = 0;

// Bool bfound0x51 = false; // indicates whether the data header has arrived.
DWORD _ tickcount = gettickcount ();
While (nreceivedlength <def_in_byte_size)
{
If (gettickcount ()-_ tickcount)> 50) // prevents endless loops that cannot receive data
{
Trace ("receiving data timeout. \ n ");
Break;
 

_ Bufferlength = m_serialport.read (tempbyte, 1 );
// If (! Bfound0x51) // if no data header is obtained
//{
// If (tempbyte [0] = 0x51) // determines whether the data header has arrived
//{
// Bfound0x51 = true;
//}
//}
//
// If (! Bfound0x51)
//{
// Trace ("0x % 02x: Wait for the data header to arrive. \ n", tempbyte [0]);
// Continue;
//}

If (_ bufferlength> 0)
{
Memcpy (_ rxdata_array + nreceivedlength, tempbyte, _ bufferlength );
Nreceivedlength + = _ bufferlength;
}
}

M_serialport.clearinputbuffer ();
M_serialport.unlock ();

4. Send data:
M_serialport.lock ();
M_serialport.write (txbuffer, 9 );
M_serialport.unlock ();

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.