문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
cuwin:cuwin_modport:index [2016/04/12 03:08] Comfile Technology |
cuwin:cuwin_modport:index [2022/11/02 17:33] (현재) Comfile Technology |
||
---|---|---|---|
줄 1: | 줄 1: | ||
+ | ====== CUWIN과 CW-DIO32 연결, MODPORT연결 ====== | ||
+ | * 8-o 저희 회사에서는 프로그램 개발과정에 관련된 어떠한 기술지원도 제공해드릴 수 없음을 양해바랍니다. 저희 회사에서는 하드웨어와 관련된 부분에 대해서만 기술 지원이 가능합니다. Visaul Studio 에서 코딩하다 발생된 문의는 Microsoft사로 직접 해주시기 바랍니다. | ||
+ | |||
+ | |||
+ | CW-DIO32는 16점 입력, 16점 출력 포트을 제어할 수 있는 DAQ(데이터 수집) 보드입니다. MODBUS-RTU방식으로 되어 있으며, RS485 포트를 가지고 있습니다. MODPORT와 사용방법및 프로토콜이 동일합니다. | ||
+ | |||
+ | {{ :cuwin:cuwin_modport:cwdio321.jpg?nolink |}} | ||
+ | |||
+ | 아래 사진처럼 CUWIN의 뒷부분에 장착해서 사용하실 수 있습니다. | ||
+ | |||
+ | {{ :cuwin:cuwin_modport:cwdio32.gif?nolink |}} | ||
+ | |||
+ | 옵토커플러 DC24V 입력 16점과 NPN TR출력 포트를 가지고 있습니다. 출력포트는 아래 사진처럼 RELAY보드와 연결해서 릴레이를 직접 ON /OFF 할 수도 있습니다. | ||
+ | |||
+ | {{ :cuwin:cuwin_modport:dsc03913.jpg?nolink |}} | ||
+ | |||
+ | CUWIN에서 MODBUS를 이용해서 한번 구동시켜 보았습니다. 입력8개, 출력8개를 제어하는 프로그램입니다. | ||
+ | |||
+ | 아래는 동작하는 동영상입니다. | ||
+ | |||
+ | <html> | ||
+ | <iframe width="544" height="306" src="http://serviceapi.nmv.naver.com/flash/convertIframeTag.nhn?vid=96CEDA0C9E152129F1FB5B6D9C178BE30775&outKey=V12104e0f1bad1a65709710a85c6cda01eae498eefd74d2773dd510a85c6cda01eae4" frameborder="no" scrolling="no"></iframe> | ||
+ | </html> | ||
+ | |||
+ | [[http://www.comfile.co.kr/download/blog/cwdio32.zip|프로그램 소스 다운로드]] | ||
+ | |||
+ | <code csharp> | ||
+ | using System; | ||
+ | using System.Linq; | ||
+ | using System.Collections.Generic; | ||
+ | using System.ComponentModel; | ||
+ | using System.Data; | ||
+ | using System.Drawing; | ||
+ | using System.Text; | ||
+ | using System.Windows.Forms; | ||
+ | using System.Threading; | ||
+ | using Modbus.Device; | ||
+ | using System.IO.Ports; | ||
+ | |||
+ | namespace NModbusTest | ||
+ | { | ||
+ | public partial class Form1 : Form | ||
+ | { | ||
+ | public Form1() | ||
+ | { | ||
+ | InitializeComponent(); | ||
+ | } | ||
+ | |||
+ | ModbusSerialMaster _modbusMaster; | ||
+ | SerialPort _serialPort; | ||
+ | const int SLAVE_ADDRESS = 1; | ||
+ | bool[] bitdata = new bool[16]; | ||
+ | |||
+ | private void Form1_Load(object sender, EventArgs e) | ||
+ | { | ||
+ | _serialPort = new SerialPort("COM1", 57600); | ||
+ | _serialPort.Open(); | ||
+ | |||
+ | _modbusMaster = ModbusSerialMaster.CreateRtu(_serialPort); | ||
+ | _modbusMaster.Transport.ReadTimeout = 500; | ||
+ | _modbusMaster.Transport.WriteTimeout = 500; | ||
+ | _modbusMaster.Transport.Retries = 0; | ||
+ | |||
+ | } | ||
+ | |||
+ | void ReportError(string s) | ||
+ | { | ||
+ | MessageBox.Show(s); | ||
+ | } | ||
+ | |||
+ | |||
+ | private void ReadDigitalInput() | ||
+ | { | ||
+ | try | ||
+ | { | ||
+ | bool[] result = _modbusMaster.ReadCoils(SLAVE_ADDRESS, 2980, 16); | ||
+ | |||
+ | if (result[0]) | ||
+ | { _digitalInLabel1.Text = "ON"; } | ||
+ | else | ||
+ | { _digitalInLabel1.Text = "OFF"; } | ||
+ | |||
+ | if (result[1]) | ||
+ | { _digitalInLabel2.Text = "ON"; } | ||
+ | else | ||
+ | { _digitalInLabel2.Text = "OFF"; } | ||
+ | if (result[2]) | ||
+ | { _digitalInLabel3.Text = "ON"; } | ||
+ | else | ||
+ | { _digitalInLabel3.Text = "OFF"; } | ||
+ | |||
+ | if (result[3]) | ||
+ | { _digitalInLabel4.Text = "ON"; } | ||
+ | else | ||
+ | { _digitalInLabel4.Text = "OFF"; } | ||
+ | |||
+ | if (result[4]) | ||
+ | { _digitalInLabel5.Text = "ON"; } | ||
+ | else | ||
+ | { _digitalInLabel5.Text = "OFF"; } | ||
+ | if (result[5]) | ||
+ | { _digitalInLabel6.Text = "ON"; } | ||
+ | else | ||
+ | { _digitalInLabel6.Text = "OFF"; } | ||
+ | if (result[6]) | ||
+ | { _digitalInLabel7.Text = "ON"; } | ||
+ | else | ||
+ | { _digitalInLabel7.Text = "OFF"; } | ||
+ | if (result[7]) | ||
+ | { _digitalInLabel8.Text = "ON"; } | ||
+ | else | ||
+ | { _digitalInLabel8.Text = "OFF"; } | ||
+ | } | ||
+ | catch (Exception ex) | ||
+ | { | ||
+ | //ReportError(ex.Message); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void WriteDigitalOutput(ushort portn, bool on) | ||
+ | { | ||
+ | try | ||
+ | { | ||
+ | portn += 5980; | ||
+ | _modbusMaster.WriteSingleCoil(SLAVE_ADDRESS, portn , on); | ||
+ | } | ||
+ | catch (Exception ex) | ||
+ | { | ||
+ | //ReportError(ex.Message); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void _offButton8_Click_1(object sender, EventArgs e) | ||
+ | { | ||
+ | WriteDigitalOutput(7, false); | ||
+ | } | ||
+ | |||
+ | private void Form1_Closing(object sender, CancelEventArgs e) | ||
+ | { | ||
+ | _modbusMaster.Dispose(); | ||
+ | _modbusMaster = null; | ||
+ | |||
+ | _serialPort.Close(); | ||
+ | _serialPort.Dispose(); | ||
+ | _serialPort = null; | ||
+ | } | ||
+ | |||
+ | private void timer1_Tick(object sender, EventArgs e) | ||
+ | { | ||
+ | ReadDigitalInput(); | ||
+ | } | ||
+ | |||
+ | private void button1_Click(object sender, EventArgs e) | ||
+ | { | ||
+ | Close(); | ||
+ | } | ||
+ | |||
+ | private void _onButton1_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(0, true); } | ||
+ | |||
+ | private void _offButton1_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(0, false); } | ||
+ | |||
+ | private void _onButton2_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(1, true); } | ||
+ | |||
+ | private void _offButton2_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(1, false); } | ||
+ | |||
+ | private void _onButton3_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(2, true); } | ||
+ | |||
+ | private void _offButton3_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(2, false); } | ||
+ | |||
+ | private void _onButton4_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(3, true); } | ||
+ | |||
+ | private void _offButton4_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(3, false); } | ||
+ | |||
+ | private void _onButton5_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(4, true); } | ||
+ | |||
+ | private void _offButton5_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(4, false); } | ||
+ | |||
+ | private void _onButton16_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(5, true); } | ||
+ | |||
+ | private void _offButton6_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(5, false); } | ||
+ | |||
+ | private void _onButton7_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(6, true); } | ||
+ | |||
+ | private void _offButton7_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(6, false); } | ||
+ | |||
+ | private void _onButton8_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(7, true); } | ||
+ | |||
+ | private void _offButton8_Click(object sender, EventArgs e) | ||
+ | { WriteDigitalOutput(7, false); } | ||
+ | |||
+ | private void button2_Click(object sender, EventArgs e) | ||
+ | { | ||
+ | try | ||
+ | { | ||
+ | for (int i = 0; i < 16; i++) | ||
+ | { | ||
+ | bitdata[i] = true; | ||
+ | } | ||
+ | _modbusMaster.WriteMultipleCoils(SLAVE_ADDRESS, 5980, bitdata); | ||
+ | } | ||
+ | catch (Exception ex) | ||
+ | { | ||
+ | //ReportError(ex.Message); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void button3_Click(object sender, EventArgs e) | ||
+ | { | ||
+ | try | ||
+ | { | ||
+ | for (int i = 0; i < 16; i++) | ||
+ | { | ||
+ | bitdata[i] = false; | ||
+ | } | ||
+ | _modbusMaster.WriteMultipleCoils(SLAVE_ADDRESS, 5980, bitdata); | ||
+ | } | ||
+ | catch (Exception ex) | ||
+ | { | ||
+ | //ReportError(ex.Message); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | * 위 소스 코드는 예시일 뿐입니다. 이 소스를 참조해서 여러분의 소스를 완성하시라고 예시를 제공해드리고 있습니다. 따라서 당사에서는 이 소스의 동작여부에 대한 어떠한 보증도 하지 않습니다. 추후 동작여부에 따른 손해배상 책임이 없음을 명시하는 바입니다. | ||
+ | |||
+ | [[cuwin:index|한단계 뒤로]] |