新建头文件.h
内容如下:

#pragma once
#include<Windows.h>
extern void* g_Port;
#if 1
char* getErrorMsg(int code);
#endif

cpp

#include "global.h"
#include"pch.h"
void* g_Port = INVALID_HANDLE_VALUE; //串口句柄



char* getErrorMsg(int code)
{
switch (code)
{
case 0:
return "成功";
case 1:
return "失败";
case 2:
return "数据非法,报文格式错";
case 3:
return "报文长度错误";
case 4:
return "HASH校验错";
case 5:
return "指令不存在";
case 6:
return "类型不存在";
case 7:
return "保存失败";
default:
break;
}
return "";
}

发现项目编译时出现:

WINDOWS.H already included. MFC apps must not #include <Windows.h>

百度上说windows要在mfc库下

把cpp头文件顺序改成以下即可

#include"pch.h"
#include "global.h"