修改项目结构

This commit is contained in:
gongxh
2021-12-26 21:30:02 +08:00
parent b06189cb49
commit d39ac43a08
106 changed files with 3392 additions and 3083 deletions

View File

@@ -60,7 +60,7 @@ namespace CleanMachineHMI
set { this._isConn = value; }
}
private static IModbusMaster master;
private static SerialPort port;
private static System.Net.Sockets.TcpClient port;
public bool PlcInit()
{
@@ -72,7 +72,8 @@ namespace CleanMachineHMI
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"]);
port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis);
// port = new SerialPort(portname, baudrate, paritymsg, databits, stopbis);
port = new System.Net.Sockets.TcpClient();
if (modbusPattern == 0)
{
master = ModbusSerialMaster.CreateRtu(port);
@@ -81,7 +82,9 @@ namespace CleanMachineHMI
{
master = ModbusSerialMaster.CreateAscii(port);
}
port.Open();
port.Connect("127.0.0.1", 5002);
// port.Open();
this._isConn = true;
return true;
}
catch (Exception ex)
@@ -93,7 +96,7 @@ namespace CleanMachineHMI
}
public void ClosePlcConn()
{
if (port.IsOpen)
if (port.Connected)
{
port.Close();
master = null;
@@ -702,7 +705,7 @@ namespace CleanMachineHMI
{
return false;
}
master.WriteSingleCoilAsync(1, Convert.ToUInt16(item_M.PlcItemModbusAddress), bool.Parse(writeValue));
master.WriteSingleCoil(1, Convert.ToUInt16(item_M.PlcItemModbusAddress), bool.Parse(writeValue));
return true;
case PlcItemTypeDesc.Y:
BasPlcItemMd item_Y = GlobalHelper.PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode);
@@ -710,7 +713,7 @@ namespace CleanMachineHMI
{
return false;
}
master.WriteSingleCoilAsync(1, Convert.ToUInt16(item_Y.PlcItemModbusAddress), bool.Parse(writeValue));
master.WriteSingleCoil(1, Convert.ToUInt16(item_Y.PlcItemModbusAddress), bool.Parse(writeValue));
return true;
case PlcItemTypeDesc.D:
BasPlcItemMd item_D = GlobalHelper.PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode);
@@ -718,7 +721,7 @@ namespace CleanMachineHMI
{
return false;
}
master.WriteSingleRegisterAsync(1, Convert.ToUInt16(item_D.PlcItemModbusAddress), ushort.Parse(writeValue));
master.WriteSingleRegister(1, Convert.ToUInt16(item_D.PlcItemModbusAddress), ushort.Parse(writeValue));
return true;
case PlcItemTypeDesc.X:
return false;
@@ -792,18 +795,18 @@ namespace CleanMachineHMI
WritePlcItem("PlcHeartbeatWriteData", wcsToPlcHeartDataNew.ToString(), PlcItemTypeDesc.D);
}
if (DateTime.Now > HeartbeatTime.AddSeconds(5))//心跳超时
{
isConn = false;
//报警处理。
frmMainNew.Show_Plc_Alarm("PLC heartbeat data is abnormal, PLC loses connection!");
if (ReConnNum % 5 == 0)
{
ClosePlcConn();
PlcInit();
}
ReConnNum++;
}
//if (DateTime.Now > HeartbeatTime.AddSeconds(5))//心跳超时
//{
// isConn = false;
// //报警处理。
// frmMainNew.Show_Plc_Alarm("PLC heartbeat data is abnormal, PLC loses connection!");
// if (ReConnNum % 5 == 0)
// {
// ClosePlcConn();
// PlcInit();
// }
// ReConnNum++;
//}
}
catch
{