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.

345 lines
14 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using HslCommunication;
using HslCommunication.Profinet.Omron;
using HslCommunication.Profinet.Siemens;
using ICSharpCode.Core;
using Mesnac.Compressor.Data;
using Mesnac.Log;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace LaserMarking
{
internal class Program
{
private static int OldRFID = 0;
// 定时器对象
private static Timer _timer;
// 用于记录检测次数
private static int _checkCount = 0;
static HslCommunication.Profinet.Melsec.MelsecMcNet plc = new HslCommunication.Profinet.Melsec.MelsecMcNet();
public static bool IsConnected { get; set; }
static void Main(string[] args)
{
//Example();
init();
Connect("192.168.0.20", 2000);
CreatEquipClient();
Console.ReadLine();
}
public static bool Convert16(string decimalStr, out string hexStr, bool uppercase = true)
{
hexStr = string.Empty;
// 检查输入是否为空
if (string.IsNullOrWhiteSpace(decimalStr))
return false;
// 尝试将字符串解析为整数
if (int.TryParse(decimalStr, out int decimalNumber))
{
// 转换为16进制X表示大写x表示小写
hexStr = decimalNumber.ToString(uppercase ? "X" : "x");
return true;
}
// 如果int类型不够可以尝试long类型
if (long.TryParse(decimalStr, out long decimalLong))
{
hexStr = decimalLong.ToString(uppercase ? "X" : "x");
return true;
}
return false;
}
// 示例用法
public static void Example()
{
string decimalString = "4102";
if (Convert16(decimalString, out string hexString))
{
Console.WriteLine($"10进制 {decimalString} 转换为16进制是: {hexString}"); // 输出 FF
}
else
{
Console.WriteLine($"无法将 {decimalString} 转换为16进制");
}
}
private static void init()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("54c90c9e-3625-4136-b9c7-1f02885925c8"))
{
LogService.Instance.Debug("HslCommunication 11.0.6.0激活失败");
return;
}
LogService.Instance.Debug("HslCommunication 11.0.6.0激活成功");
plc.NetworkNumber = 0;
plc.NetworkStationNumber = 0;
plc.TargetIOStation = 1023;
plc.EnableWriteBitToWordRegister = false;
plc.ByteTransform.IsStringReverseByteWord = false;
}
public static bool Connect(string IP, int port)
{
plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(IP, port)
{
ConnectTimeOut = 5000, // 连接超时时间,单位毫秒
ReceiveTimeOut = 10000, // 接收设备数据反馈的超时时间
};
System.Net.IPAddress address;
if (!System.Net.IPAddress.TryParse(IP, out address))
{
// MessageBox.Show("Ip地址输入不正确");
return false;
}
plc.IpAddress = IP;
try
{
OperateResult connect = plc.ConnectServer();
if (connect.IsSuccess)
{
IsConnected = true;
return true;
}
else
{
LogService.Instance.Debug("连接失败!");
IsConnected = false;
return false;
}
}
catch (Exception ex)
{
LogService.Instance.Debug(ex.Message);
IsConnected = false;
return false;
}
}
public static string readStringByAddress(string address, ushort length)
{
try
{
OperateResult<String> read = plc.ReadString(address, length);
if (read.IsSuccess)
{
LogService.Instance.Debug(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
LogService.Instance.Debug(String.Format("通过PLC地址{0}读取string类型数据失败", address));
IsConnected = false;
return "";
}
catch (Exception ex)
{
LogService.Instance.Error("通过PLC地址读取int32类型数据异常", ex);
return "";
}
}
public static int readInt32ByAddress(string address)
{
int returnflag = 0;
try
{
//LogService.Instance.Debug(String.Format("开始通过PLC地址{0}读取int32类型数据", address));
OperateResult<Int32> read = plc.ReadInt32(address);
if (read.IsSuccess)
{
//LogService.Instance.Debug(String.Format("通过PLC地址{0}读取int32类型数据成功:{1}", address, read.Content));
returnflag = read.Content;
}
else
{
LogService.Instance.Error(String.Format("通过PLC地址{0}读取int32类型数据失败{1}", address, read.Message));
IsConnected = false;
}
}
catch (Exception ex)
{
IsConnected = false;
}
return returnflag;
}
public static int readInt16ByAddress(string address)
{
//LogHelper.PlcLog(String.Format("开始通过PLC地址{0}读取int32类型数据",address));
try
{
OperateResult<short> read = plc.ReadInt16(address);
if (read.IsSuccess)
{
if (read.Content != 0)
{
//LogService.Instance.Debug(String.Format("通过PLC地址{0}读取int16类型数据成功{1}", address, read.Content));
}
return read.Content;
}
LogService.Instance.Debug(String.Format("通过PLC地址{0}读取int16类型数据失败", address));
IsConnected = false;
return 0;
}
catch (Exception ex)
{
LogService.Instance.Error("通过PLC地址读取int16类型数据异常", ex);
IsConnected = false;
return 0;
}
}
public static bool writeInt16ByAddress(string address, int value)
{
//LogHelper.PlcLog(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, value));
try
{
OperateResult write = plc.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
LogService.Instance.Debug(String.Format("通过PLC地址{0}写入int16类型数据{1}成功", address, value));
return true;
}
LogService.Instance.Debug(String.Format("通过PLC地址{0}写入int16类型数据{1}失败!!!", address, value));
IsConnected = false;
return false;
}
catch (Exception ex)
{
LogService.Instance.Error(String.Format("通过PLC地址{0}写入int16类型数据异常", address), ex);
IsConnected = false;
return false;
}
}
public static bool writeValueByAddress(int value, string address)
{
//LogHelper.PlcLog(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
try
{
OperateResult operateResult = plc.Write(address, Convert.ToInt32(value));
if (operateResult.IsSuccess)
{
LogService.Instance.Debug(String.Format("开始通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
LogService.Instance.Debug(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
IsConnected = false;
return false;
}
catch (Exception ex)
{
LogService.Instance.Error("通过PLC地址写入int类型数据", ex);
IsConnected = false;
return false;
}
}
public static bool writeStringByAddress(string address, string value)
{
//LogHelper.PlcLog(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
try
{
OperateResult operateResult = plc.Write(address, value);
if (operateResult.IsSuccess)
{
LogService.Instance.Debug(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
LogService.Instance.Debug(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
//this.IsConnected = false;
return false;
}
catch (Exception ex)
{
LogService.Instance.Error(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
//this.IsConnected = false;
return false;
}
}
public static void CreatEquipClient()
{
var t = Task.Run(async delegate
{
while (true)
{
try
{
//查询交互字
int Flag = readInt16ByAddress("D1557");
if (Flag == 1)
{
//先置位
bool iflag = writeInt16ByAddress("D1557",0);
int RFID = readInt16ByAddress("D1555");
if (RFID != 0)
{
if (RFID != OldRFID)
{
OldRFID = RFID;
//通过RFID号查询产品码
LoggingService.Debug("获取RFID" + RFID);
string semibarcode = "";
//10进制转16进制
if (Convert16(RFID.ToString(), out string hexString))
{
LogService.Instance.Debug($"10进制 {RFID.ToString()} 转换为16进制是: {hexString}"); // 输出 FF
DbHandler dbHandler = new DbHandler();
semibarcode = DbHandler.GetTraySemiBarcode(hexString);
}
if (!string.IsNullOrEmpty(semibarcode))
{
string productbarcode = DbHandler.GetMainBarcode(semibarcode);
LogService.Instance.Debug("通过RFID获取条码" + productbarcode);
if (!string.IsNullOrEmpty(productbarcode))
{
//先置位
bool iflag1 = writeInt16ByAddress("D1557", 2);
//写入PLC
writeStringByAddress("D1561", productbarcode);
//int iflag1 = readInt16ByAddress("D1559");
//if (readInt16ByAddress("D1559") == 1)
//{
// bool iflag2 = writeInt16ByAddress("D1559", 2);
//}
}
else
{
//先置位
bool iflag1 = writeInt16ByAddress("D1557", 3);
}
}
else
{
//先置位
bool iflag1 = writeInt16ByAddress("D1557", 3);
}
}
}
}
}
catch (Exception ex)
{
LogService.Instance.Error(ex.ToString());
}
await Task.Delay(1000);
}
});
}
}
}