public void StartModbusCommunication() { InStartupMode = true; if (_modbus == null) { _modbus = new WSMBSControl(); } if (ModBusIsOn) { StopTakingData(); return; } String[] comPortsToCheck = { "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10" }; foreach (string portName in comPortsToCheck) { _modbus.Mode = Mode.RTU; _modbus.PortName = portName; _modbus.BaudRate = 19200; _modbus.StopBits = 1; _modbus.Parity = Parity.None; _modbus.ResponseTimeout = 950; _modbus.LicenseKey("0AA1-91A6-7BE9-08B7-1A9A-B68B"); Node1ModResult = _modbus.Open(); if (Node1ModResult != Result.SUCCESS) { ModbusError = string.Format("Connecting to radio network {0} - {1}", portName, _modbus.GetLastErrorString()); ButtonText = "Error"; InStartupMode = false; ModBusIsOn = false; } else { //clear the modbus error incase there was one ModbusError = "Radio Network Connected..."; ModBusIsOn = true; InStartupMode = true; _bw.RunWorkerAsync(); return; } } }
/// <summary> /// 端口连接函数 /// </summary> /// <param name="sName"></param> /// <param name="nPort"></param> /// <returns></returns> public bool MB_Connect(string sName, int nPort) { if (m_nProcotol == 2) { tcp_svr = new WSMBTControl(); tcp_svr.LicenseKey("2222222222222222222222222AAF2"); return(WSMBT.Result.SUCCESS == tcp_svr.Connect(sName, nPort)); } ser_svr = new WSMBSControl(); ser_svr.LicenseKey("2222222222222222222222222F3AA"); ser_svr.PortName = sName; ser_svr.Parity = Parity.None; ser_svr.DataBits = 8; ser_svr.StopBits = 1; ser_svr.BaudRate = nPort; bTaskStatus = false; return(WSMBS.Result.SUCCESS == ser_svr.Open()); }