private void singleDeviceModbusRequest() { ushort[] rxBuf = new ushort[250]; if (utility.validateIPAddress(txtSlaveIP.Text) == false) { return; } if (txtNReg.Text == " ") { Console.WriteLine("Number of register must be in range of 1 to 120"); return; } if (txtStartAddress.Text == " ") { Console.WriteLine("Number of register must be in range of 1 to 120"); return; } modbusSlaveInfo slaveInfo = new modbusSlaveInfo(); slaveInfo.slaveIPAdd = txtSlaveIP.Text.ToString(); slaveInfo.tcpPortId = 502; slaveInfo.slaveUid = 1; mrAccess.setSlaveInfo(slaveInfo); try { btSendModbus.IsEnabled = false; if (rdFromFile.IsChecked == false) { ushort startAdd = Convert.ToUInt16(txtStartAddress.Text); ushort nReg = Convert.ToUInt16(txtNReg.Text); handleModbusReadRequest(startAdd, nReg, rxBuf); } else { if (nRequestInArray >= 0) { for (int i = 0; i < nRequestInArray - 1; i++) { handleModbusReadRequest(readReqArray[i].regAdd, readReqArray[i].nReg, rxBuf); } } else { Console.WriteLine("Fail to read Modbus request from Excel file"); } } btSendModbus.IsEnabled = true; } catch (excepetion ex) { Console.WriteLine(ex.Message); } }
private void btSendModbus_Click(object sender, RoutedEventArgs e) { ushort[] rxBuf = new ushort[250]; if (utility.validateIPAddress(txtSlaveIP.Text) == false) { return; } if (txtNReg.Text == " ") { Console.WriteLine("Number of register must be in range of 1 to 120"); return; } if (txtStartAddress.Text == " ") { Console.WriteLine("Number of register must be in range of 1 to 120"); return; } modbusSlaveInfo slaveInfo = new modbusSlaveInfo(); slaveInfo.slaveIPAdd = txtSlaveIP.Text.ToString(); slaveInfo.tcpPortId = 502; slaveInfo.slaveUid = 1; mrAccess.setSlaveInfo(slaveInfo); try { if (chReqInput.IsChecked == false) { ushort startAdd = Convert.ToUInt16(txtStartAddress.Text); ushort nReg = Convert.ToUInt16(txtNReg.Text); handleModbusReadRequest(startAdd, nReg, rxBuf); } else { int nRequest = readMBReqFromExcel(); if (nRequest >= 0) { for (int i = 0; i < nRequest - 1; i++) { handleModbusReadRequest(readReqArray[i].regAdd, readReqArray[i].nReg, rxBuf); } } else { Console.WriteLine("Fail to read Modbus request from Excel file"); } } } catch (excepetion ex) { Console.WriteLine(ex.Message); } }
/*********************************END******************************************************************************/ /*********************************END******************************************************************************/ private void btScan_Click(object sender, RoutedEventArgs e) { if (txtIPRange.Text == " ") { MessageBox.Show(" Range value should be 0 to 255"); } if (txtIPStart.Text == " ") { MessageBox.Show(" Start Address can't be empty "); } try { UInt16 range = Convert.ToUInt16(txtIPRange.Text); if (utility.validateIPAddress(txtIPStart.Text) == false) { MessageBox.Show(" No a valid IP address "); } else { lbIConStatus.Content = ""; pbScanning.Value = 0; int lstCnt = grdDeviceList.Items.Count; for (int i = 0; i < lstCnt; i++) { grdDeviceList.Items.RemoveAt(0); } btScan.IsEnabled = false; //MessageBox.Show("start Scan"); tcpNIpInfoStruct portInfo = new tcpNIpInfoStruct(); portInfo.clientIP = clientipStr; portInfo.clientPort = 0; // any available port portInfo.serverIP = txtIPStart.Text.ToString(); portInfo.serverPort = 502; // always modbus port portInfo.nDevice = range; networkScan.startScanNetwork(portInfo); lbIConStatus.Content = "Scanning ...."; } } catch { MessageBox.Show(" Only interger allowed "); } }