增加报警信息代码
This commit is contained in:
@@ -60,5 +60,48 @@ namespace JY.DAL
|
||||
{ "CCDI5", "左面凹坑数量" },
|
||||
{ "CCDI6", "右面凹坑数量" }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// 报警地址信息: 起始地址-长度(分组数,每个分组10个寄存器)
|
||||
/// </summary>
|
||||
public static readonly Dictionary<int, ushort> AlarmAddressDic = new Dictionary<int, ushort>()
|
||||
{
|
||||
{ 10010, 15 }
|
||||
};
|
||||
|
||||
// 展开后的平铺映射表:Key为连续的位索引(0, 1, 2...),Value为实际的物理地址
|
||||
public static readonly Dictionary<int, int> FlatAddressMap = new Dictionary<int, int>();
|
||||
|
||||
/// <summary>
|
||||
/// 根据数组索引获取对应的物理地址
|
||||
/// </summary>
|
||||
public static int GetAddressByIndex(int index)
|
||||
{
|
||||
if (FlatAddressMap.TryGetValue(index, out int address))
|
||||
{
|
||||
return address;
|
||||
}
|
||||
throw new IndexOutOfRangeException($"索引 {index} 超出了地址字典的映射范围。");
|
||||
}
|
||||
|
||||
static ConstValue()
|
||||
{
|
||||
FlatAddressMap = new Dictionary<int, int>();
|
||||
int bitIndex = 0;
|
||||
|
||||
// 按起始地址排序,确保映射顺序正确
|
||||
foreach (var kvp in AlarmAddressDic.OrderBy(x => x.Key))
|
||||
{
|
||||
int startAddress = kvp.Key;
|
||||
ushort length = kvp.Value;
|
||||
|
||||
// 将当前段的每一位索引映射到具体的物理地址
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
FlatAddressMap[bitIndex] = startAddress + i;
|
||||
bitIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using JY.Inspection.Entity;
|
||||
using JY.DAL;
|
||||
using JY.Inspection.Entity;
|
||||
using NPOI.SS.Formula.Functions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
@@ -47,6 +50,53 @@ namespace JY.Inspection.Common
|
||||
return listAlarmStatus;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 欧姆龙NX系列Ethernet/IP通讯时将报警地址值转成报警List {直接读取标签地址/无需高低位互换}
|
||||
/// </summary>
|
||||
/// <param name="plcAddrSuffix">PLC地址值,如100</param>
|
||||
/// <param name="byteData">PLC读取出来的值</param>
|
||||
/// <param name="plcAddrPrefix">PLC地址类型,默认值R</param>
|
||||
/// <returns></returns>
|
||||
public static List<AlarmStatus> OmronEIPByte2Status(List<byte> byteData, char plcAddrPrefix = 'D')
|
||||
{
|
||||
var listAlarmStatus = new List<AlarmStatus>();
|
||||
// byte[]转为二进制字符串表示
|
||||
string strResult = "";
|
||||
byte[] revBytes = SWAPbyte(byteData.ToArray()); //字节高低位互换
|
||||
for (int i = 0; i < revBytes.Length; i++)
|
||||
{
|
||||
string strTemp = System.Convert.ToString(revBytes[i], 2);
|
||||
strTemp = strTemp.PadLeft(8, '0').StrReverse();
|
||||
strResult += strTemp;
|
||||
}
|
||||
|
||||
int groupIndex = -1;
|
||||
for (int i = 0; i < strResult.Length; i++)
|
||||
{
|
||||
var plcByte = i % 160;
|
||||
|
||||
//如果是16的倍数地址+1
|
||||
if (groupIndex == -1 || (plcByte % 160 == 0 && i != 0))
|
||||
{
|
||||
groupIndex += 1;
|
||||
}
|
||||
|
||||
int address = ConstValue.GetAddressByIndex(groupIndex);
|
||||
|
||||
//将地址和状态添加到结果集
|
||||
listAlarmStatus.Add(new AlarmStatus()
|
||||
{
|
||||
PLCAdress = $"{plcAddrPrefix}{address}.{plcByte.ToString()}",
|
||||
Status = strResult[i] == '1'
|
||||
});
|
||||
|
||||
//if (strResult[i] == '1')
|
||||
// Console.WriteLine($"{plcAddrPrefix}{plcAddrSuffix}.{plcByte.ToString("X")}:{strResult[i]}");
|
||||
}
|
||||
|
||||
return listAlarmStatus;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 欧姆龙NX系列Ethernet/IP通讯时将报警地址值转成报警List {直接读取标签地址/无需高低位互换}
|
||||
@@ -55,7 +105,7 @@ namespace JY.Inspection.Common
|
||||
/// <param name="byteData">PLC读取出来的值</param>
|
||||
/// <param name="plcAddrPrefix">PLC地址类型,默认值R</param>
|
||||
/// <returns></returns>
|
||||
public static List<AlarmStatus> OmronEIPByte2Status(int plcAddrSuffix, List<byte> byteData, char plcAddrPrefix = 'W')
|
||||
public static List<AlarmStatus> OmronEIPByte2Status(int plcAddrSuffix, List<byte> byteData, char plcAddrPrefix = 'D')
|
||||
{
|
||||
var listAlarmStatus = new List<AlarmStatus>();
|
||||
// byte[]转为二进制字符串表示
|
||||
|
||||
Generated
+13
@@ -33,6 +33,7 @@
|
||||
this.btn_stationArrival = new MetroFramework.Controls.MetroButton();
|
||||
this.btn_sendMQTTClient = new MetroFramework.Controls.MetroButton();
|
||||
this.btn_logPLCData = new MetroFramework.Controls.MetroButton();
|
||||
this.btn_testAlarm = new MetroFramework.Controls.MetroButton();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// btn_readCollectItemCfg
|
||||
@@ -85,11 +86,22 @@
|
||||
this.btn_logPLCData.UseSelectable = true;
|
||||
this.btn_logPLCData.Click += new System.EventHandler(this.btn_logPLCData_Click);
|
||||
//
|
||||
// btn_testAlarm
|
||||
//
|
||||
this.btn_testAlarm.Location = new System.Drawing.Point(426, 229);
|
||||
this.btn_testAlarm.Name = "btn_testAlarm";
|
||||
this.btn_testAlarm.Size = new System.Drawing.Size(125, 53);
|
||||
this.btn_testAlarm.TabIndex = 5;
|
||||
this.btn_testAlarm.Text = "报警测试";
|
||||
this.btn_testAlarm.UseSelectable = true;
|
||||
this.btn_testAlarm.Click += new System.EventHandler(this.btn_testAlarm_Click);
|
||||
//
|
||||
// FrmTest
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(998, 586);
|
||||
this.Controls.Add(this.btn_testAlarm);
|
||||
this.Controls.Add(this.btn_logPLCData);
|
||||
this.Controls.Add(this.btn_sendMQTTClient);
|
||||
this.Controls.Add(this.btn_stationArrival);
|
||||
@@ -108,5 +120,6 @@
|
||||
private MetroFramework.Controls.MetroButton btn_stationArrival;
|
||||
private MetroFramework.Controls.MetroButton btn_sendMQTTClient;
|
||||
private MetroFramework.Controls.MetroButton btn_logPLCData;
|
||||
private MetroFramework.Controls.MetroButton btn_testAlarm;
|
||||
}
|
||||
}
|
||||
@@ -74,5 +74,10 @@ namespace JY.Inspection.Frm
|
||||
|
||||
LogHelper.WriteErrorLine("错误日志");
|
||||
}
|
||||
|
||||
private void btn_testAlarm_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+35
-17
@@ -173,14 +173,15 @@ namespace JY.Inspection
|
||||
/// </summary>
|
||||
List<ChartDataType> NGList = new List<ChartDataType>();
|
||||
|
||||
//private readonly object lockObject = new object();
|
||||
/// <summary>
|
||||
/// 报警原始数据
|
||||
/// </summary>
|
||||
List<byte> listAlamByte = new List<byte>();
|
||||
//List<byte> listAlamByte = new List<byte>();
|
||||
//报警表单数据
|
||||
List<AlarmForm> listAlarmForm = null;
|
||||
//PLC报警状态值
|
||||
List<AlarmStatus> listAlarmStatus = null;
|
||||
//List<AlarmStatus> listAlarmStatus = null;
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -278,6 +279,8 @@ namespace JY.Inspection
|
||||
this.ControlBox = true;
|
||||
// 或设置窗口可调整大小
|
||||
this.Resizable = true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void InitDgvBColumns()
|
||||
@@ -560,6 +563,8 @@ namespace JY.Inspection
|
||||
CheckClassShift();
|
||||
InitOmronCom();
|
||||
|
||||
string strFileName = Environment.CurrentDirectory + "\\欧姆龙报警寄存器对应表.csv";
|
||||
listAlarmForm = CSVHelper<AlarmForm>.ReadCSV(strFileName,",");
|
||||
|
||||
CancellationToken token1 = mctsA1.Token;
|
||||
CancellationToken token2 = mctsA2.Token;
|
||||
@@ -739,7 +744,7 @@ namespace JY.Inspection
|
||||
IsStartNGFL = IniFileHelper.ReadIniData("MES配置", "StartNGFL") == "1" ? true : false;
|
||||
chkStartNGFL.Checked = IsStartNGFL;
|
||||
|
||||
listAlamByte = GetListByte(200);
|
||||
//listAlamByte = GetListByte(200);
|
||||
|
||||
|
||||
Global.systemConfig.isUpMes = IniFileHelper.ReadIniData("SYSTEM_CONFIGURE", "IsMesUP") == "1" ? true : false;
|
||||
@@ -3123,30 +3128,43 @@ namespace JY.Inspection
|
||||
if (!mt.IsCancellationRequested)
|
||||
{
|
||||
sw.Restart();
|
||||
byte[] res = new byte[2];
|
||||
for (int i = 0; i < 30; i++)
|
||||
//byte[] res = new byte[2];
|
||||
//for (int i = 0; i < 30; i++)
|
||||
//{
|
||||
// res = omronPLCCom.lstMcUI[0].Readbyte("W6100[" + i + "].Word区", 1);
|
||||
// if (res != null)
|
||||
// {
|
||||
// for (int j = i; j < res.Length + i; j++)
|
||||
// {
|
||||
// listAlamByte[i + j] = res[j - i];
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
byte[] res = null;
|
||||
List<byte> listAlamByte = new List<byte>();
|
||||
foreach (var address in ConstValue.AlarmAddressDic.Keys)
|
||||
{
|
||||
res = omronPLCCom.lstMcUI[0].Readbyte("W6100[" + i + "].Word区", 1);
|
||||
if (res != null)
|
||||
ushort byteLen = (ushort)(ConstValue.AlarmAddressDic[address] * 10);
|
||||
res = omronPLCCom.lstMcUI[0].ReadByteFins($"D{address}", byteLen);
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
for (int j = i; j < res.Length + i; j++)
|
||||
{
|
||||
listAlamByte[i + j] = res[j - i];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
listAlamByte.AddRange(res);
|
||||
}
|
||||
|
||||
//AddLog(0, "读取报警耗时:" + sw.ElapsedMilliseconds + "ms");
|
||||
if (listAlamByte == null) { return; }
|
||||
string strFileName = Environment.CurrentDirectory + "\\欧姆龙报警寄存器对应表.csv";
|
||||
listAlarmForm = CSVHelper<AlarmForm>.ReadCSV(strFileName);
|
||||
|
||||
//string strFileName = Environment.CurrentDirectory + "\\欧姆龙报警寄存器对应表.csv";
|
||||
//listAlarmForm = CSVHelper<AlarmForm>.ReadCSV(strFileName);
|
||||
//如果未配置报警地址信息返回 TODO
|
||||
if (listAlarmForm == null) { return; }
|
||||
int startPlcAddr = 0;//TODO 读取起始地址
|
||||
//3.公用方法解析byte[]数据 => List<AlarmStatus>
|
||||
if (listAlamByte == null) { return; } //如果未配置报警地址信息返回
|
||||
|
||||
//TODO 切换三菱和欧姆龙
|
||||
listAlarmStatus = PLCAlarmParse.OmronEIPByte2Status(startPlcAddr, listAlamByte);
|
||||
List<AlarmStatus> listAlarmStatus = PLCAlarmParse.OmronEIPByte2Status(listAlamByte);
|
||||
//4.判定有不良后,关联AlarmStatus 和AlarmForm
|
||||
if (listAlarmStatus == null) { return; } //状态信息为空返回
|
||||
|
||||
|
||||
@@ -331,7 +331,7 @@
|
||||
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
|
||||
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
|
||||
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAAE
|
||||
CgAAAk1TRnQBSQFMAgEBAwEAAQgBCQEIAQkBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
|
||||
CgAAAk1TRnQBSQFMAgEBAwEAASgBCQEoAQkBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
|
||||
AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA
|
||||
AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5
|
||||
AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA
|
||||
|
||||
@@ -64,6 +64,8 @@ namespace PLCCommunication
|
||||
/// 欧姆龙Fins协议
|
||||
/// </summary>
|
||||
public HslCommunication.Profinet.Omron.OmronCipNet omronCipNet = null;
|
||||
|
||||
public HslCommunication.Profinet.Omron.OmronFinsNet omronFinsNet = null;
|
||||
/// <summary>
|
||||
/// 是否链接PLC
|
||||
/// </summary>
|
||||
@@ -198,6 +200,10 @@ namespace PLCCommunication
|
||||
InitializeComponent();
|
||||
omronCipNet = new OmronCipNet();
|
||||
omronCipNet.ConnectTimeOut = 2000;
|
||||
|
||||
omronFinsNet = new OmronFinsNet();
|
||||
omronFinsNet.ConnectTimeOut = 2000;
|
||||
|
||||
PanelTest.Enabled = false;
|
||||
}
|
||||
|
||||
@@ -336,6 +342,11 @@ namespace PLCCommunication
|
||||
omronCipNet.Port = port;
|
||||
omronCipNet.Slot = solt;
|
||||
omronCipNet.ConnectClose();
|
||||
|
||||
omronFinsNet.IpAddress = address.ToString();
|
||||
omronFinsNet.Port = 9600;
|
||||
omronFinsNet.ConnectClose();
|
||||
|
||||
try
|
||||
{
|
||||
omronCipNet.ConnectTimeOut = 2000;
|
||||
@@ -356,6 +367,9 @@ namespace PLCCommunication
|
||||
AddLog(lstRecv,1, StringResources.Language.ConnectedFailed);
|
||||
}
|
||||
|
||||
|
||||
omronFinsNet.ConnectTimeOut = 2000;
|
||||
OperateResult connectFins = omronFinsNet.ConnectServer();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -939,6 +953,30 @@ namespace PLCCommunication
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] ReadByteFins(string iAddr, ushort length)
|
||||
{
|
||||
byte[] result = null;
|
||||
try
|
||||
{
|
||||
//string type = iAddr.Substring(0, 1);
|
||||
//if (type != "D" && type != "R")
|
||||
//{
|
||||
// AddLog(lstSend, 2, "暂只支持D,R存储区");
|
||||
//}
|
||||
if (IsConnected)
|
||||
{
|
||||
result = omronFinsNet.Read(iAddr, length).Content;
|
||||
AddLog(lstResWrite, 0, $"{iAddr} --> {result?.ToString()}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AddLog(lstResWrite, 2, $"{iAddr} --> {ex.Message}");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入byte数组
|
||||
/// </summary>
|
||||
|
||||
Binary file not shown.
+2402
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user