½Ã¸®¾óÆ÷Æ®¿ë ¸±·¹ÀÌ Á¦¾î.

   Á¶È¸ 3813   Ãßõ 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ÄÉÀ̺í·Î º¸µå°¡ ÀÛµ¿ÇÏ´ÂÁöºÎÅÍ È®ÀÎÇغ¸½Ã´Â°Ô ÁÁÀ»°Í°°³×¿ä.
     
½Ã¸®¾óÆ÷Æ® ´Þ¸° À©µµ¿ì ¸Ó½ÅÀÌ ÇöÀç ¾ø½À´Ï´Ù. @@
          
datafaber 2020-06
RS232ÄÉÀ̺íÀÌ Á¦´ë·Î ÀÛµ¿Çؼ­ Á¾´Ü±îÁö ÆÐŶÀÌ Àß ³¯¶ó°¡´ÂÁö È®½ÇÇÏÁö ¾ÊÀº »óÅ¿¡¼­´Â ¹é³¯ ÄÚµùÇغÁ¾ß ¾µ¸ð°¡ ¾ø½À´Ï´Ù. ¸ÕÀú HW¸¦ üũÇغ¸½ÃÁÒ. º¸µåÀÛµ¿À̶ó´ø°¡ RS232ÄÉÀ̺íÀÌ ¸é ·çÇÁ¹é Å×½ºÆ®µµ °¡´ÉÇÏ´Ï °Å±â¼­ºÎÅÍ ½ÃÀÛÇغ¸½Ã´Â°Ô ÁÁÀ»°Í°°³×¿ä.
               
¹æ±Ý À©µµ¿ì10 ¼³Ä¡ÇÏ°í ¸µÅ©¿¡ ÷ºÎµÈ C# ÇÁ·Î±×·¥À¸·Î Á¤»óÅ×½ºÆ®¸¦ ¸¶ÃƽÀ´Ï´Ù.
À©µµ¿ì10¿¡¼­´Â Àß µ¿À۵dz׿ä.
¹«¾Æ 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);
        }
    }
     
½Ã¸®¾ó Æ÷Æ® ¼³Á¤ÀÌ ²¿¿´´ø°Ô ¹®Á¦¿´½À´Ï´Ù.
´ÙÀ½°ú °°ÀÌ 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 997/5696
2015-12   1566708   ¹é¸Þ°¡
2014-05   5030167   Á¤ÀºÁØ1
2020-06   4212   Ç®·Îµå½Ã½ºÅÛ
2020-06   4015   epowergate
2020-06   2771   ³×À̽ºÅÍ
2020-06   7964   ÇãÀα¸¸¶Æ¾
2020-06   3794   ÇູÇϼ¼
2020-06   2640   ÄðÄðÄð
2020-06   2647   ¹«¾Æ
2020-06   2628   MinGi
2020-06   3814   ¿¥ºê¸®¿À
2020-06   2154   AplPEC
2020-06   2797   °ø¹é±â
2020-06   3388   ÇãÀα¸¸¶Æ¾
2020-06   4274   VSPress
2020-06   3810   moby
2020-06   5770   ¾Æ½º¶õ
2020-06   3855   ½ÅÀº¿Ö
2020-06   4087   º´Á¤°³¹Ì
2020-06   2573   Â÷³ÝÄÄÇ»ÅÍ
2020-06   2615   jang
2020-06   5893   Á¹¸®µª