串行通信C++选择连接

epggiuax  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(155)

主要的

#include "stdafx.h"
#include <iostream>
#include "SerialPort.h"
#include <WinBase.h>


using namespace std;

int main()
{
    cout << "Monitoring System \n";
    cout << "Please select a connection method\n\n";
    cout << "1.Serial   2.TCP \n";

    int num;

    while (!(cin >> num))
    {
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Please enter a number only   ";
    }
        
    if(num==1)
    {   
        int i,SN;
        int k=0;
        
        cout << "Scan all ports that are currently...\n";
        
        
            for (i = 1; i < 30; ++i)
            {

            if (COM_exists(i))
            {
                cout <<++k<< ".COM" << i << "\n";
            }
                
            }
            cout << "\nselect a port to connect.\n";
            scanf_s("%d", &SN, sizeof SN);

            if (SN == 1)
            {
                SerialPort PortOpen("COM", CBR_115200, 8, "#or", 24);

                PortOpen.getData();
                PortOpen.getData();
                cout << PortOpen.SerialBuffer << endl;
                return 0;
            }

        

    }





    

}

串行cpp

#include "stdafx.h"
#include <string.h>
#include "SerialPort.h"



using namespace std;


BOOL COM_exists(int port)
{
    char buffer[7];
    COMMCONFIG CommConfig;
    DWORD size;

    if (!(1 <= port && port <= 30))
    {
        return FALSE;
    }

    snprintf(buffer, sizeof buffer, "COM%d", port);
    size = sizeof CommConfig;

    
    // COM port exists if GetDefaultCommConfig returns TRUE
    // or changes <size> to indicate COMMCONFIG buffer too small.
    return (GetDefaultCommConfig(buffer, &CommConfig, &size)
        || size > sizeof CommConfig);
}

SerialPort::SerialPort(LPCSTR COM,int setBaudRate, int setByteSize, char* commandBuffer, int BufSize) {
    this->BufSize = BufSize;
    this->SerialBuffer = new char[BufSize];
    this->NoBytesRead = BufSize;
    Connect(COM, setBaudRate, setByteSize, commandBuffer);
}



void SerialPort::Connect(LPCSTR COM ,int setBaudRate, int setByteSize, char* commandBuffer) {
    this->COMport = COM;
    this->hComm = CreateFile(COM, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); // (port name, read/write , no sharing , no security, open existing port only, non overlapped i/o , null for comm devices)

    if (this->hComm == INVALID_HANDLE_VALUE)
        cout << "PortOpen Fail." << endl;

    else {
        cout << "PortOpen Success." << endl;
        sendCommand(setBaudRate, setByteSize, commandBuffer);
    }
}

void SerialPort::sendCommand(int setBaudRate, int setByteSize, char* commandBuffer) {
    this->commandBuffer = commandBuffer;
    this->dNoOFBytestoWrite = sizeof(commandBuffer);
    this->Status = WriteFile(hComm, commandBuffer, dNoOFBytestoWrite, &dNoOfBytesWritten, NULL);

    DCB dcbSerialParams = { 0 };
    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    dcbSerialParams.BaudRate = setBaudRate;
    dcbSerialParams.ByteSize = setByteSize;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;

    COMMTIMEOUTS timeouts = { 0 };
    timeouts.ReadIntervalTimeout = 10;
    timeouts.ReadTotalTimeoutConstant = 10;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 10;

    if (this->Status)
        cout << "Command Successful" << endl;
    else
    {
        cout << "fail to Command" << endl;
    }
}

void SerialPort::getData() {
    ReadFile(this->hComm, &*this->SerialBuffer, this->BufSize, &this->NoBytesRead, NULL);
    
}

SerialPort.h

#pragma once

#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <WinBase.h>

using namespace std;

class SerialPort {
    public:
        HANDLE hComm;
        LPCSTR COMport;
        DWORD dNoOFBytestoWrite;
        DWORD dNoOfBytesWritten;
        int BufSize;
        int Status;
        char* SerialBuffer;
        char* commandBuffer;
        DWORD NoBytesRead;
        int port;

        SerialPort(LPCSTR COMport,int setBaudRate, int setByteSize, char* commandBuffer, int BufSize);
        void Connect(LPCSTR COMport,  int setBaudRate, int setByteSize, char* commandBuffer);
        void sendCommand(int setBaudRate, int setByteSize, char* commandBuffer);
        void getData();
        
};

BOOL COM_exists(int port);

我正在写一个代码,通过选择一个串行端口来连接。例如,1.COM1 / 2.COM3 / 3.COM4我如何修改main来连接与3号端口匹配的COM 4?我如何自动读取并使用匹配的端口来匹配数字?
我正在写一个代码,通过选择一个串行端口来连接。例如,1.COM1 / 2.COM3 / 3.COM4我如何修改main来连接与3号端口匹配的COM 4?我如何自动读取并使用匹配的端口来匹配数字?

ijxebb2r

ijxebb2r1#

我个人永远不会使用老式的打开和关闭COM端口来检测它是否存在。假设你在Windows上运行,我会使用SetupDi来找到串行端口而不打开它。
下面是一个小片段:

#include <initguid.h>
#include <windows.h>                    // Data Type
#include <setupapi.h>                   // ::SetupDi*********
#include <devguid.h>                    // Device

    // Global Variables
    HDEVINFO            m_hDevInfo;             //!< Reference to device information set
    SP_DEVINFO_DATA     m_spDevInfoData;        //!< Device information structure (references a device instance that is a member of a device information set)
    BOOL                m_bDevInfo;             //!< Tested to ensure EnumDeviceInfo has been called
    short               m_MemberIndex = -1;     //!< Preserves state between EnumDeviceInfo calls

bool WIN_EnumDeviceInfo ()
{
    m_MemberIndex++;
    m_spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    m_bDevInfo = ::SetupDiEnumDeviceInfo(m_hDevInfo, m_MemberIndex, &m_spDevInfoData);
    return m_bDevInfo;
}

bool WIN_GetDeviceRegistryProperty(DWORD Property, PBYTE PropertyBuffer)
{
    BOOL bGotRegProp = ::SetupDiGetDeviceRegistryProperty(m_hDevInfo, &m_spDevInfoData,
                                                  Property,
                                                  0L,
                                                  PropertyBuffer,
                                                  2048,
                                                  0);
    return bGotRegProp;
}

void ScanSerial()
{

    WORD    Flags;
    PCWSTR  Enumerator=0;
    const   GUID *ClassGuid;
    HWND    m_hWnd;

    ClassGuid = &GUID_DEVCLASS_PORTS;
    Flags = DIGCF_PROFILE;

    m_hDevInfo = SetupDiGetClassDevs(ClassGuid, Enumerator, m_hWnd, Flags);

    while(WIN_EnumDeviceInfo())
    {
        wchar_t  szBuf[MAX_PATH] = {0};
        if(WIN_GetDeviceRegistryProperty(SPDRP_CLASS, (PBYTE)szBuf))
        {

            wchar_t  szFriendlyName[MAX_PATH] = {0};
            WIN_GetDeviceRegistryProperty (SPDRP_FRIENDLYNAME,  (PBYTE)szFriendlyName);
        }
    }
}

从szFriendlyName中,您可以过滤COMx编号,而无需打开它或混淆它们...尝试并享受...

相关问题