public void ThreadUpdateInput() { ModBusDevice device = null; while (true) { try { for (int nIndex = 0; nIndex < _devices.Count; nIndex++) { device = _devices[nIndex]; if (device._bMonitor) { ScanInputStatus(device); ScanInputRegister(device); } Thread.Sleep(10); } } catch (Exception ex) { ModBusDevice.WriteLog(string.Format("Scan input from device {0} occurred exception:{1}", (device == null) ? "" : device.Name, ex.Message)); } } }
private void menuItemComm_Click(object sender, EventArgs e) { if (this.ActiveMdiChild != null) { frmDevice frmDev = (frmDevice)(this.ActiveMdiChild); ModBusDevice device = frmDev._device; frmDeviceSetting commSetting = new frmDeviceSetting(device); commSetting.ShowDialog(); if (commSetting._bOk) { if (commSetting.PortIsChanged()) { bool bCurMonitorStatus = device._bMonitor; bool bPortIsOpen = (device._port != null) ? device._port.IsOpen : false; device._bMonitor = false; device.ClosePort(); commSetting.GetDeviceInfo(ref device); if (bPortIsOpen) { device.OpenPort(); } device._bMonitor = bCurMonitorStatus; } device._bModified = true; } } }
private ModBusDevice GetDeviceFromFile() { Stream myStream; OpenFileDialog openFileDlg = new OpenFileDialog(); openFileDlg.Filter = "xml files (*.xml)|*.xml"; openFileDlg.FilterIndex = 1; openFileDlg.RestoreDirectory = true; if (openFileDlg.ShowDialog() == DialogResult.OK) { try { if ((myStream = openFileDlg.OpenFile()) != null) { using (myStream) { // Insert code to read the stream here. XmlSerializer xs = new XmlSerializer(typeof(ModBusDevice)); ModBusDevice device = xs.Deserialize(myStream) as ModBusDevice; return(device); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "打开文件错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } return(null); }
private void ShowDeviceForm(ModBusDevice device) { frmDevice frmDev = new frmDevice(device); frmDev.MdiParent = this; frmDev.SetBarText = InvokeSetBarText; frmDev.Show(); }
private void UpdateInputStatus(int nAddr, byte[] data, ModBusDevice device) { frmDevice chFrmDev = GetFormByDeviceName(device.Name); if (chFrmDev._device != null) { chFrmDev.InvokeUpdateInputStatus(nAddr, data); } }
public frmDeviceSetting(ModBusDevice device) { InitializeComponent(); _device = device; InitControls(); this.Text = "通信设置"; tbDeviceName.Text = _device.Name; tbAddress.Text = _device.Address.ToString(); }
public void ThreadSetOutput(object list) { object[] param = (object[])list; string strAddr = param[0].ToString(); int nAddr = Convert.ToInt32(strAddr); ModBusDevice device = (ModBusDevice)param[1]; string strOutput = (string)param[2]; bool bRet = device.SetOutputStatusToDevice(nAddr); SetBarText("设置\"" + strOutput + (bRet ? "成功\"" : "失败\"")); }
private void menuItemOpen_Click(object sender, EventArgs e) { ModBusDevice device = GetDeviceFromFile(); if (device != null) { _devices.Add(device); device.ResetCurrentStatus(); ShowDeviceForm(device); } }
public frmDevice(ModBusDevice device) { InitializeComponent(); MessageFilter msgFilter = new MessageFilter(); msgFilter.MouseClick = new LButtonClick(msgMouseClick); Application.AddMessageFilter(msgFilter); _device = device; this.Text = "ModBus Device - " + _device.Name; InitListView(); UpdateDeviceToPage(); }
// 扫描输入寄存器 void ScanInputRegister(ModBusDevice device) { foreach (RegisterItem reg in device._dicInputRegs.Values) { byte[] buf = null; ModBusDevice.WriteLog(String.Format("Read intput register from device {0} start at address 0x{1:4X}.", device.Name, reg._nStartAddr)); if (device.ReadInputReg(reg._nStartAddr, ref buf)) { byte[] bufData = new byte[reg._nLength]; Array.Copy(buf, 3, bufData, 0, buf[2]); string strVal = reg.BufDataToValueString(bufData); if (strVal != reg._strData) { GetFormByDeviceName(device.Name).InvokeUpdateInputRegValueToList(reg._nStartAddr, strVal); } } } }
/// <summary> /// 获取设置的设备信息 /// </summary> /// <param name="device"></param> public void GetDeviceInfo(ref ModBusDevice device) { device.Name = tbDeviceName.Text; device.Address = Convert.ToByte(tbAddress.Text); device.SetPort(string.Format("PortName:{0};BaudRate:{1};DataBits:{2};StopBits:{3};Parity:{4}", comboBoxPort.Text, comboBoxBaud.Text, comboBoxDataBits.Text, comboBoxStopBits.Text, comboBoxCheck.Text)); // 读功能码 device.FuncReadCoilStatus = (byte)numUpDownReadCoil.Value; device.FuncReadSwitch = (byte)numUpDownReadSwitch.Value; device.FuncReadHoldReg = (byte)numUpDownReadHoldReg.Value; device.FuncReadInputReg = (byte)numUpDownReadInputReg.Value; // 写功能码 device.FuncReadCoilStatus = (byte)numUpDownReadCoil.Value; device.FuncReadSwitch = (byte)numUpDownReadSwitch.Value; device.FuncReadHoldReg = (byte)numUpDownReadHoldReg.Value; device.FuncReadInputReg = (byte)numUpDownReadInputReg.Value; }
private void menuItemNew_Click(object sender, EventArgs e) { //if (this.ActiveMdiChild != null && !SaveCurrentDevice()) //{ // return; //} frmDeviceSetting frmDevice = new frmDeviceSetting(); frmDevice._refDevices = _devices; frmDevice.ShowDialog(); if (frmDevice._bOk) { ModBusDevice device = new ModBusDevice(); device._bCreatNew = true; frmDevice.GetDeviceInfo(ref device); _devices.Add(device); ShowDeviceForm(device); } }
private void SaveDevice(ModBusDevice device) { Stream myStream; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "xml files (*.xml)|*.xml"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = saveFileDialog1.OpenFile()) != null) { // Code to write the stream goes here. XmlSerializer xs = new XmlSerializer(typeof(ModBusDevice)); xs.Serialize(myStream, device); device._bModified = false; device._bCreatNew = false; device._strFilePath = saveFileDialog1.FileName; myStream.Close(); } } }
// 扫描输入状态 void ScanInputStatus(ModBusDevice device) { int nAddr = 0; foreach (StatusRegister status in device._dicInputStatus.Values) { if (status._nStartAddr < nAddr) { continue; } byte[] buf = null; nAddr = status._nStartAddr; ModBusDevice.WriteLog(String.Format("Read intput status from device {0} start at address 0x{1:4X}.", device.Name, nAddr)); if (device.ReadInputStatus(ref nAddr, ref buf, true)) { //判断输入状态是否有变化,有变化则进行更新 bool bMultiBits = (status._lsHiName.Count == 16) ? true : false; int nAddr1 = status._nStartAddr; int nCount = 0; while (device._dicInputStatus.ContainsKey(nAddr1) && device._dicInputStatus[nAddr1]._nStartAddr < nAddr) { //读取返回数据4个字节开始为寄存器数据 //单寄存器16位状态数据,取2个字节长度 bool bStatus = false; if (bMultiBits) { for (int nBitIndex = 0; nBitIndex < 16; nBitIndex++) { //判断状态是否变化,变化则更新界面显示 bStatus = ((buf[nCount * 2 + 3 + nBitIndex / 8] & (1 << (nBitIndex % 8))) != 0); if (bStatus != device._dicInputStatus[nAddr1]._lsCurStatus[nBitIndex]) { byte[] data = new byte[16]; Array.Copy(buf, data, 16); UpdateInputStatus(nAddr1, data, device); break; } } } else// 1bit 对应一个地址寄存器的状态 { for (int nBitIndex = 0; nBitIndex < 8; nBitIndex++) { //判断状态是否变化,变化则更新界面显示 bStatus = ((buf[nCount + 3] & (1 << nBitIndex)) != 0); if (bStatus != device._dicInputStatus[nAddr1]._lsCurStatus[nBitIndex]) { byte[] data = new byte[8]; Array.Copy(buf, data, 8); UpdateInputStatus(nAddr1, data, device); break; } } } nCount++; nAddr1 += bMultiBits ? 1 : 8; } } } }