第一天:
首先串口是一种通迅资源,VC++中可以用CreateFile(...)打开,以下是MSDN中的一段话:
The following example opens a handle to COM2 and fills in a DCB structure with the current configuration. The DCB structure is then modified and used to reconfigure the device.
/* A sample program to illustrate setting up a serial port. */
#include <windows.h>
int main(int argc, char *argv[])
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM2"; //定义打开的文件名
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d./n", GetLastError());
return (1);
}
// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);//将串口的状态信息填充到DCD结构体中
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d./n", GetLastError());
return (2);
}
// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_57600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d./n", GetLastError());
return (3);
}
printf ("Serial port %s successfully reconfigured./n", pcCommPort);
return (0);
}
关于用CreateFile打开通迅资源时
The CreateFile function can create a handle to a communications
resource, such as the serial port COM1. For communications resources, the
dwCreationDisposition parameter must be OPEN_EXISTING, and the
hTemplate parameter must be NULL. Read, write, or read/write access can
be specified, and the handle can be opened for overlapped I/O.
在打开串口的时候是以同步I/O方式打开的,关于同步I/O操作方式,比如说发出写命令后,直到数据完全写入缓冲区后写函数才返回
如果写入时间较长将会使程序挂起.
关于重叠I/O操作方式.参见MSDN中说明FILE_FLAG_OVERLAPPED
Instructs the system to initialize the object, so that operations that take a
significant amount of time to process return ERROR_IO_PENDING. When the
operation is finished, the specified event is set to the signaled state.
When you specify FILE_FLAG_OVERLAPPED, the file read and write functions must specify an OVERLAPPED structure. That is, when FILE_FLAG_OVERLAPPED is specified, an application must perform overlapped reading and writing.
When FILE_FLAG_OVERLAPPED is specified, the system does not maintain the file pointer. The file position must be passed as part of the lpOverlapped parameter (pointing to an OVERLAPPED structure) to the file read and write functions.
This flag also enables more than one operation to be performed simultaneously with the handle (a simultaneous read and write operation, for example).
关于DCB结构体中的几个元素
-
BaudRate
-
Baud rate at which the communications device operates. This member can be an actual baud rate value, or one of the following indexes:
CBR_110
CBR_19200
CBR_300
CBR_38400
CBR_600
CBR_56000
CBR_1200
CBR_57600
CBR_2400
CBR_115200
CBR_4800
CBR_128000
CBR_9600
CBR_256000
CBR_14400
fBinary
- If this member is TRUE, binary mode is enabled. Windows does not support nonbinary mode transfers, so this member must be TRUE. fOutxCtsFlow
- If this member is TRUE, the CTS (clear-to-send) signal is monitored for output flow control. If this member is TRUE and CTS is turned off, output is suspended until CTS is sent again. fOutxDsrFlow
- If this member is TRUE, the DSR (data-set-ready) signal is monitored for output flow control. If this member is TRUE and DSR is turned off, output is suspended until DSR is sent again.