⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialport.htm

📁 串口编程控件,可以读取串口发出的数据,并且可以向串中写入数据
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<a href="#ClearDTR">
CSerialPort::ClearDTR</a><br>
<a href="#ClearRTS">
CSerialPort::ClearRTS</a><br>
<a href="#SetDTR">
CSerialPort::SetDTR</a><br>
<a href="#SetRTS">
CSerialPort::SetRTS</a><br>
<a href="#SetXOFF">
CSerialPort::SetXOFF</a><br>
<a href="#SetXON">
CSerialPort::SetXON</a><br>
<a href="#GetProperties">
CSerialPort::GetProperties</a><br>
<a href="#GetModemStatus">
CSerialPort::GetModemStatus</a><br>
<a href="#SetTimeouts">
CSerialPort::SetTimeouts</a><br>
<a href="#GetTimeouts">
CSerialPort::GetTimeouts</a><br>
<a href="#Set0Timeout">
CSerialPort::Set0Timeout</a><br>
<a href="#Set0WriteTimeout">
CSerialPort::Set0WriteTimeout</a><br>
<a href="#Set0ReadTimeout">
CSerialPort::Set0ReadTimeout</a><br>
<a href="#SetMask">CSerialPort::SetMask</a><br>
<a href="#GetMask">
CSerialPort::GetMask</a><br>
<a href="#WaitEvent">
CSerialPort::WaitEvent</a><br>
<a href="#Flush">
CSerialPort::Flush</a><br>
<a href="#Purge">
CSerialPort::Purge</a><br>
<a href="#TerminateOutstandingWrites">
CSerialPort::TerminateOutstandingWrites</a><br>
<a href="#TerminateOutstandingReads">
CSerialPort::TerminateOutstandingReads</a><br>
<a href="#ClearWriteBuffer">
CSerialPort::ClearWriteBuffer</a><br>
<a href="#ClearReadBuffer">
CSerialPort::ClearReadBuffer</a><br>
<a href="#Setup">
CSerialPort::Setup</a><br>
<a href="#OnCompletion">
CSerialPort::OnCompletion</a><br>
<a href="#BytesWaiting">CSerialPort::BytesWaiting</a><br><b><a href="#DataWaiting">
CSerialPort::DataWaiting</a></b></p>

<p></p>

<p>&nbsp;</p>

</strong>

<p><b><a name="CSerialException"></a>CSerialException::CSerialException</b></p>
<p><b>CSerialException(DWORD </b><i>
dwError</i><b><i>
</i>= 0);</b></p>
<p><b>Parameters</b></p>
<p><i>dwError</i>&nbsp;&nbsp;&nbsp;The error that caused the exception.</p>
<p><b>Remarks</b></p>
<p>This member function is called when a CSerialException
object is created. To throw a CSerialException, call the global function 
AfxThrowSerialException. If you call if using the default value for dwError, 
then internally it will call GetLastError for you.</p>

<p>&nbsp;</p>

<p><b><a name="GetErrorMessage"></a>CSerialException::GetErrorMessage</b></p>
<p><b>virtual BOOL GetErrorMessage(LPTSTR </b><i>
lpszError</i><b>, UINT </b><i>
nMaxError</i><b>, PUINT </b><i>
pnHelpContext</i><i>
</i><b>= NULL);<br>
</b><b>CString GetErrorMessage();</b></p>
<p><b>Return Value</b></p>
<p>1) Nonzero if the function is successful; otherwise 0 if no error message 
text is available.</p>
<p>2) A CString representation of the error</p>
<p><b>Parameters</b></p>
<p><i>lpszError</i>&nbsp;A pointer to a buffer that will 
receive an error message.</p>
<p><i>nMaxError</i>&nbsp;The maximum number of characters 
the buffer can hold, including the NULL
terminator.</p>
<p><i>pnHelpContext&nbsp;</i>The address of a UINT that will receive the help context ID. If NULL, 
no ID will be returned.</p>
<p><b>Remarks</b></p>
<p>Call this member function to provide text about an error that has occurred.</p>

<p>&nbsp;</p>

<p><b><a name="m_dwError"></a>CSerialException::m_dwError</b></p>
<p><b>Remarks</b></p>
<p>The error that caused the exception. This error value is a system error code 
as found in WinError.h.</p>
<p>For a list of Win32 error codes, see Error Codes in the 
Win32 SDK.</p>

<strong>

<p>&nbsp;</p>

</strong>

<p><b><a name="CSerialPort"></a>CSerialPort::CSerialPort</b></p>
<p><b>CSerialPort();</b></p>
<p><b>Remarks</b></p>
<p>Standard C++ constructor for the class. Internally it just sets up the member 
variables to default values.</p>

<p>&nbsp;</p>

<p><b><a name="~CSerialPort"></a>CSerialPort::~CSerialPort</b></p>
<p><b>virtual ~CSerialPort();</b></p>
<p><b>Remarks</b></p>
<p>Standard C++ destructor for the class. It will ensure that the comm port is 
closed if it is open.</p>

<p>&nbsp;</p>

<p><b><a name="Open"></a>CSerialPort::Open</b></p>
<p><b>void Open(int </b><i>nPort</i>,<b> DWORD
</b><i>dwBaud<b> </b> </i><b>= 9600, Parity </b><i>parity<b> </b> </i><b>= 
NoParity, BYTE
</b><i>DataBits<b> </b>
</i><b>= 8, StopBits </b><i>stopbits<b> </b> </i><b>= OneStopBit, FlowControl
</b><i> fc</i><b> = NoFlowControl, BOOL </b><i>bOverlapped</i><b> = FALSE);<br>
&nbsp;&nbsp;&nbsp; throw( CSerialException* );</b></p>
<p><b>Parameters</b></p>
<p><i>nPort</i>&nbsp;&nbsp;&nbsp;The communications port to open.</p>
<p><i>dwBaud</i> The baud rate to use</p>
<p><i>parity</i> The parity to use. parity is an enum with the following values:</p>
<p>
enum Parity<br>
{ <br>
EvenParity,<br>
MarkParity,<br>
NoParity,<br>
OddParity,<br>
SpaceParity<br>
};</p>
<p><i>Databits</i> The number of data bits to use</p>
<p><i>stopbits</i> The number of stop bits to use. stopbits is an enum with the 
following values:</p>

<p>
enum StopBits<br>
{<br>
OneStopBit,<br>
OnePointFiveStopBits,<br>
TwoStopBits<br>
};
</p>

<p><i>fc</i> The flow control method to use. fc is an enum with the following 
values:</p>

<p>enum FlowControl<br>
{<br>
NoFlowControl,<br>
CtsRtsFlowControl,<br>
CtsDtrFlowControl,<br>
DsrRtsFlowControl,<br>
DsrDtrFlowControl,<br>
XonXoffFlowControl<br>
};
</p>

<p><i>bOverlapped</i> TRUE if you want to open in overlapped mode, otherwise 
FALSE to use blocking calls.</p>
<p><b>Remarks</b></p>
<p>Call this member function to open a communications port. Internally the class 
will use CreateFile to open the comm port (handling the case where the port 
number if greater than 9) and then uses SetState to set the various RS-232 
settings as specified via the function parameters. If an error occurs, a 
CSerialException will be thrown.</p>

<p>&nbsp;</p>

<p><b><a name="Close"></a>CSerialPort::Close</b></p>
<p><b>Close();</b></p>
<p><b>Remarks</b></p>
<p>The corollary function to Open. Just closes the comm port if already open.</p>

<p>&nbsp;</p>

<p><b><a name="Attach"></a>CSerialPort::Attach</b></p>
<p><b>void Attach(HANDLE </b><i>hComm</i><b>);</b></p>
<p><b>Parameters</b></p>
<p><i>hComm</i>&nbsp;&nbsp;&nbsp;The SDK handle of the open comm port.</p>
<p><b>Remarks</b></p>
<p>Allows you to attach a CSerialPort instance to an existing SDK comm port 
handle. This function is similar in behaviour to the CWnd::Attach function 
provided in MFC.</p>

<p>&nbsp;</p>

<p><b><a name="Detach"></a>CSerialPort::Detach</b></p>
<p><b>HANDLE Detach();</b></p>
<p><b>Return Value</b></p>
<p>The SDK comm port HANDLE.</p>
<p><b>Remarks</b></p>
<p>Corollary function to Attach. This function is similar in behaviour to the 
CWnd::Detach function provided in MFC.</p>

<p>&nbsp;</p>

<p><b><a name="operator HANDLE"></a>CSerialPort::operator HANDLE</b></p>
<p><b>operator HANDLE();</b></p>
<p><b>Return Value</b></p>
<p>The SDK comm port HANDLE.</p>
<p><b>Remarks</b></p>
<p>Use this operator to get the handle of the underlying comm port. You can use 
this handle to call the Windows APIs directly.</p>

<p>&nbsp;</p>

<p><b><a name="IsOpen"></a>CSerialPort::IsOpen</b></p>
<p><b>BOOL IsOpen() const</b></p>
<p><b>Return Value</b></p>
<p>TRUE if the comm port is open otherwise FALSE</p>

<p>&nbsp;</p>

<p><b><a name="Dump"></a>CSerialPort::Dump</b></p>
<p><b>void Dump(CDumpContext&amp; <i>dc</i>) const</b></p>
<p><b>Remarks</b></p>
<p>Standard MFC diagnostic support function</p>

<p>&nbsp;</p>

<p><b><a name="Read"></a>CSerialPort::Read \ ReadEx</b></p>
<p><b>
DWORD Read(void* </b><i>lpBuf</i><b>, DWORD </b><i>dwCount</i><b>);<br>
BOOL Read(void* </b><i>lpBuf</i><b>, DWORD </b><i>dwCount</i><b>, OVERLAPPED&amp;
</b><i>overlapped<b>, </b> </i><b>DWORD* </b><i>pBytesRead<b> </b> </i><b>= 
NULL);<br>
void ReadEx(void* </b><i>lpBuf</i><b>, DWORD </b><i>dwCount</i><b>);<br>
&nbsp;&nbsp;&nbsp; throw( CSerialException* );</b></p>
<p><b>Return Value</b></p>
<p>1) The number of bytes read.</p>
<p>2)&nbsp;TRUE if the overlapped read completely synchronously, FALSE if the 
operation is to be completed asynchronously.</p>
<p><b>Parameters</b></p>
<p><i>lpBuf</i>&nbsp;&nbsp;Points to the buffer to read the data 
into from the serial port</p>
<p><i>dwCount</i>&nbsp;&nbsp;Specifies the number of bytes to read 
from the serial port.</p>
<p><i>overlapped</i>&nbsp;&nbsp;reference to an OVERLAPPED 
structure. This is required if the port was opened in overlapped mode.</p>
<p><i>pBytesRead</i> If the value is non-null then upon return it will contain 
the number of bytes read</p>
<p><b>Remarks</b></p>
<p>These 3 functions are wrappers for the SDK calls ReadFile and ReadFileEx. The 
2nd version of Read is the overlapped version. Also please note that depending 
on the way you have configured timeouts, this function may appear to block while 
it waits to receive the specified data. To configure timeouts, please check out 
the Set*Timeout(s) functions.</p>

<p>&nbsp;</p>

<p><b><a name="Write"></a>CSerialPort::Write \ WriteEx</b></p>
<p><b>
DWORD Write(const void* </b><i>lpBuf</i><b>, DWORD </b><i>dwCount</i><b>);<br>
BOOL Write(const void* </b><i>lpBuf</i>,<b> DWORD </b><i>dwCount</i><b>, 
OVERLAPPED&amp;
</b><i>overlapped<b>, </b> </i><b>DWORD* </b><i>pBytesWritten<b> </b> </i><b>= 
NULL);<br>
void WriteEx(const void* </b><i>lpBuf</i><b>, DWORD </b><i>dwCount</i><b>);<br>
&nbsp;&nbsp;&nbsp; throw( CSerialException* );</b></p>
<p><b>Return Value</b></p>
<p>1) The number of bytes written.</p>
<p>2)&nbsp;TRUE if the overlapped write completely synchronously, FALSE if the 
operation is to be completed asynchronously.</p>
<p><b>Parameters</b></p>
<p><i>lpBuf</i>&nbsp;&nbsp;Points to the buffer containing the data 
to be written to the serial port</p>
<p><i>dwCount</i>&nbsp;&nbsp;Specifies the number of bytes to write 
to the serial port.</p>
<p><i>overlapped</i>&nbsp;&nbsp;reference to an OVERLAPPED 
structure. This is required if the port was opened in overlapped mode.</p>
<p><i>pBytesWritten</i> If the value is non-null then upon return it will 
contain the number of bytes written</p>
<p><b>Remarks</b></p>
<p>These 3 functions are wrappers for the SDK calls WriteFile and WriteFileEx. 
The 2nd version of Write is the overlapped version.</p>

<p>&nbsp;</p>

<p><b><a name="TransmitChar"></a>CSerialPort::TransmitChar</b></p>
<p><b>void TransmitChar(char </b><i>cChar</i><b>) const<br>
&nbsp;&nbsp;&nbsp;</b> <b> throw( CSerialException* );</b></p>
<p><b>Remarks</b></p>
<p>Simple wrapper for the TransmitCommChar SDK function call. See the Win32 SDK 
documentation for further details.</p>

<p>&nbsp;</p>

<p><b><a name="GetOverlappedResult"></a>CSerialPort::GetOverlappedResult</b></p>
<p><b>void GetOverlappedResult(OVERLAPPED&amp; </b><i>overlapped</i><b>, 
DWORD&amp; </b><i>dwBytesTransferred</i><b>, BOOL </b><i>bWait</i><b>)<br>
&nbsp;&nbsp;&nbsp;</b> <b> throw( CSerialException* );</b></p>
<p><b>Remarks</b></p>
<p>Simple wrapper for the GetOverlappedResult SDK function call. See the Win32 
SDK documentation for further details.</p>

<p>&nbsp;</p>

<p><b><a name="CancelIo"></a>CSerialPort::CancelIo</b></p>
<p><b>void CancelIo()<br>
&nbsp;&nbsp;&nbsp; throw( CSerialException* );</b></p>
<p><b>Remarks</b></p>
<p>Simple wrapper for the CancelIo SDK function call. See the Win32 SDK 
documentation for further details. Please note that this function is only 
available on NT 4 or Windows 98 or later. In version 1.0 of CSerialPort, this 
would cause code developed with it to fail to load on Windows 95 with an error 
about a missing export. This problem has been rectified in v1.01 of CSerialPort.</p>

<p>&nbsp;</p>

<p><b><a name="GetConfig"></a>CSerialPort::GetConfig</b></p>
<p><b>void GetConfig(COMMCONFIG&amp; </b><i>config</i><b>)<br>
&nbsp;&nbsp;&nbsp;</b> <b> throw( CSerialException* );</b></p>
<p><b>Remarks</b></p>
<p>Simple wrapper for the GetCommConfig SDK function call. See the Win32 SDK 
documentation for further details.</p>

<p>&nbsp;</p>

<p><b><a name="GetDefaultConfig"></a>CSerialPort::GetDefaultConfig</b></p>
<p><b>static void GetDefaultConfig(int </b><i>nPort</i><b>, 
COMMCONFIG&amp; </b><i>config</i><b>)<br>
&nbsp;&nbsp;&nbsp;</b> <b> throw( CSerialException* );</b></p>
<p>Remarks</p>
<p>Simple wrapper for the GetDefaultCommConfig SDK function call. See the Win32 
SDK documentation for further details.</p>

<p>&nbsp;</p>

<p><b><a name="SetConfig"></a>CSerialPort::SetConfig</b></p>
<p><b>void SetConfig(COMMCONFIG&amp; </b><i>config</i><b>)<br>
&nbsp;&nbsp;&nbsp;</b> <b> throw( CSerialException* );</b></p>
<p><b>Remarks</b></p>
<p>Simple wrapper for the SetCommConfig SDK function call. See the Win32 SDK 
documentation for further details.</p>

<p>&nbsp;</p>

<p><b><a name="SetDefaultConfig"></a>CSerialPort::SetDefaultConfig</b></p>
<p><b>static void SetDefaultConfig(int </b><i>nPort</i><b>, 
COMMCONFIG&amp; </b><i>config</i><b>)<br>
&nbsp;&nbsp;&nbsp;</b> <b> throw( CSerialException* );</b></p>
<p><b>Remarks</b></p>
<p>Simple wrapper for the SetDefaultCommConfig SDK function call. See the Win32 
SDK documentation for further details.</p>

<p>&nbsp;</p>

<p><b><a name="ClearBreak"></a>CSerialPort::ClearBreak</b></p>
<p><b>void ClearBreak()<br>
&nbsp;&nbsp;&nbsp; throw( CSerialException* );</b></p>
<p><b>Remarks</b></p>
<p>Simple wrapper for the ClearCommBreak SDK function call. See the Win32 SDK 
documentation for further details.</p>

<p>&nbsp;</p>

<p><b><a name="SetBreak"></a>CSerialPort::SetBreak</b></p>
<p><b>void SetBreak()<br>
&nbsp;&nbsp;&nbsp; throw( CSerialException* );</b></p>
<p><b>Remarks</b></p>
<p>Simple wrapper for the SetCommBreak SDK function call. See the Win32 SDK 
documentation for further details.</p>

<p>&nbsp;</p>

<p><b><a name="ClearError"></a>CSerialPort::ClearError</b></p>
<p><b>void ClearError(DWORD&amp; </b><i>dwErrors</i><b>)<br>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -