https://www.aliexpress.com/item/32990413968.html
RS232 시리얼포트/196; 1228;Ǻ12;.104;lj16; 4채널 릴/112;1060; 1228;품1012; 샀습니다.
https://onedrive.live.com/redir?resid=3DFF9FAEE8435679!15927&authkey=!AG1AzoSlnv7JM4g&ithint=file%2crar
그런데 1228;공Ȣ16;lj16; 소프트웨Ǻ12;lj16; 윈도우용 C#1004;/196; 1676;여1652; 코드 1104;1060;네요.
단순Ȣ16;,172; 그냥 161652;수값1012; writeȢ16;면 .104;lj16;,152;/196; 생각Ȣ16;고 리눅스에서 C코드를 1089;성Ȣ16;였1004;나 ᇼ1;1089;1060; 안.104;네요.
무Ǻ16;가 IC를 1228;Ǻ12;Ȣ16;lj16; 별도1032; 라1060;브러리가 필요한,163;1068;까요?
-------------
#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 160; 160;"/dev/ttyS0"
static int fd3;
// 시리얼포트 속성처리용 구1312;체
struct termios newtio3;
int serial_init(void)
{
160; 160; fd3 = open(COM3, O_RDWR | O_NOCTTY);
160; 160; // COM1 시리얼 속성 셋ᔚ1;
160; 160; memset(&newtio3, 0, sizeof(newtio3));
160; 160; cfsetispeed(&newtio3, B9600);
160; 160; cfsetospeed(&newtio3, B9600);
160; 160; newtio3.c_cflag |= PARENB;
160; 160; newtio3.c_cflag &= ~PARODD;
160; 160; newtio3.c_cflag &= ~CSTOPB;
160; 160; newtio3.c_cflag &= ~CSIZE;
160; 160; newtio3.c_cflag |= CS8;
160; 160; newtio3.c_cflag |= CLOCAL;
160; 160; newtio3.c_cflag |= CREAD;
160; 160; newtio3.c_iflag = IGNPAR;
160; 160; newtio3.c_oflag = 0;
160; 160; newtio3.c_lflag = 0;
160; 160; newtio3.c_cc[VTIME] = 0;
160; 160; newtio3.c_cc[VMIN] = 1;
160; 160; tcflush (fd3, TCIFLUSH);
160; 160; tcsetattr(fd3, TCSANOW, &newtio3);
160; 160; return 0;
}
int main(int argc, char* argv[])
{
160; 160; int bytes, i;
160; 160; char buf[4][8];
160; 160; char momentary[8];
160; 160; serial_init();
160; 160; memset(momentary, 0x00, 8);
160; 160; momentary[0] = 0x55; momentary[1] = 0x56; momentary[2] = 0x00; momentary[3] = 0x00;
160; 160; momentary[4] = 0x00; momentary[5] = 0x01; momentary[6] = 0x04; momentary[7] = 0xB0;
160; 160; //setvbuf(stdout, NULL, _IOFBF, 0);
160; 160; bytes = write(fd3, momentary, 8);
#if 0
160; 160; for(i=0;i<4;i++)
160; 160; {
160; 160; 160; 160; memset(buf[i], 0x00, 8);
160; 160; }
160; 160; // Relay Open
160; 160; 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;
160; 160; 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;
160; 160; 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;
160; 160; 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;
160; 160; for(i=0;i<4;i++)
160; 160; {
160; 160; 160; 160; write(fd3, buf[i], 8);
160; 160; 160; 160; sleep(1);
160; 160; }
160; 160; // Relay Close
160; 160; 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;
160; 160; 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;
160; 160; 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;
160; 160; 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;
160; 160; for(i=0;i<4;i++)
160; 160; {
160; 160; 160; 160; write(fd3, buf[i], 8);
160; 160; 160; 160; sleep(1);
160; 160; }
#endif
160; 160; close(fd3);
160; 160; return 0;
}
À©µµ¿ì10¿¡¼´Â Àß µ¿À۵dz׿ä.
¾Æ·¡Ã³·³ µÇ¾î Àִµ¥..
Æ÷Æ®¹øÈ£ ¼³Á¤¸¸ ÀÖ°í ´Ù¸¥ °Ç ¾ø´Â °Å º¸´Ï c# ¿¡¼ »ç¿ëÇÏ´Â µðÆúÆ® ¼³Á¤ÀÎ µí ÇÕ´Ï´Ù.
vs c# ¿¡¼ µðÆúÆ® ¼³Á¤ À̹ÌÁö¸¦ ¿Ã¸®´Ï Âü°íÇÏ¿© c ¿¡¼µµ ¼³Á¤ÀÌ °°°Ô µÇ¾î ÀÖ´ÂÁö È®ÀÎÇغ¸¼¼¿ä.
[https://i.imgur.com/Hcf1BHO.png]
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;
}