博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用API操作串口
阅读量:5963 次
发布时间:2019-06-19

本文共 3522 字,大约阅读时间需要 11 分钟。

步骤:

  1、CreateFile,打开串口

  示例:g_hCOM = CreateFile(L"COM3", GENERIC_READ| GENERIC_WRITE, 0, NULL,  OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);  

  //必须用0,表示以独占的模式来占用串口

  //打开除文件之外的设备时,用 OPEN_EXISTING

  2、SetupComm,用来设置缓存大小

  3、SetCommTimeouts,设置读写的超时时间

  4、SetCommState,用来设置一些串口参数,如波特率,读取的字节数,是否奇偶检验。

  5、WriteFile,向串口发送数据

  6、ReadFile,从串口读数据

  7、采用异步的方式来获取数据,如IO完成端口

 

代码:

 

#include 
#include
#include
#include
using namespace std;HANDLE g_hCompletionPort, g_hCOM, g_hEvent;byte buff[1000];byte writeBuf = 0x1a;//这个数据是针对我这块板子的DWORD dwErr;UINT WINAPI WriteData(PVOID pParameter){ OVERLAPPED overLap; overLap.hEvent = 0; overLap.Offset = 0; overLap.OffsetHigh = 0; DWORD dwWrite; WriteFile(g_hCOM, &writeBuf, sizeof(writeBuf), &dwWrite, &overLap); DWORD dwTransLen = 0; DWORD dwKey = 0; LPOVERLAPPED poverLap; BOOL IsOk = GetQueuedCompletionStatus(g_hCompletionPort, &dwTransLen, &dwKey, &poverLap, INFINITE); if(IsOk) { cout << "Translate Num: " <
InternalHigh << endl; cout << "Write Success" << endl; SetEvent(g_hEvent); } else { dwErr = GetLastError(); cout << "Write Failed. Code = " << dwErr << endl; } return 0;}UINT WINAPI ReadData(PVOID pParame){ //SetCommMask(g_hCOM, EV_RXCHAR | EV_TXEMPTY); OVERLAPPED overLap; overLap.hEvent = 0; overLap.Offset = 0; overLap.OffsetHigh = 0; ReadFile(g_hCOM, buff, 1000, 0, &overLap); DWORD dwTransLen = 0; DWORD dwKey = 0; LPOVERLAPPED poverLap = {0}; BOOL IsOk = GetQueuedCompletionStatus(g_hCompletionPort, &dwTransLen, &dwKey, &poverLap, INFINITE); if(IsOk) { cout << "Read Success." << endl; cout << "The Length is : " << poverLap->InternalHigh << endl; for (int i = 0; i < 10; i++) { printf("%02X ", buff[i]); } cout << endl << endl; } else { dwErr = GetLastError(); cout << "Read failed. Code = : " << dwErr << endl; } SetEvent(g_hEvent); return 0;}void main(){ g_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); //创建IO完成端口 g_hCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 0); //打开文件 g_hCOM = CreateFile(L"COM3", GENERIC_READ| GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); //将该设备(文件)与IO完成端口相关联,以获得通知 HANDLE h = CreateIoCompletionPort(g_hCOM, g_hCompletionPort, NULL, 0); if(h == g_hCompletionPort) cout << "关联成功" << endl; else { dwErr = GetLastError(); cout << "关联失败 : " << dwErr << endl; }// SetCommMask(g_hCOM, EV_RXCHAR | EV_TXEMPTY); SetupComm(g_hCOM, 1024, 512); PurgeComm(g_hCOM, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); COMMTIMEOUTS CommTimeout; CommTimeout.ReadIntervalTimeout = 5; CommTimeout.ReadTotalTimeoutConstant = 1000; CommTimeout.ReadTotalTimeoutMultiplier = 5; CommTimeout.WriteTotalTimeoutConstant = 1000; CommTimeout.WriteTotalTimeoutMultiplier = 5; SetCommTimeouts(g_hCOM, &CommTimeout); DCB dcb; GetCommState(g_hCOM, &dcb); dcb.BaudRate = 4800; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.fBinary = TRUE; dcb.fParity = FALSE; SetCommState(g_hCOM, &dcb); HANDLE hThread[20]; hThread[0] = (HANDLE)_beginthreadex(NULL, 0, WriteData, NULL, 0, NULL); for (int i = 1; i < 20; i++) { WaitForSingleObject(g_hEvent, INFINITE); hThread[i] = (HANDLE)_beginthreadex(NULL, 0, ReadData, NULL, 0, NULL); ResetEvent(g_hEvent); } WaitForMultipleObjects(20, hThread, TRUE, INFINITE); CloseHandle(g_hCOM); CloseHandle(g_hCompletionPort); CloseHandle(g_hEvent);}

 

 

遇到的问题:

1、向串口写数据的时候最好用字节来写。

 

 

转载于:https://www.cnblogs.com/wang-can/p/3338020.html

你可能感兴趣的文章
Linux Namespace系列(09):利用Namespace创建一个简单可用的容器
查看>>
博客搬家了
查看>>
Python中使用ElementTree解析xml
查看>>
jquery 操作iframe、frameset
查看>>
解决vim中不能使用小键盘
查看>>
jenkins权限管理,实现不同用户组显示对应视图views中不同的jobs
查看>>
我的友情链接
查看>>
批量删除用户--Shell脚本
查看>>
Eclipse Java @Override 报错
查看>>
知道双字节码, 如何获取汉字 - 回复 "pinezhou" 的问题
查看>>
Python高效编程技巧
查看>>
js中var self=this的解释
查看>>
Facebook 接入之获取各个配置参数
查看>>
linux的日志服务器关于屏蔽一些关键字的方法
查看>>
事情的两面性
查看>>
只要会营销,shi都能卖出去?
查看>>
sed单行处理命令奇偶行输出
查看>>
VC++深入详解学习笔记1
查看>>
安装配置discuz
查看>>
线程互互斥锁
查看>>