C++ RS485 串口通信:解决 "Serial=COM11,Error opening port: 5" 错误
#pragma once#include <stdio.h>#include <stdlib.h>#include <wtypes.h>#include <windows.h>#include <cstdint>#include <string>#include <vector>#include <atlconv.h>\using namespace std;\n\nint main()\n{\n\n\t//RS485通信,串口接收数据\n\t\n\t//查看所有串口信息\n\tvector<string> strCom;\n\tchar strTemp[255];\n\tHANDLE hCom;\n\tfor (int i = 0; i < 255; i++)\n\t{\n\t //strTemp.Format("#\.\COM%d", i + 1);\n\t sprintf_s(strTemp, "#\.\COM%d", i + 1);\n\t USES_CONVERSION;\n\t hCom = CreateFile(A2W(strTemp), 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);\n\t if (INVALID_HANDLE_VALUE == hCom)\n\t continue;\n\t strCom.push_back(strTemp);\n\t CloseHandle(hCom);\n\t}\n\n\t//打开设备\n\t/\n\tHANDLE hHandle = CreateFile(L"#\.\COM11", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);\n\tif (hHandle == INVALID_HANDLE_VALUE)\n\t{\n\t printf("Serial=COM11,Error opening port");\n\t return 0;\n\t}\n\t/\n\tHANDLE hHandle = CreateFile(L"#\.\COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);\n\tif (hHandle == INVALID_HANDLE_VALUE)\n\t{\n\t DWORD dwError = GetLastError();\n\t LPVOID lpMsgBuf;\n\t FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,\n\t NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);\n\t //printf("Serial=COM11,Error opening port: %s", (LPCWSTR)lpMsgBuf);\n\t printf("Serial=COM11,Error opening port: %d", dwError);\n\t LocalFree(lpMsgBuf);\n\t return 0;\n\t}\n\n\t//获取设备属性值+修改+设置\n\tDCB mDCB;\n\tGetCommState(hHandle, &mDCB);\n\tmDCB.Parity = NOPARITY;\n\tmDCB.ByteSize = 8;\n\tmDCB.StopBits = ONESTOPBIT;\n\tmDCB.BaudRate = CBR_115200;\n\n\tSetCommState(hHandle, &mDCB);\n\n\t//设置串口写和读的缓冲区大小\n\tSetupComm(&mDCB, 1024, 1024);\n\n\t//设置超时时间\n\tCOMMTIMEOUTS TimeOuts;\n\t//设定读超时\n\tTimeOuts.ReadIntervalTimeout = MAXDWORD;//读间隔超时\n\tTimeOuts.ReadTotalTimeoutMultiplier = 0;//读时间系数\n\tTimeOuts.ReadTotalTimeoutConstant = 0;//读时间常量\n\t//设定写超时\n\tTimeOuts.WriteTotalTimeoutMultiplier = 1;//写时间系数\n\tTimeOuts.WriteTotalTimeoutConstant = 1;//写时间常量\n\tSetCommTimeouts(hCom, &TimeOuts); //设置超时\n\n\t//在发送和接收前,清空渡河写的缓冲区域\n\tPurgeComm(&mDCB, PURGE_TXCLEAR | PURGE_RXCLEAR);\n\n\t//开始接收串口数据\n\tDWORD wCount; //读取的字节数\n\tunsigned char lpInBuffer[1024];\n\tint nCount = 0;\n\twhile (1) {\n\t if (!ReadFile(hHandle, lpInBuffer, 1024, &wCount, 0)) {\n\t printf("error:%d\n", GetLastError());\n\t }\n\t else {\n\t if (wCount != 0)\n\t {\n\t printf("Info%d:", nCount);\n\t nCount++;\n\t for (int i = 0; i < wCount; i++) printf("%x ", lpInBuffer[i]);\n\t printf("\n");\n\t }\n\t else\n\t {\n\t Sleep(1000);\n\t }\n\t }\n\t}\n\n\t//关闭设备句柄\n\tCloseHandle(hHandle);\n\n\treturn 0;\n}\nCOM1串口已经建立,上述代码运行结果为Serial=COM11,Error opening port: 5,如何解决?\n内容:错误代码5表示访问被拒绝,这可能是由于以下原因导致的:\n\n1. 串口COM1已被其他程序或进程占用。请确保没有其他程序正在使用COM1串口。\n\n2. 使用管理员权限运行程序。尝试以管理员权限运行程序,以便获得对串口的访问权限。\n\n3. 检查串口设备是否正常。可以尝试使用其他串口设备进行测试,确保串口设备正常工作。\n\n如果以上解决方法不起作用,可能需要进一步检查系统设置和串口驱动程序是否正确安装。\n
原文地址: https://www.cveoy.top/t/topic/qaxv 著作权归作者所有。请勿转载和采集!