본문 바로가기
Window Tool

[C][윈도우] Serial 통신 (Relay 제어)

by 暻煥 2024. 2. 5.

개발 및 작업을 하다 보면

전원을 Control 해야 할 일이 생긴다.

 

220v 전원에 연결해서

저렴한 가격에 USB를 이용해서 제어할 수 있는 Relay는 시중에 다양하다.

검색창에 "1 Channel USB Relay"라고 검색하고

http://vctec.co.kr/product/1%EC%B1%84%EB%84%90-usb-%EB%A6%B4%EB%A0%88%EC%9D%B4-10a-1-channel-usb-relay-10a/14806/

 

1채널 USB 릴레이 -10A (1 Channel USB Relay -10A) - 가치창조기술

Step1: the USB relay module into the computer, install CH340 USB to serial chip driver Step 2: Open the STC-ISP, SSCOM32 serial debugging software, select the baud rate of 9600, in hexadecimal (hex) form to send A0 01 01 A2 to open the relay; hex (hex) for

vctec.co.kr

 

나는 해당 제품을 구매했다,

전기/전자 관련 공부를 해본 적이 없다면 NC, NO, COM 각각 무슨 뜻인가 싶겠지만

 

 

그냥 com은 항상 연결되어 있는 선이고

나머지 2개를 각각 스위칭 할 수 있는 선이다.

 

어쨌든

해당 제품의 설명을 보면

Open USB switch: A0 01 01 A2
Close USB switch: A0 01 00 A1

 

Close와 Open으로 각각 스위칭 할 수 있는 Serial 명령어가 나와 있다.

 

아두이노 라이브러리를 이용해서 간단하게

Close 및 Open 시킬 수 있는 윈도우 C 프로그램을 만들어 보자.

 

(라이브러리 출처 : https://playground.arduino.cc/Interfacing/CPPWindows/ )

 

Arduino Playground - CPPWindows

Interfacing... Arduino and C++ (for Windows) As I found it pretty hard finding the good information, or an already working code to handle Serial communication on windows based system, I finally made a class that do what is needed for basic Serial Communica

playground.arduino.cc

 

/*
 * My_Close_Open.cpp
 */

#include <stdio.h>
#include <tchar.h>
#include <string>

#include "SerialClass.h"

char circuite_close_cmd[] = { (char)0xA0, (char)0x01, (char)0x00, (char)0xA1 };
char circuite_open_cmd[] = { (char)0xA0, (char)0x01, (char)0x01, (char)0xA2 };

int main()
{
	printf("Welcome to the serial test app!\n\n");

	// com 번호 입력
	Serial* SP = new Serial("\\\\.\\COM9");

	if (SP->IsConnected())
		printf("We're connected");

	/* 읽기 동작 방법
	// 입력 버퍼 (읽기 동작)
	char incomingData[256] = "";
	int dataLength = 255;
	int readResult = 0;

	// 읽기 동작시 다음 API 사용
	readResult = SP->ReadData(incomingData, dataLength);
	printf("%s", incomingData);
	*/

	if (SP->IsConnected())
	{
		// open 명령
		SP->WriteData(circuite_open_cmd, sizeof(circuite_open_cmd) / sizeof(char));

		// close 명령
		SP->WriteData(circuite_close_cmd, sizeof(circuite_close_cmd)/sizeof(char));

	}
	return 0;
}
/*
 * SerialClass.h
 */

#ifndef SERIALCLASS_H_INCLUDED
#define SERIALCLASS_H_INCLUDED

#define ARDUINO_WAIT_TIME 2000

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

class Serial
{
private:
    //Serial comm handler
    HANDLE hSerial;
    //Connection status
    bool connected;
    //Get various information about the connection
    COMSTAT status;
    //Keep track of last error
    DWORD errors;

public:
    //Initialize Serial communication with the given COM port
    Serial(const char* portName);
    //Close the connection
    ~Serial();
    //Read data in a buffer, if nbChar is greater than the
    //maximum number of bytes available, it will return only the
    //bytes available. The function return -1 when nothing could
    //be read, the number of bytes actually read.
    int ReadData(char* buffer, unsigned int nbChar);
    //Writes data from a buffer through the Serial connection
    //return true on success.
    bool WriteData(const char* buffer, unsigned int nbChar);
    //Check if we are actually connected
    bool IsConnected();


};

#endif // SERIALCLASS_H_INCLUDED
/*
 * Serial.cpp
 */
 
 #include "SerialClass.h"

Serial::Serial(const char* portName)
{
    //We're not yet connected
    this->connected = false;

    //Try to connect to the given port throuh CreateFile
    this->hSerial = CreateFileA(portName,
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    //Check if the connection was successfull
    if (this->hSerial == INVALID_HANDLE_VALUE)
    {
        //If not success full display an Error
        if (GetLastError() == ERROR_FILE_NOT_FOUND) {

            //Print Error if neccessary
            printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);

        }
        else
        {
            printf("ERROR!!!");
        }
    }
    else
    {
        //If connected we try to set the comm parameters
        DCB dcbSerialParams = { 0 };

        //Try to get the current
        if (!GetCommState(this->hSerial, &dcbSerialParams))
        {
            //If impossible, show an error
            printf("failed to get current serial parameters!");
        }
        else
        {
            //Define serial connection parameters for the arduino board
            dcbSerialParams.BaudRate = CBR_9600;
            dcbSerialParams.ByteSize = 8;
            dcbSerialParams.StopBits = ONESTOPBIT;
            dcbSerialParams.Parity = NOPARITY;
            //Setting the DTR to Control_Enable ensures that the Arduino is properly
            //reset upon establishing a connection
            dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;

            //Set the parameters and check for their proper application
            if (!SetCommState(hSerial, &dcbSerialParams))
            {
                printf("ALERT: Could not set Serial Port parameters");
            }
            else
            {
                //If everything went fine we're connected
                this->connected = true;
                //Flush any remaining characters in the buffers 
                PurgeComm(this->hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
                //We wait 2s as the arduino board will be reseting
                Sleep(ARDUINO_WAIT_TIME);
            }
        }
    }

}

Serial::~Serial()
{
    //Check if we are connected before trying to disconnect
    if (this->connected)
    {
        //We're no longer connected
        this->connected = false;
        //Close the serial handler
        CloseHandle(this->hSerial);
    }
}

int Serial::ReadData(char* buffer, unsigned int nbChar)
{
    //Number of bytes we'll have read
    DWORD bytesRead;
    //Number of bytes we'll really ask to read
    unsigned int toRead;

    //Use the ClearCommError function to get status info on the Serial port
    ClearCommError(this->hSerial, &this->errors, &this->status);

    //Check if there is something to read
    if (this->status.cbInQue > 0)
    {
        //If there is we check if there is enough data to read the required number
        //of characters, if not we'll read only the available characters to prevent
        //locking of the application.
        if (this->status.cbInQue > nbChar)
        {
            toRead = nbChar;
        }
        else
        {
            toRead = this->status.cbInQue;
        }

        //Try to read the require number of chars, and return the number of read bytes on success
        if (ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL))
        {
            return bytesRead;
        }

    }

    //If nothing has been read, or that an error was detected return 0
    return 0;

}


bool Serial::WriteData(const char* buffer, unsigned int nbChar)
{
    DWORD bytesSend;

    //Try to write the buffer on the Serial port
    if (!WriteFile(this->hSerial, (void*)buffer, nbChar, &bytesSend, 0))
    {
        //In case it don't work get comm error and return false
        ClearCommError(this->hSerial, &this->errors, &this->status);

        return false;
    }
    else
        return true;
}

bool Serial::IsConnected()
{
    //Simply return the connection status
    return this->connected;
}

※ Relay 접점의 의미

https://blog.naver.com/PostView.nhn?blogId=elepartsblog&logNo=221470372661&categoryNo=23&parentCategoryNo=0&viewDate=¤tPage=1&postListTopCurrentPage=1&from=postView

 

스위치,릴레이의 a접점,b접점 (NO, NC)의 의미

스위치/릴레이의 접점 동작 구조를 표시하는 방법 중에는 a 접점, b 접점 등이 있습니다. 접점은 종류에 따...

blog.naver.com

.