Windows串口编程基础(推荐资料)

         串口编程方面的学习资料很多,笔者根据这两年工作中串口编程的学习机应用给大家推荐一些个人认为非常值得参考的资料,希望对你有用!

 1、《Serial Communications in Win32》—Allen Denver(Microsoft Windows Developer Support )

http://msdn.microsoft.com/en-us/library/ms810467.aspx 

2、《Windows API串口编程参考》—中文的,作者给出了串口操作会用到的 API 的介绍。

http://mail.ustc.edu.cn/~wangyu/MFC/sy09/sy0902.htm 

3、《Serial library for C++》—Ramon de Klein

非常好,特别是底下的 FAQ,里面有很多使用者遇到的问题及解决办法,就算你暂时未遇到,也可以经常看看,对你提高串口编程的能力非常有帮助!

http://www.codeproject.com/KB/system/serial.aspx 

4、个人总结的两个初学者常见的问题:

(1)访问编号大于9的串口的方法[来源MSDN]

        How to specify Serial ports lager than COM9? CreateFile() can be used to get a handle to a serial port. The "Win32 Programmer's Reference" entry for "CreateFile()" mentions that the share mode must be 0, the create parameter must be OPEN_EXISTING, and the template must be NULL. CreateFile() is successful when you use "COM1" through "COM9" for the name of the file; however, the message INVALID_HANDLE_VALUE is returned if you use "COM10" or greater. If the name of the port is \\.\COM10, the correct way to specify the serial port in a call to CreateFile() is as follows:

CreateFile(
                       "\\\\.\\COM10",                // address of name of the communications device

                       fdwAccess,                     // access (read-write) mode

                       0,                                      // share mode

                      NULL,                              // address of security descriptor

                      OPEN_EXISTING,         // how to create

                      0,                                      // file attributes

                      NULL                               // handle of file with attributes to copy

                   );

NOTES: This syntax also works for ports COM1 through COM9. Certain boards will let you choose the port names yourself. This syntax works for those names as well.

访问COM1到COM9,可以使用“COM9:”这样的方式,注意在WinCE下要加冒号。也可以使用"\\\\.\\COM9:"的方式,前者是后者的简化。但打开大于9的串口,这种简化方式就会出现问题,就必须使用"\\\\.\\COM12:"的方式了。这点可能容易被忽略。前面的程序没有提供这个功能,不过可以通过自己修改源代码来完成!

(2)OVERLAPPED     olR,olW;

定义为全局变量。当定义为函数内的局部变量时会有问题。

发送函数代码(参考):

void CWin32CommTestDlg::OnBnClickedButtonSnd()

{

         // TODO: 在此添加控件通知处理程序代码

#define OUTBUFSZ 1024

         BYTE         OutBuf[OUTBUFSZ+1] = {0};

         DWORD   dwBytesOfWritten;

 

         olW.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

         ASSERT(olW.hEvent);

         UpdateData(TRUE);

         for (int i=0; i<m_strSND.GetLength(); i++)

         {

                   OutBuf[i] = (BYTE)m_strSND.GetAt(i);

         }

         PurgeComm(hComm, PURGE_TXABORT|PURGE_TXCLEAR);

         EnterCriticalSection(&m_csCommSync);

         WriteFile(hComm, OutBuf, m_strSND.GetLength(), &dwBytesOfWritten, &olW);

         LeaveCriticalSection(&m_csCommSync);

}

 

5、串口编程的 3 个选择:

(1)微软提供的串口控件 MSComm

(2)第三方提供的串口类(如:CSerialPort 等)

(3)直接使用 API 进行串口编程

要下班了,下一篇文章再继续接着 5 展开。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值