PLC XGCommLib

jiyul·2024년 1월 3일
0

PLC

목록 보기
10/10

XG5000이 설치되어 있어야 함

  1. Visual Studio Project의 Dependencies(종속성) 항목 우클릭 > Add Project Reference
  2. COM 카테고리에서 XGCommLib 선택 및 추가

화면

코드 부분

using XGCommLib;


namespace XGCommLib
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            CommObjectFactory20 factory = new CommObjectFactory20();
        }

        private void Button_Connect_Click(object sender, RoutedEventArgs e)
        {
            //연결
            CommObjectFactory20 factory = new CommObjectFactory20();
            CommObject20 oCommDriver = factory.GetMLDPCommObject20("192.168.1.33:2004");

            if (0 == oCommDriver.Connect(""))
            {
                MessageBox.Show("연결 실패");
            }
            else
            {
                MessageBox.Show("연결 성공");
            }

            //버퍼 초기화
            int nMaxBuf = 1400;
            byte[] bufWrite = new byte[nMaxBuf];

            int nTotal_len = 0;

            //데이터 설정
            XGCommLib.DeviceInfo oDevice = factory.CreateDevice();

            ComboBoxItem item = (ComboBoxItem)ComboBox_DataType.SelectedValue;
            char item_name = item.Content.ToString()[0];
            oDevice.ucDataType = (byte)item_name;

            item = (ComboBoxItem)ComboBox_DeviceType.SelectedValue;
            item_name = item.Content.ToString()[0];
            oDevice.ucDeviceType = (byte)item_name;

            //if문으로 X,B,W를 나눌려고 코드를 추가하니 오류가 발생했다.
            //위의 item과 item_name도 사실 없어도 된다.
            //데이터 타입은 'X' 디바이스 타입은 'M'으로 직접 설정하면된다.
            //8로 나누는 이유는 1byte = 8bit이기 때문이다.
            if (oDevice.ucDataType == (byte)'X')
            {
                int sum = int.Parse(TextBox_ByteOffset.Text) + int.Parse(TextBox_BitOffset.Text);
                oDevice.lOffset = sum / 8;
                oDevice.lSize = sum % 8;
            }

            oDevice.lOffset = int.Parse(TextBox_ByteOffset.Text);
            oDevice.lSize = int.Parse(TextBox_BitOffset.Text);
            oCommDriver.AddDeviceInfo(oDevice);

            bufWrite[0] = (byte)int.Parse(TextBox_1or0.Text);
            nTotal_len += 1;

            //데이터 전송
            byte[] bWriteBuf = new byte[nTotal_len];
            Array.Copy(bufWrite, 0, bWriteBuf, 0, nTotal_len);

            if(1 == oCommDriver.WriteRandomDevice(bWriteBuf))
            {
                MessageBox.Show("데이터 전송 성공");
            }
            else
            {
                MessageBox.Show("데이터 전송 실패");
            }

            //데이터 읽기
            byte[] bufRead = new byte[nTotal_len];
            if(1==oCommDriver.ReadRandomDevice(bufRead))
            {
                for (int i = 0; i < bufRead.Length; i++)
                {
                    TextBox_Result.AppendText(bufRead[i].ToString("X2"));
                }
                MessageBox.Show("데이터 읽기 성공");
            }
            else
            {
                MessageBox.Show("데이터 읽기 실패");
            }

            if(false == bWriteBuf.SequenceEqual(bufRead))
            {
                TextBox_Result.AppendText("Mismatch");
            }
            else
            {
                TextBox_Result.AppendText("Match");
            }

            //연결 종료
            int nRetn = oCommDriver.Disconnect();
            if(nRetn == 1)
            {
                MessageBox.Show("연결 종료");
            }
            else
            {
                MessageBox.Show("연결 종료 실패");
            }
        }
    }
}
profile
Let's take the lead

0개의 댓글

관련 채용 정보