using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; //for Sleep function using PISOCANDNM_DotNET; namespace CSharp_Demo1 { public partial class Form1 : Form { UInt32 Ret = 0; String Str; static Byte ActiveBoardNo = 0xFF; static Byte ActivePortNo = 0xFF; UInt16 TotalDevices = 0; UInt16[] DeviceInputLen = new UInt16[100]; UInt16[] DeviceOutputLen = new UInt16[100]; Byte[] DesMACID = new Byte[100]; Byte[] Type = new Byte[100]; public Form1() { InitializeComponent(); Btn_TotalModules_Click(null, null); Btn_DLLVer_Click(null, null); ComBox_SetBaudRate.SelectedIndex = 0; ComBox_SetMasterID.SelectedIndex = 0; ComBox_DebugDevice.SelectedIndex = 0; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_TotalModules_Click(object sender, EventArgs e) { //Scan total PISO-CAN board-7565-DNM modules in you PC Byte TotalCount = 0; Byte[] BoardIDList = new Byte[15]; Byte[] PortNumList = new Byte[15]; int i; uint dwVID=0, dwDID=0, dwSVID=0, dwSDID=0, dwSAuxID=0, dwIrqNo=0, MAX_SDID=0; String BoardName=""; ComboBox_BoardNo.Items.Clear(); ComboBox_PortNo.Items.Clear(); DeviceNet.CANDNM_TotalPISOCANDNMBoard(ref TotalCount, BoardIDList, PortNumList); TBox_TotalModules.Text = String.Format(" {0} modules in PC", TotalCount); if (TotalCount == 0) return; for (i = 0; i < TotalCount; i++) { DeviceNet.CANDNM_GetBoardInf((Byte)i, ref dwVID, ref dwDID, ref dwSVID, ref dwSDID, ref dwSAuxID, ref dwIrqNo); switch (dwDID) { case 0x9030: if (dwSDID == 0xC210) BoardName = "PISO-CAN200U"; if (dwSDID == 0xC250) BoardName = "PCM-CAN200"; if (dwSDID == 0xC410) BoardName = "PISO-CAN400U"; if (dwSDID == 0xC450) BoardName = "PCM-CAN400"; break; case 0x9050: if (dwSDID == 0xC200) BoardName = "PISO-CAN200"; if (dwSDID == 0xC280) BoardName = "PEX-CAN200i"; if (dwSDID == 0xC400) BoardName = "PISO-CAN400"; if (dwSDID == 0xC480) BoardName = "PEX-CAN400i"; break; } if (MAX_SDID < dwSDID) MAX_SDID = dwSDID; ComboBox_BoardNo.Items.Add(String.Format("{0} : {1}", BoardIDList[i], BoardName)); } ComboBox_PortNo.Items.Add("Port : 0"); ComboBox_PortNo.Items.Add("Port : 1"); if (MAX_SDID > 0xC280) { ComboBox_PortNo.Items.Add("Port : 2"); ComboBox_PortNo.Items.Add("Port : 3"); } ComboBox_BoardNo.SelectedIndex = 0; ComboBox_PortNo.SelectedIndex = 0; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_DLLVer_Click(object sender, EventArgs e) { // Get PISOCANDNM.DLL version Ret = DeviceNet.CANDNM_GetDLLVersion(); TBox_DLLVer.Text = String.Format("DLL Ver: {0:X}.{1:X}", ((Ret >> 16) & 0x1), (Ret & 0xEFF)); } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_GetBaudRate_Click(object sender, EventArgs e) { // Get Baud Rate of the CAN bus // Users' program could call this function to know what the CAN bus baud rate of the PISOCANDNM. if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } Ret = DeviceNet.CANDNM_GetBaudRate(ActiveBoardNo, ActivePortNo); switch (Ret) { case 0: Str = "Rate: 125K bps(0)"; break; case 1: Str = "Rate: 250K bps(1)"; break; case 2: Str = "Rate: 500K bps(2)"; break; default: Str = String.Format("Error({0})", Ret); break; } TBox_GetBaudRate.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_ActiveModule_Click(object sender, EventArgs e) { // ActiveBoard // This is required function call for everytime using this module. // Users' program should add this function call in the starting of the all operations. if ((ComboBox_PortNo.SelectedIndex < 0) || (ComboBox_BoardNo.SelectedIndex < 0)) { TBox_ActiveModule.Text = String.Format("Please Select a BoardNo or PortNo."); return; } try { ActiveBoardNo = Byte.Parse(ComboBox_BoardNo.SelectedIndex.ToString()); ActivePortNo = Byte.Parse(ComboBox_PortNo.SelectedIndex.ToString()); } catch(FormatException ex) { MessageBox.Show(ex.Message); return; } Ret = DeviceNet.CANDNM_Active(ActiveBoardNo, ActivePortNo); if (Ret == DeviceNet.DNM_NoError) Str = "Return : OK!!"; else Str = String.Format("Return : Error! {0}", Ret); TBox_ActiveModule.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_GetMasterID_Click(object sender, EventArgs e) { // Get Master MAC ID // Users' program could call this function to get what the DeviceNet Master ID of the PISOCANDNM. if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } Ret = DeviceNet.CANDNM_GetMasterMACID(ActiveBoardNo, ActivePortNo); if (Ret > 63) Str = String.Format("Error({0})", Ret); else Str = String.Format("ID: {0}", Ret); TBox_GetMasterID.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_GetMasterStatus_Click(object sender, EventArgs e) { // Get Master Status // Users' program could call this function continually to get what the DeviceNet Master status of the PISOCANDNM. if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } Ret = DeviceNet.CANDNM_GetMasterStatus(ActiveBoardNo, ActivePortNo); Str = String.Format("Status: 0x{0}", Ret); TBox_GetMasterStatus.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Form1_FormClosed(object sender, FormClosedEventArgs e) { //Close the driver of the PISOCANDNM. DeviceNet.CANDNM_Close(ActiveBoardNo, ActivePortNo); } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_SetBaudRate_Click(object sender, EventArgs e) { // Set Baud Rate // Users' program could call this function to change the CAN bus baud rate of the PISOCANDNM. // The PISOCANDNM will save the information in the EEPROM. // If the baud rate have been set, users need not call this function again. // If the baud rate have been changed, the PISOCANDNM should be reseted to make the change enable. if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } Byte NewBaudRate = (Byte)ComBox_SetBaudRate.SelectedIndex; Ret = DeviceNet.CANDNM_SetBaudRate(ActiveBoardNo, ActivePortNo, NewBaudRate); if (Ret == DeviceNet.DNM_NoError) Str = "Return : OK!!"; else Str = String.Format("Return : Error! {0}", Ret); TBox_SetBaudRate.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_SetMasterID_Click(object sender, EventArgs e) { // Set Master MAC ID // Users' program could call this function to change the DeviceNet Master ID of the I-7565-DNM. // The I-7565-DNM will save the ID information in the EEPROM. // If the Master ID have been set, users need not call this function again. // If the Master ID have been changed, the I-7565-DNM should be reseted to make the change enable. if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } Byte NewMasterID = (Byte)ComBox_SetMasterID.SelectedIndex; Ret = DeviceNet.CANDNM_SetMasterMACID(ActiveBoardNo, ActivePortNo, NewMasterID); if (Ret == DeviceNet.DNM_NoError) Str = "Return : OK!!"; else Str = String.Format("Return : Error! {0}", Ret); TBox_SetMasterID.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_DebugDevice_Click(object sender, EventArgs e) { // Debug Device // Users can utilize the procedure below to check if slave devices are exist or not Byte[] DesMACIDList = new Byte[1]; Byte TimeIndex = 0; if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } DesMACIDList[0] = (Byte)ComBox_DebugDevice.SelectedIndex; //Make PISOCANDNM to search the Slave Device Ret = DeviceNet.CANDNM_SearchSpecificDevice(ActiveBoardNo, ActivePortNo, 1, DesMACIDList); if (Ret != DeviceNet.DNM_NoError) { Str = String.Format("Search Return : Error! {0}", Ret); TBox_DebugDevice.Text = Str; return; } //Wait for searching TimeIndex = 0; while (DeviceNet.CANDNM_IsSearchOK(ActiveBoardNo,ActivePortNo) != 0) { if (TimeIndex++ > 40) { Str = String.Format("Return : Device Not Found!"); TBox_DebugDevice.Text = Str; return; } Thread.Sleep(300); } //Get searched result Ret = DeviceNet.CANDNM_GetSearchedDevices(ActiveBoardNo, ActivePortNo, ref TotalDevices, DesMACID, Type, DeviceInputLen, DeviceOutputLen); if(TotalDevices > 0) Str = "Return : Device OK!!"; else Str = String.Format("Return : Error! {0}",Ret); TBox_DebugDevice.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_AddDevice_Click(object sender, EventArgs e) { // Add Device if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } Byte Slave_MACID = (Byte)ComBox_DebugDevice.SelectedIndex; Ret = DeviceNet.CANDNM_AddDevice(ActiveBoardNo, ActivePortNo, Slave_MACID, 1000); if (Ret == DeviceNet.DNM_NoError) Str = "Return : OK!!"; else Str = String.Format("Return : Error! {0}", Ret); TBox_AddDevice.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_GetPollInfo_Click(object sender, EventArgs e) { // GET POLL information from slave device UInt16 i,InputLen,OutputLen; if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } InputLen = OutputLen = 0; for(i = 0; i < TotalDevices;i++) { if(Type[i] == (Byte)DeviceNet.ConType.ConType_Poll) { InputLen = DeviceInputLen[i]; OutputLen = DeviceOutputLen[i]; } } if((InputLen != 0) || (OutputLen != 0)) Str = "Return : OK!!"; else Str = "Return : No POLL Information!"; TBox_GetPollInfo.Text = Str; TBox_PollInput.Text = InputLen.ToString(); TBox_PollOutput.Text = OutputLen.ToString(); } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_ConfigPoll_Click(object sender, EventArgs e) { // configure the POLL I/O connection if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } if (TBox_GetPollInfo.Text != "Return : OK!!") { TBox_ConfigPoll.Text = "Step 7 error!!"; return; } Byte SlaveID = (Byte)ComBox_DebugDevice.SelectedIndex; UInt16 InputLen = UInt16.Parse(TBox_PollInput.Text); UInt16 OutputLen = UInt16.Parse(TBox_PollOutput.Text); Ret = DeviceNet.CANDNM_AddIOConnection(ActiveBoardNo, ActivePortNo, SlaveID, (Byte)DeviceNet.ConType.ConType_Poll, InputLen, OutputLen, 200); if (Ret == DeviceNet.DNM_NoError) Str = "Return : OK!!"; else Str = String.Format("Return : Error! {0}", Ret); TBox_ConfigPoll.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_StartDevice_Click(object sender, EventArgs e) { // Start Device if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } Byte SlaveID = (Byte)ComBox_DebugDevice.SelectedIndex; Ret = DeviceNet.CANDNM_StartDevice(ActiveBoardNo, ActivePortNo, SlaveID); if (Ret == DeviceNet.DNM_NoError) Str = "Return : OK!!"; else Str = String.Format("Return : Error! {0}", Ret); TBox_StartDevice.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_ReadPollData_Click(object sender, EventArgs e) { //Read POLL input I/O data UInt16 IOLen = 0,i; Byte[] IODATA = new Byte[512]; if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } Byte SlaveID = (Byte)ComBox_DebugDevice.SelectedIndex; Ret = DeviceNet.CANDNM_ReadInputData(ActiveBoardNo, ActivePortNo, SlaveID, (Byte)DeviceNet.ConType.ConType_Poll, ref IOLen, IODATA); if(Ret == DeviceNet.DNM_NoError) { Str = ""; for(i = 0;i < IOLen;i++) Str += String.Format("0x{0:X}, ",IODATA[i]); } else Str = String.Format("Return : Error! {0}",Ret); TBox_ReadPollData.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// private void Btn_ClearAllConfig_Click(object sender, EventArgs e) { // Clear all configuration information if ((ActiveBoardNo == 0xFF) || (ActivePortNo == 0xFF)) { MessageBox.Show("Please ActiveBoard first"); return; } //Ret = I7565DNM.I7565DNM_ClearAllConfig(ActiveBoardNo); if (Ret == 0)//I7565DNM.DNMXS_NoError) Str = "Return : OK!!"; else Str = String.Format("Return : Error! {0}", Ret); TBox_ClearAllConfig.Text = Str; } ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// } }