"#pragma once"\n"#include <stdio.h>"\n"#include <stdlib.h>"\n"#include <wtypes.h>"\n"#include <windows.h>"\n"#include "\n"#include "\n"#include "\n"#include <atlconv.h>"\n"using namespace std;\n\nint main() {\n\n\t//RS485\u8054\u901a\uff0c\u5e42\u53e3\u63a5\u6536\u6570\u636e\n\n\t//\u67e5\u770b\u6240\u6709\u5e42\u53e3\u4fe1\u606f\n\tvector strCom;\n\tchar strTemp[255];\n\tHANDLE hCom;\n\tfor (int i = 0; i < 255; i++) {\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//\u6253\u5f00\u8bbe\u5907\n\t/\n\tHANDLE hHandle = CreateFile(L"\.\COM11", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);\n\t if (hHandle == INVALID_HANDLE_VALUE) {\n\t printf("Serial=COM11,Error opening port");\n\t return 0;\n\t }\n\t/\n\tHANDLE hHandle = CreateFile(L"\.\COM11", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);\n\t if (hHandle == INVALID_HANDLE_VALUE) {\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", (LPCTSTR)lpMsgBuf);\n\t LocalFree(lpMsgBuf);\n\t return 0;\n\t }\n\n\t//\u83b7\u53d6\u8bbe\u5907\u5c5e\u6027\u503c+\u4fee\u6539+\u8bbe\u7f6e\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//\u8bbe\u7f6e\u5e42\u53e3\u5199\u548c\u8bfb\u7684\u7f13\u51b2\u533a\u57df\u5927\u5c0f\n\tSetupComm(&mDCB, 1024, 1024);\n\n\t//\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\n\tCOMMTIMEOUTS TimeOuts;\n\t//\u8bbe\u5b9a\u8bfb\u8d85\u65f6\n\tTimeOuts.ReadIntervalTimeout = MAXDWORD;//\u8bfb\u95f4\u95f4\u8d85\u65f6\n\tTimeOuts.ReadTotalTimeoutMultiplier = 0;//\u8bfb\u65f6\u95f4\u7cfb\u6570\n\tTimeOuts.ReadTotalTimeoutConstant = 0;//\u8bfb\u65f6\u95f4\u5e38\u91cf\n\t//\u8bbe\u5b9a\u5199\u8d85\u65f6\n\tTimeOuts.WriteTotalTimeoutMultiplier = 1;//\u5199\u65f6\u95f4\u7cfb\u6570\n\tTimeOuts.WriteTotalTimeoutConstant = 1;//\u5199\u65f6\u95f4\u5e38\u91cf\n\tSetCommTimeouts(hCom, &TimeOuts); //\u8bbe\u7f6e\u8d85\u65f6\n\n\t//\u5728\u53d1\u9001\u548c\u63a5\u6536\u524d\uff0c\u6e05\u7a7a\u6e29\u6cb3\u5199\u7684\u7f13\u51b2\u533a\u57df\n\tPurgeComm(&mDCB, PURGE_TXCLEAR | PURGE_RXCLEAR);\n\n\t//\u5f00\u59cb\u63a5\u6536\u5e42\u53e3\u6570\u636e\n\tDWORD wCount; //\u8bfb\u53d6\u7684\u5b57\u8282\u6570\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 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 Sleep(1000);\n\t }\n\t }\n\t}\n\n\t//\u5173\u95ed\u8bbe\u5907\u53e5\u624b\n\tCloseHandle(hHandle);\n\n\treturn 0;\n}\n

C++ RS485 串口通信 - 打开串口COM11失败解决方法

原文地址: https://www.cveoy.top/t/topic/qawb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录