You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
9.1 KiB
C#

using Admin.Core.Common.Helper;
using Aucma.Core.HwPLc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Aucma.Core.OldBoxFoam.Business
{
public class Collection
{
public HwPLc.PlcModel plc1 = null;
public Collection() {
startCollect();
}
public void startCollect()
{
Task.Run(() =>
{
Thread.Sleep(3000);
plc1 = HwPLc.PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("OldMelsecPlc1"));
while (true)
{
ReadBoxType(plc1);
Thread.Sleep(1000);
}
});
}
/// <summary>
/// 读取夹具箱型
/// </summary>
static void ReadBoxType(HwPLc.PlcModel obj)
{
//夹具箱型
byte[] result = obj.plc.Read("D3140", 48);
byte[] info = result;
int temp1 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(0).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp2 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(4).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp3 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(8).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp4 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(12).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp5 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(16).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp6 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(18).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
Console.WriteLine($"读取夹具箱型,1#:{temp1};2#{temp2};3#:{temp3};4#:{temp4};5#:{temp5};6#:{temp6}");
}
/// <summary>
/// 读取夹具状态
/// </summary>
static void ReadHolderStatus(PlcModel obj1, PlcModel obj2)
{
byte[] result = obj1.plc.Read("YF0", 1);
//输出二进制字符串,1#-3#手动、自动数据
string str1 = ByteArrayToBinary(array(result));
byte[] result2 = obj1.plc.Read("Y110", 1);
string str2 = ByteArrayToBinary(array(result2));
//1#
int item1 = 0;
if (str1.Substring(0, 1) == "1")
{
item1 = 1;
}
else if (str1.Substring(1, 1) == "1")
{
item1 = 2;
}
else if (str2.Substring(0, 1) == "1")
{
item1 = 3;
}
//2#
int item2 = 0;
if (str1.Substring(5, 1) == "1")
{
item2 = 1;
}
else if (str1.Substring(6, 1) == "1")
{
item2 = 2;
}
else if (str2.Substring(1, 1) == "1")
{
item2 = 3;
}
//3#
int item3 = 0;
if (str1.Substring(10, 1) == "1")
{
item3 = 1;
}
else if (str1.Substring(11, 1) == "1")
{
item3 = 2;
}
else if (str2.Substring(2, 1) == "1")
{
item3 = 3;
}
byte[] result3 = obj2.plc.Read("Y100", 1);
string str3 = ByteArrayToBinary(array(result3));
int item4 = 0;
if (str3.Substring(0, 1) == "1")
{
item4 = 1;
}
else if (str3.Substring(1, 1) == "1")
{
item4 = 2;
}
else if (str2.Substring(3, 1) == "1")
{
item4 = 3;
}
int item5 = 0;
if (str3.Substring(5, 1) == "1")
{
item5 = 1;
}
else if (str3.Substring(6, 1) == "1")
{
item5 = 2;
}
else if (str2.Substring(4, 1) == "1")
{
item5 = 3;
}
int item6 = 0;
if (str3.Substring(10, 1) == "1")
{
item6 = 1;
}
else if (str3.Substring(11, 1) == "1")
{
item6 = 2;
}
else if (str2.Substring(5, 1) == "1")
{
item6 = 3;
}
Console.WriteLine($"读取夹具状态,1#:{item1};2#:{item2};3#:{item3};4#:{item4};5#:{item5};6#:{item6}");
}
/// <summary>
/// 读取夹具产量
/// </summary>
static void ReadHolderOutPut(PlcModel obj)
{
byte[] result = obj.plc.Read("D182", 24);
byte[] info = result;
int temp1 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(0).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp2 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(2).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp3 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(4).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp4 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(6).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp5 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(8).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp6 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(10).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
Console.WriteLine($"读取夹具产量,1#:{temp1};2#{temp2};3#:{temp3};4#:{temp4};5#:{temp5};6#:{temp6}");
}
/// <summary>
/// 读取固化时间
/// </summary>
static void ReadSolidifyTime(PlcModel obj)
{
byte[] result = obj.plc.Read("D910", 24);
byte[] info = result;
int temp1 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(0).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp2 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(2).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp3 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(4).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp4 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(6).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp5 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(8).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp6 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(10).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
Console.WriteLine($"读取固化时间,1#:{temp1};2#{temp2};3#:{temp3};4#:{temp4};5#:{temp5};6#:{temp6}");
}
/// <summary>
/// 读取泡前库箱型
/// </summary>
static void ReadStoreType(PlcModel obj)
{
//夹具箱型
byte[] result = obj.plc.Read("D2010", 40);
byte[] info = result;
int temp1 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(0).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp2 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(20).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp3 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(40).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
int temp4 = short.Parse(StringChange.bytesToHexStr(array(info.Skip(60).Take(2).ToArray()), 2), System.Globalization.NumberStyles.HexNumber);
Console.WriteLine($"读取泡前库箱型,1#:{temp1};2#{temp2};3#:{temp3};4#:{temp4}");
}
/// <param name="data"></param>
/// <returns></returns>
static byte[] array(byte[] data)
{
for (int i = 0; i < data.Length; i += 2)
{
byte temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
return data;
}
/// <summary>
/// byte[]转二进制
/// </summary>
/// <param name="bytes"></param>
static string ByteArrayToBinary(byte[] bytes)
{
StringBuilder binaryString = new StringBuilder();
foreach (byte b in bytes)
{
binaryString.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
}
Console.WriteLine(binaryString);
string reversedStr = new string(binaryString.ToString().Reverse().ToArray());
Console.WriteLine(reversedStr);
return reversedStr;
}
}
}