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;
}
À©µµ¿ì10¿¡¼´Â Àß µ¿À۵dz׿ä.
¾Æ·¡Ã³·³ µÇ¾î Àִµ¥..
Æ÷Æ®¹øÈ£ ¼³Á¤¸¸ ÀÖ°í ´Ù¸¥ °Ç ¾ø´Â °Å º¸´Ï 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;
}