win32 API 开发串口 参考资料

参考网址:

Win32 API串口编程 (很好) 同步 代码 异步 代码 都有
https://blog.csdn.net/zhuyonghao123/article/details/8162250

WIN32控制台下的串口通信程序(异步操作)
https://blog.csdn.net/u013232740/article/details/49871113



windows串口通信操作和代码实现(很好的例子)
https://blog.csdn.net/wenshiabc/article/details/53026183


windows下串口初步编程(多线程+windows串口)
(一般,可以学习一下 怎么建立线程)
https://blog.csdn.net/rabbitjerry/article/details/75384688


串口封装的类
Windows下C++ 串口编程实例
(一个字节 一个字节的读)
https://blog.csdn.net/wzmsltw/article/details/50723267



串口通信编程(2):使用Windows API
一般
https://blog.csdn.net/milanleon/article/details/38438533


·设置端口读写超时
BOOL GetCommTimeouts(  
  HANDLE hFile,  
  LPCOMMTIMEOUTS lpCommTimeouts  
);  
BOOL SetCommTimeouts(  
  HANDLE hFile,  
  LPCOMMTIMEOUTS lpCommTimeouts  
);  
在用ReadFile和WriteFile读写串口时,需要考虑超时问题。
如果在指定的时间内没有读出或者写入指定数量的字节数据,
那么ReadFile函数或者WriteFile函数就会返回。
GetCommTimeouts函数用来查询当前的超时时间设置,
该函数会填充一个COMMTIMEOUTS结构。
SetCommTimeouts函数用来通过一个COMMTIMEOUTS结构设置超时时间。
两个函数的返回值都是成功时返回非零值,失败时返回零。
[cpp] view plain copy
BOOL GetCommMask(  
  HANDLE hFile,  
  LPDWORD lpEvtMask  
);  
BOOL SetCommMask(  
  HANDLE hFile,  
  DWORD dwEvtMask  
);  
BOOL WaitCommEvent(  
  HANDLE hFile,  
  LPDWORD lpEvtMask,  
  LPOVERLAPPED lpOverlapped  
);  
GetCommMask函数用于得到串口已经设置了的串口事件,
参数hFile指定已打开的串口句柄,
参数lpEvtMask用于存取得到的串口事件集。
SetCommMask函数的功能与GetCommMask函数正好相反,
用于设置串口事件集。
WaitCommEvent函数用于等待预先设置的串口事件中的某一个事件发生,
该函数将阻塞线程,
直到预先设置的串口事件中的某一事件的发生。
参数lpEvtMask用于存储已经发生的事件,
参数lpOverlapped必须设置为NULL,
因为在Windows CE中不支持重叠I/O操作。
EV_BREAK
检测到中断发生
EV_CTS
CTS信号改变了状态
EV_DSR
DSR信号改变了状态
EV_ERR
串口驱动程序检测到了错误,如CE_RXPARITY,CE_OVERRUN,CE_FRAME
EV_RING
检测到振铃
EV_RLSD
RLSD信号改变了状态
EV_RXCHAR
接收到了一个字符
EV_RXFLAG
接收到了一个事件字符
EV_TXEMPTY
在输出缓冲区中的最后一个字符被发送

https://blog.csdn.net/yangbingzhou/article/details/39611919



C/C++串口通信原理及读写与操作 (还行)
http://wangbaiyuan.cn/c-serial-communication-write-reading.html

Win32串行通信中文版(Serial Communications In Win32)
(好)
http://msdn.microsoft.com/en-us/library/ms810467.aspx 
https://blog.csdn.net/zcube/article/details/8591972



Win32串口编程(C++)(很好)
https://blog.csdn.net/zhongguoren666/article/details/7195498


win32串口编程 (排版很好)
https://www.cnblogs.com/Bonker/archive/2013/08/01/3229405.html



串口同步开:WaitCommEvent()会阻塞串口线程,等数据,用WaitForXXXObject()可等待你自定义的事件,该事件也可控制同异步
串口异步开: WaitCommEvent()非阻塞串口线程,用WaitForXXXObject()可等待你overlapped指定事件,判断异步操作是否完成,即实现串口线程阻塞

使用WaitForXXXObject函数可精确控制不同线程优先权切换,

WaitForMultipleObjects的作用是为了区分关闭、读、写这三个操作的
和串口本身的功能关系不大



比较好的代码
串口操作---C代码
https://blog.csdn.net/flydream0/article/details/7058931




https://sourceforge.net/projects/hypeterminal/

https://sourceforge.net/projects/y-a-terminal/


比较好 ,有表格框 ,可以看看 怎么实现 
https://www.codeproject.com/Articles/3061/Creating-a-Serial-communication-on-Win



http://www.naughter.com/serialport.html

You have not shown enough code to make clear what you are doing.  
There is no need to initiate a ReadFile while a ReadFile is pending.  
See if you can find the MTTTY serial port sample code in MSDN, 
or CSerialPort on the net.

For serial I/O on desktop versions of Windows ReadFile and WriteFile block each other if you do not use overlapped I/O.  So without overalapped I/O you cannot have full duplex serial communication.  As far as I know, the only place this is documented is in this article:

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

So use overlapped I/O

串口 全双工的话 ,需要 使用 overlapped I/O(不确定)

You'll likely do need overlapped I/O in the WaitCommEvent() call to prevent it from blocking the WriteFile() call.  
That's what .NET's SerialPort class does.  
ReadFile() won't be a problem, the EV_RXCHAR event reliably tells you that it won't block.


You must use the same OVERLAPPED in CreateFile and WaitCommEvent and ReadFile.  If WaitCommEvent tells you a character is available then ReadFile will return the character without suspending, but you still need the OVERLAPPED in ReadFile.

There should be no need to call SetCommMask for every write.  

Your overall approach of trying to read one character at a time is very inefficient and will give you problems at high baud rates.  
Much better to ask ReadFile for multiple bytes and also use SetCommTimeouts to make ReadFile return if no bytes come in for a while.  
Then use GetOverlappedResult to find out what you got.  

You will use much less CPU time if you set the ReadFile buffer size and timeouts so ReadFile only returns after several tens of milliseconds.

https://social.msdn.microsoft.com/Forums/en-US/17101d3e-21a2-4da7-b832-902ed9b5bc9a/only-overlapped-io-can-be-used-for-serial-communications-in-win-xp


You have not shown enough code to make clear what you are doing.  There is no need to initiate a ReadFile while a ReadFile is pending.  See if you can find the MTTTY serial port sample code in MSDN, or CSerialPort on the net.

The latency issue is simply how long you are willing to wait until the results of ReadFile are available.  If you attempt to execute upon every received character you will use excessive CPU time.  So you select an acceptable delay before ReadFile completes.  This delay is the latency.  It is set two ways: By specifying the ReadFile buffer size (== delay until completion if data is coming in) and the SetCommTimeouts (== delay until completion if data stops coming in).





 (好)
 https://social.msdn.microsoft.com/Forums/en-US/a3782aab-12dc-4db5-827c-935a3377e289/rs232-communication-in-visual-c






 The following links are some good code examples which tell about RS232 communication.

This class is meant as a very simple alternative to the more robust, feature-rich CSerialPort class presented by Remon Spekreijse. In other words, if you need a very simple class to read or write data to the serial port, then this class might be perfect for you.

http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503/

This article tells about a high-performance , complete and compact serial library for c++.
http://www.codeproject.com/KB/system/serial.aspx
CSerialPort is a freeware MFC class to wrap access to the Win32 APIs dealing with serial ports.

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

The purpose of this article is to demonstrate the use of Win32 functions for serial port communication in Visual C++. http://www.codeproject.com/KB/system/SerialPortComm.aspx

This class is meant as a very simple alternative to the more robust, feature-rich CSerialPort class presented by Remon Spekreijse. In other words, if you need a very simple class to read or write data to the serial port, then this class might be perfect for you.

http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503/



Best regards,

Lucy

https://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503/CSerial--A-C-Class-for-Serial-Communications.htm


the fix for that COM10 and beyond
Posted by jamez on 06/02/2016 12:28pm
https://support.microsoft.com/en-us/kb/115831


好
http://gopinaths.gitlab.io/post/serial_uart/


Non Overlapped Serial Port Communication using Win32
https://www.codeproject.com/Articles/8860/Non-Overlapped-Serial-Port-Communication-using-Win


MTTTY (Multi-Threaded TTY)
高效 Win 32 串行通信程序 MTTTY (Multi-Threaded TTY)
https://download.csdn.net/download/clearaelc/7738951

https://msdn.microsoft.com/en-us/library/ff802693.aspx 
MSDN 串口官方例程,纯C语言,非常好

HOWTO: Specify Serial Ports Larger than COM9


https://support.microsoft.com/en-us/help/115831/howto-specify-serial-ports-larger-than-com9

reateFile() 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.

CSerial - A C++ Class for Serial Communications

https://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503/CSerial--A-C-Class-for-Serial-Communications.htm

This class is meant as a very simple alternative to the more robust, feature-rich CSerialPort class presented by Remon Spekreijse. In other words, if you need a very simple class to read or write data to the serial port, then this class might be perfect for you. However, if you need more control over just how the serial communications is to be conducted, then Remon’s very fine class will probably be what you want.

一本参考书:(看第3章)

The Windows Serial Port Programming Handbook
作者:Ying Bai

https://books.google.co.jp/books?id=iJzje_1tJA4C&pg=PA189&lpg=PA189&dq=SetCommMask+EV_RXCHAR&source=bl&ots=hLcVXs8lMO&sig=xAYnv00BhWbK_aI9gNqAjhihxUA&hl=zh-CN&sa=X&ved=0ahUKEwj5hO_5vL7bAhVHerwKHfMnBKMQ6AEITjAF#v=onepage&q=SetCommMask%20EV_RXCHAR&f=false

win32 API 开发串口 参考资料_第1张图片

下载地址:
包括:The Windows Serial Port Programming Handbook.pdf
和 对应的代码

https://download.csdn.net/download/wowocpp/10462514

你可能感兴趣的:(串口)