串口编程笔记

花了几天学习了孙鑫老师的VC++,感觉讲的很不错,但是听完了以后还是觉得自已什么都不会,所以想自已动手做几个串口通迅程序,因本人太健忘所以将学习过程记于此.
第一天:
首先串口是一种通迅资源,VC++中可以用CreateFile(...)打开,以下是MSDN中的一段话:
Configuring a Communications Resource href="ms-help://Hx/HxRuntime/HxLink.css" type="text/css" rel="STYLESHEET"> href="ms-help://Hx/HxRuntime/HxLinkDefault.css" type="text/css" rel="STYLESHEET"> href="../common/backsdk4.css" type="text/css" rel="STYLESHEET"><script src="../common/langref.js"></script>

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.





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值