시리얼포트용 릴레이 제어.

   조회 4212   추천 0    

https://www.aliexpress.com/item/32990413968.html
RS232 시리얼포트로 제어되는 4채널 릴레이 제품을 샀습니다.

https://onedrive.live.com/redir?resid=3DFF9FAEE8435679!15927&authkey=!AG1AzoSlnv7JM4g&ithint=file%2crar
그런데 제공하는 소프트웨어는 윈도우용 C#으로 짜여진 코드 뿐이네요.

단순하게 그냥 16진수값을 write하면 되는걸로 생각하고 리눅스에서 C코드를 작성하였으나 동작이 안되네요.

무언가 IC를 제어하는 별도의 라이브러리가 필요한것일까요?

-------------


#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>

#define COM3    "/dev/ttyS0"

static int fd3;

// 시리얼포트 속성처리용 구조체
struct termios newtio3;

int serial_init(void)
{
    fd3 = open(COM3, O_RDWR | O_NOCTTY);

    // COM1 시리얼 속성 셋팅
    memset(&newtio3, 0, sizeof(newtio3));

    cfsetispeed(&newtio3, B9600);
    cfsetospeed(&newtio3, B9600);

    newtio3.c_cflag |= PARENB;
    newtio3.c_cflag &= ~PARODD;
    newtio3.c_cflag &= ~CSTOPB;
    newtio3.c_cflag &= ~CSIZE;
    newtio3.c_cflag |= CS8;

    newtio3.c_cflag |= CLOCAL;
    newtio3.c_cflag |= CREAD;
    newtio3.c_iflag = IGNPAR;
    newtio3.c_oflag = 0;
    newtio3.c_lflag = 0;
    newtio3.c_cc[VTIME] = 0;
    newtio3.c_cc[VMIN] = 1;

    tcflush (fd3, TCIFLUSH);
    tcsetattr(fd3, TCSANOW, &newtio3);

    return 0;
}

int main(int argc, char* argv[])
{
    int bytes, i;
    char buf[4][8];
    char momentary[8];

    serial_init();

    memset(momentary, 0x00, 8);

    momentary[0] = 0x55; momentary[1] = 0x56; momentary[2] = 0x00; momentary[3] = 0x00;
    momentary[4] = 0x00; momentary[5] = 0x01; momentary[6] = 0x04; momentary[7] = 0xB0;

    //setvbuf(stdout, NULL, _IOFBF, 0);

    bytes = write(fd3, momentary, 8);

#if 0
    for(i=0;i<4;i++)
    {
        memset(buf[i], 0x00, 8);
    }

    // Relay Open
    buf[0][0] = 0x55; buf[0][1] = 0x56; buf[0][2] = 0x00; buf[0][3] = 0x00; buf[0][4] = 0x00; buf[0][5] = 0x01; buf[0][6] = 0x01; buf[0][7] = 0xAD;
    buf[1][0] = 0x55; buf[1][1] = 0x56; buf[1][2] = 0x00; buf[1][3] = 0x00; buf[1][4] = 0x00; buf[1][5] = 0x02; buf[1][6] = 0x01; buf[1][7] = 0xAE;
    buf[2][0] = 0x55; buf[2][1] = 0x56; buf[2][2] = 0x00; buf[2][3] = 0x00; buf[2][4] = 0x00; buf[2][5] = 0x03; buf[2][6] = 0x01; buf[2][7] = 0xAF;
    buf[3][0] = 0x55; buf[3][1] = 0x56; buf[3][2] = 0x00; buf[3][3] = 0x00; buf[3][4] = 0x00; buf[3][5] = 0x04; buf[3][6] = 0x01; buf[3][7] = 0xB0;

    for(i=0;i<4;i++)
    {
        write(fd3, buf[i], 8);
        sleep(1);
    }

    // Relay Close
    buf[0][0] = 0x55; buf[0][1] = 0x56; buf[0][2] = 0x00; buf[0][3] = 0x00; buf[0][4] = 0x00; buf[0][5] = 0x01; buf[0][6] = 0x02; buf[0][7] = 0xAE;
    buf[1][0] = 0x55; buf[1][1] = 0x56; buf[1][2] = 0x00; buf[1][3] = 0x00; buf[1][4] = 0x00; buf[1][5] = 0x02; buf[1][6] = 0x02; buf[1][7] = 0xAF;
    buf[2][0] = 0x55; buf[2][1] = 0x56; buf[2][2] = 0x00; buf[2][3] = 0x00; buf[2][4] = 0x00; buf[2][5] = 0x03; buf[2][6] = 0x02; buf[2][7] = 0xB0;
    buf[3][0] = 0x55; buf[3][1] = 0x56; buf[3][2] = 0x00; buf[3][3] = 0x00; buf[3][4] = 0x00; buf[3][5] = 0x04; buf[3][6] = 0x02; buf[3][7] = 0xB1;

    for(i=0;i<4;i++)
    {
        write(fd3, buf[i], 8);
        sleep(1);
    }
#endif
    close(fd3);

    return 0;
}

짧은글 일수록 신중하게.
datafaber 2020-06
먼저 제공하는 소프트웨어와 RS232케이블로 보드가 작동하는지부터 확인해보시는게 좋을것같네요.
     
엠브리오 2020-06
시리얼포트 달린 윈도우 머신이 현재 없습니다. @@
          
datafaber 2020-06
RS232케이블이 제대로 작동해서 종단까지 패킷이 잘 날라가는지 확실하지 않은 상태에서는 백날 코딩해봐야 쓸모가 없습니다. 먼저 HW를 체크해보시죠. 보드작동이라던가 RS232케이블이 면 루프백 테스트도 가능하니 거기서부터 시작해보시는게 좋을것같네요.
               
엠브리오 2020-06
방금 윈도우10 설치하고 링크에 첨부된 C# 프로그램으로 정상테스트를 마쳤습니다.
윈도우10에서는 잘 동작되네요.
무아 2020-06
해당 c# 코드를 다운해서 보니
아래처럼 되어 있는데..
포트번호 설정만 있고 다른 건 없는 거 보니 c# 에서 사용하는 디폴트 설정인 듯 합니다.
vs c# 에서 디폴트 설정 이미지를 올리니 참고하여 c 에서도 설정이 같게 되어 있는지 확인해보세요.

public class SerialRelay
    {
        private System.IO.Ports.SerialPort port = new SerialPort();
        private string _lastError = string.Empty;
        private byte[] _sums = new byte[] {0xAB,0xAD,0xAE,0xAF,0xB0,0xAE,0xAF,0xB0,0xB1,0xAF,0xB0,0xB1,0xB2,0xB0,0xB1,0xB2,0xB3};
        byte[] b = new byte[8];

        public static String[] GetPorts()
        {
            return SerialPort.GetPortNames();
        }
        public string LastError
        {
            get { return _lastError; }
        }
        public SerialRelay()
        {
            b[0] = 0x55;
            b[1] = 0x56;
            b[2] = 0x00;
            b[3] = 0x00;
            b[4] = 0x00;
        }

        public bool Open(string portName)
        {
            _lastError = string.Empty;
            if (port.IsOpen) port.Close();
            port.PortName = portName;
            try
            {
                port.Open();
                return true;
            }
            catch(Exception ex)
            {
                _lastError = ex.Message;
            }
            return false;
        }
        public void SendCommand(RelayCommand command,int channel)
        {
            if (!port.IsOpen) return;
            if (channel < 1 || channel > 4) return;
            b[5] = Convert.ToByte(channel);
            b[6] = Convert.ToByte(command);
            b[7] = 0xAB;
            b[7] += b[5];
            b[7] += b[6];

            port.Write(b, 0, 8);
        }
    }
     
엠브리오 2020-06
시리얼 포트 설정이 꼬였던게 문제였습니다.
다음과 같이 C 코드를 수정하여 해결보았습니다. 감사합니다.

struct termios newtio3;

int serial_init(void)
{
    fd3 = open(COM3, O_RDWR | O_NOCTTY);

    newtio3.c_cflag = B9600;
    newtio3.c_cflag |= CS8;
    newtio3.c_cflag |= CLOCAL;
    newtio3.c_cflag |= CREAD;
    newtio3.c_iflag = 0;
    newtio3.c_oflag = 0;
    newtio3.c_lflag = 0;
    newtio3.c_cc[VTIME] = 0;
    newtio3.c_cc[VMIN] = 1;

    tcflush(fd3, TCIFLUSH);
    tcsetattr(fd3, TCSANOW, &newtio3);

    return 0;
}


QnA
제목Page 1035/5728
2014-05   5256231   정은준1
2015-12   1781199   백메가
2020-06   3295   MinGi
2020-06   4213   엠브리오
2020-06   2300   AplPEC
2020-06   3371   공백기
2020-06   3582   허인구마틴
2020-06   5196   VSPress
2020-06   4206   moby
2020-06   6371   아스란
2020-06   4177   신은왜
2020-06   4482   병정개미
2020-06   2830   차넷컴퓨터
2020-06   2824   jang
2020-06   6344   졸리뎁
2020-06   4969   불알왕
2020-06   4728   맑은여름
2020-06   4338   jang
2020-06   3091   나비z
2020-06   3992   witbox
2020-06   2873   monan
2020-06   6210   응무소주