API serial port operation Encapsulation

Source: Internet
Author: User

Comm. h header file:

 

 

# Ifndef _ ccomm_h __
# DEFINE _ ccomm_h __

Class ccomm // serial port operation encapsulation class
{
PRIVATE:
Handle m_hcomm;
Public:
Ccomm (); // Constructor
~ Ccomm (); // destructor

Bool opencomm (INT ncomm); // enable the serial port Function
Void closecomm (); // disable the serial port Function

Bool setcomm (INT nbaudrate/* baud rate */, int nparity/* parity bit */, int nbytesize/* bytes */, int nstopbits/* Stop bit */); // set the serial port Function
Bool settimeouts (); // sets the timeout function.

Bool readcomm (char * lpbuf, int nlen); // read the serial port Function
Bool writecomm (char * lpbuf, int nlen); // write a serial Function
};

# Endif

 

Comm. cpp implementation file:

 

# Include "stdafx. H"
# Include "Comm. H"

// Constructor
Ccomm: ccomm ()
{
}

// Destructor
Ccomm ::~ Ccomm ()
{
Closecomm (); // close the serial port
}

// Enable the serial port Function
Bool ccomm: opencomm (INT ncomm)
{
Cstring strcommname;
Cstring strerrinfo;

Strcommname. Format ("Com % d", ncomm );

M_hcomm =: createfile (strcommname,/* Name of the serial port to be opened */
Generic_read | generic_write,/* allow read and write */
0,/* exclusive mode */
Null,/* Security Attribute */
Open_existing,/* Open instead of creating */
0,/* synchronization mode */
Null);/* template handle */

If (m_hcomm = invalid_handle_value)
{
Strerrinfo. Format ("failed to open % s! ", Strcommname );
Afxmessagebox (strerrinfo );
Return false;
}
Else
{
Return true;
}
}

// Disable the serial port Function
Void ccomm: closecomm ()
{
If (m_hcomm! = Invalid_handle_value)
{
: Closehandle (m_hcomm );
M_hcomm = invalid_handle_value;
}
}

// Set the serial port Function
Bool ccomm: setcomm (INT nbaudrate/* baud rate */, int nparity/* parity bit */, int nbytesize/* bytes */, int nstopbits/* Stop bit */)
{
DCB stdcb;
Memset (& stdcb, 0, sizeof (stdcb ));
If (! : Getcommstate (m_hcomm, & stdcb) // get the current status attribute of the serial port
Return false;
Stdcb. baudrate = nbaudrate; // baud rate
Stdcb. fparity = 0;
Stdcb. Parity = nparity; // parity bit (noparity, etc)
Stdcb. bytesize = nbytesize; // each byte has 8 bits
Stdcb. stopbits = nstopbits; // stop bit (onestopbit, etc)
If (! : Setcommstate (m_hcomm, & stdcb) // sets the serial port status attribute.
Return false;

If (! : Setupcomm (m_hcomm,) // you can specify the size of the input and output buffers.
Return false;
: Purgecomm (m_hcomm, purge_txclear | purge_rxclear); // clears the input/output buffer.

Return true;
}

// Set the timeout Function
Bool ccomm: settimeouts ()
{
Commtimeouts sttimeouts;

Sttimeouts. readintervaltimeout = 0; // you can specify the read timeout value.
Sttimeouts. readtotaltimeoutmultiplier = 100;
Sttimeouts. readtotaltimeoutconstant = 500;

Sttimeouts. writetotaltimeoutmultiplier = 100; // set the write timeout.
Sttimeouts. writetotaltimeoutconstant = 500;

: Setcommtimeouts (m_hcomm, & sttimeouts); // set timeout
: Purgecomm (m_hcomm, purge_txclear | purge_rxclear); // clears the input/output buffer.
Return true;
}

// Read the serial port Function
Bool ccomm: readcomm (char * lpbuf, int nlen)
{
If (: readfile (m_hcomm, lpbuf, nlen, (DWORD *) & nlen, null) = false)
{
Return false;
}
Else
{
/*
Cstring STR;
Str. Format ("% d", nlen );
Afxmessagebox (STR );
*/

//: Purgecomm (m_hcomm, purge_txabort | purge_rxabort | purge_txclear | purge_rxclear );
Return true;
}
}

// Write the serial Function
Bool ccomm: writecomm (char * lpbuf, int nlen)
{
If (: writefile (m_hcomm, lpbuf, nlen, (DWORD *) & nlen, null) = false)
{
Return false;
}
Else
{
/*
Cstring STR;
Str. Format ("% d", nlen );
Afxmessagebox (STR );
*/
Return true;
}
}

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.