使用Windows API实现一个简单的串口助手

2021-03-09 07:29

阅读:655

标签:nba   输入   wine   thread   microsoft   seh   存在   打开   src   

目录
  • 使用window API开发一个具有字符串收发功能的串口助手
    • 开发环境
    • 串口设备相关的API
    • 步骤
    • 实现代码
    • 收发测试图

使用window API开发一个具有字符串收发功能的串口助手

开发环境

  • Visual Studio 2015

串口设备相关的API

  • CreateFile
    参数详情见:https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea?redirectedfrom=MSDN

  • SetCommState
    参数详情见:https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setcommstate?redirectedfrom=MSDN

  • GetCommState
    参数详情见:https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcommstate?redirectedfrom=MSDN

  • ReadFile
    参数详情见:https://docs.microsoft.com/en-us/previous-versions/windows/embedded/ee489594(v=winembedded.80)?redirectedfrom=MSDN

  • WriteFile
    参数详情见:https://docs.microsoft.com/en-us/previous-versions/windows/embedded/ee490774(v=winembedded.80)?redirectedfrom=MSDN

  • PurgeComm
    参数详情见:https://docs.microsoft.com/en-us/previous-versions/windows/embedded/ee488020(v=winembedded.80)?redirectedfrom=MSDN

  • CloseHandle
    参数详情见:https://docs.microsoft.com/en-us/previous-versions/windows/embedded/ee490442(v=winembedded.80)?redirectedfrom=MSDN


// 函数原型 
HANDLE WINAPI CreateFile(
  _In_      LPCTSTR lpFileName,
  _In_      DWORD dwDesiredAccess,
  _In_      DWORD dwShareMode,
  _In_opt_  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  _In_      DWORD dwCreationDisposition,
  _In_      DWORD dwFlagsAndAttributes,
  _In_opt_  HANDLE hTemplateFile
);

BOOL WINAPI SetCommState(
  _In_  HANDLE hFile,
  _In_  LPDCB lpDCB
);

BOOL WINAPI GetCommState(
  _In_     HANDLE hFile,
  _Inout_  LPDCB lpDCB
);

BOOL ReadFile(
  HANDLE hFile,
  LPVOID lpBuffer,
  DWORD nNumberOfBytesToRead,
  LPDWORD lpNumberOfBytesRead,
  LPOVERLAPPED lpOverlapped
);

BOOL WriteFile( 
  HANDLE hFile, 
  LPCVOID lpBuffer, 
  DWORD nNumberOfBytesToWrite, 
  LPDWORD lpNumberOfBytesWritten, 
  LPOVERLAPPED lpOverlapped
);

BOOL PurgeComm(
  HANDLE hFile,
  DWORD dwFlags
);

BOOL CloseHandle(
  HANDLE hObject
);

步骤

  1. 创建一个设备句柄
  2. 创建一个设备文件
  3. 配置串口参数
  4. 创建读写线程
  5. 对设备文件进行读写
  6. 退出线程后关闭设备文件

实现代码


#define _CRT_SECURE_NO_WARNINGS
#include 
#include 

HANDLE hCom; // 句柄,用于初始化串口

DWORD WINAPI ThreadWrite(LPVOID lpParameter)
{
	char outputData[100] = { 0x00 }; // 输出数据缓存

	if (hCom == INVALID_HANDLE_VALUE)
	{
		puts("打开串口失败");
		return 0;
	}

	DWORD strLength = 0;
	while (1)
	{
		for (int i = 0; i  0)
		{
			printf("接收的数据为:%s\r\n", getputData);
			fflush(stdout);
		}
		Sleep(100);
	}
	return 0;
}

int main()
{
	// 初始化串口
	TCHAR *com_name = (TCHAR *)malloc(10 * sizeof(TCHAR));
	do
	{
		printf("请输入需要打开的串口号(示例:COM2):");
		scanf("%s",com_name);
		getchar();
		hCom = CreateFile(com_name, GENERIC_READ | GENERIC_WRITE, 
                        0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		if (hCom == INVALID_HANDLE_VALUE)
			printf("串口号不存在,请重新输入!\n");
		else
			break;
	} while (1);
	free(com_name);

	// 获取和设置串口参数
	DCB myDCB;
	myDCB.BaudRate = 115200;       // 波特率
	myDCB.Parity = NOPARITY;     // 校验位
	myDCB.ByteSize = 8;          // 数据位
	myDCB.StopBits = ONESTOPBIT; // 停止位
	SetCommState(hCom, &myDCB);  // 设置串口参数
	printf("baud rate is %d\n", (int)myDCB.BaudRate);

	// 线程创建
	HANDLE HRead, HWrite;
	HWrite = CreateThread(NULL, 0, ThreadWrite, NULL, 0, NULL);
	HRead = CreateThread(NULL, 0, ThreadRead, NULL, 0, NULL);

	while (1);

	CloseHandle(HRead);
	CloseHandle(HWrite);
        CloseHandle(hCom);
	return 0;
}

收发测试图

  • 备注(左边为自己开发的串口软件,右边为正点原子团队开发的XCOM V2.0串口上位机软件)

技术图片

使用Windows API实现一个简单的串口助手

标签:nba   输入   wine   thread   microsoft   seh   存在   打开   src   

原文地址:https://www.cnblogs.com/veis/p/12757813.html


评论


亲,登录后才可以留言!