diff --git a/CleanMachineHMI/App.config b/CleanMachineHMI/App.config index a8e5596..fa12a1f 100644 --- a/CleanMachineHMI/App.config +++ b/CleanMachineHMI/App.config @@ -30,6 +30,8 @@ + + diff --git a/CleanMachineHMI/Utils/KeyboardAutoPopUp.cs b/CleanMachineHMI/Utils/KeyboardAutoPopUp.cs index a5aa45c..adb5647 100644 --- a/CleanMachineHMI/Utils/KeyboardAutoPopUp.cs +++ b/CleanMachineHMI/Utils/KeyboardAutoPopUp.cs @@ -30,7 +30,7 @@ namespace CleanMachineHMI static KeyboardAutoPopUp() { - hasKeyboard = GetKeyboardPresent(); + // hasKeyboard = GetKeyboardPresent(); diff --git a/CleanMachineHMI/Utils/PlcOperationHelper.cs b/CleanMachineHMI/Utils/PlcOperationHelper.cs index b3f19c2..82d4b35 100644 --- a/CleanMachineHMI/Utils/PlcOperationHelper.cs +++ b/CleanMachineHMI/Utils/PlcOperationHelper.cs @@ -7,7 +7,7 @@ //启用心跳检查 #if RELEASE - #define UseHeartbeat +#define UseHeartbeat #endif using CleanMachine.Model; using LeadDataBaseUtility; @@ -90,45 +90,58 @@ namespace CleanMachineHMI public bool PlcInit() { -#if RELEASE + try { -#endif - string portname = GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'PortName'")[0]["PARAMETER_VALUE"].ToString(); - int baudrate = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'BaudRate'")[0]["PARAMETER_VALUE"]); - int databits = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'DataBits'")[0]["PARAMETER_VALUE"]); - StopBits stopbis = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'StopBits'")[0]["PARAMETER_VALUE"]) == 0 ? StopBits.None : (Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'StopBits'")[0]["PARAMETER_VALUE"]) == 1 ? StopBits.One : StopBits.Two); - Parity paritymsg = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'ParityMsg'")[0]["PARAMETER_VALUE"]) == 0 ? Parity.None : (Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'ParityMsg'")[0]["PARAMETER_VALUE"]) == 1 ? Parity.Odd : Parity.Even); - int modbusPattern = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'ModbusPattern'")[0]["PARAMETER_VALUE"]); + + string portname = ConfigurationManager.AppSettings["PortName"]; + int baudrate = int.Parse(ConfigurationManager.AppSettings["BaudRate"]); + int databits = int.Parse(ConfigurationManager.AppSettings["DataBits"]); + StopBits stopbis = (StopBits)int.Parse(ConfigurationManager.AppSettings["StopBits"]); + Parity paritymsg = (Parity)int.Parse(ConfigurationManager.AppSettings["ParityMsg"]); + int modbusPattern = int.Parse(ConfigurationManager.AppSettings["ModbusPattern"]); + + //string portname = GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'PortName'")[0]["PARAMETER_VALUE"].ToString(); + //int baudrate = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'BaudRate'")[0]["PARAMETER_VALUE"]); + //int databits = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'DataBits'")[0]["PARAMETER_VALUE"]); + //StopBits stopbis = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'StopBits'")[0]["PARAMETER_VALUE"]) == 0 ? StopBits.None : (Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'StopBits'")[0]["PARAMETER_VALUE"]) == 1 ? StopBits.One : StopBits.Two); + //Parity paritymsg = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'ParityMsg'")[0]["PARAMETER_VALUE"]) == 0 ? Parity.None : (Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'ParityMsg'")[0]["PARAMETER_VALUE"]) == 1 ? Parity.Odd : Parity.Even); + //int modbusPattern = Convert.ToInt32(GlobalHelper.ParameterList.Select(" PARAMETER_CODE = 'ModbusPattern'")[0]["PARAMETER_VALUE"]); #if USE_LOCAL_TCP_DEBUG port = new System.Net.Sockets.TcpClient(); var adaptet = new NModbus.IO.TcpClientAdapter(port); #else - port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis); - var adaptet = new NModbus.Serial.SerialPortAdapter(port); + port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis); + var adaptet = new NModbus.Serial.SerialPortAdapter(port); + adaptet.ReadTimeout = 200; #endif - var factory = new ModbusFactory(); - if (modbusPattern == 0) - { + var factory = new ModbusFactory(); + if (modbusPattern == 0) + { + + + master = factory.CreateRtuMaster(adaptet); + } + else + { + master = factory.CreateAsciiMaster(adaptet); + } - master = factory.CreateRtuMaster(adaptet); - } - else - { - master = factory.CreateAsciiMaster(adaptet); - } #if USE_LOCAL_TCP_DEBUG port.Connect("10.0.0.95", 5002); this._isConn = true; + return true; #else - port.Open(); + port.Open(); + return port.IsOpen; + #endif - return true; -#if RELEASE + + } catch (Exception ex) { @@ -136,7 +149,7 @@ namespace CleanMachineHMI isConn = false; return false; } -#endif + } public void ClosePlcConn() { @@ -170,7 +183,7 @@ namespace CleanMachineHMI if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress)) { //string PlcItemId = item.PlcItemId.ToString(); - // string plcdatatype = item.PlcItemDataType.ToString(); + // string plcdatatype = item.PlcItemDataType.ToString(); int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress); item.PlcItemValue = readBytArr_M[plcItemDeviationAddress].ToString(); GlobalHelper.ShowSensorStatus(item, new EventArgs()); @@ -216,7 +229,7 @@ namespace CleanMachineHMI { string PlcItemId = item.PlcItemId.ToString(); - // string plcdatatype = item.PlcItemDataType.ToString(); + // string plcdatatype = item.PlcItemDataType.ToString(); int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress); if (Convert.ToInt32(item.PlcItemPriAddress) < 409) { diff --git a/CleanMachineHMI/frmMainNew.cs b/CleanMachineHMI/frmMainNew.cs index fa87713..b008fc3 100644 --- a/CleanMachineHMI/frmMainNew.cs +++ b/CleanMachineHMI/frmMainNew.cs @@ -192,36 +192,36 @@ namespace CleanMachineHMI isPlcConnStatus = true; GlobalHelper.SysThIsRunFlag = true; - Thread th = new Thread(RefreshPlcItem) - { - IsBackground = true - }; - th.Start(); - - - //Thread th = new Thread(RefreshPlcItem_M) + //Thread th = new Thread(RefreshPlcItem) //{ // IsBackground = true //}; //th.Start(); - //Thread th1 = new Thread(RefreshPlcItem_D) - //{ - // IsBackground = true - //}; - //th1.Start(); - //Thread th2 = new Thread(RefreshPlcItem_X) - //{ - // IsBackground = true - //}; - //th2.Start(); + Thread th = new Thread(RefreshPlcItem_M) + { + IsBackground = true + }; + th.Start(); - //Thread th3 = new Thread(RefreshPlcItem_Y) - //{ - // IsBackground = true - //}; - //th3.Start(); + Thread th1 = new Thread(RefreshPlcItem_D) + { + IsBackground = true + }; + th1.Start(); + + Thread th2 = new Thread(RefreshPlcItem_X) + { + IsBackground = true + }; + th2.Start(); + + Thread th3 = new Thread(RefreshPlcItem_Y) + { + IsBackground = true + }; + th3.Start(); Thread th4 = new Thread(MainThRunFunc) {