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.

331 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonFunc;
using MySql.Data.MySqlClient;
using CentralControl.BaseData;
using Snap7;
using System.Threading;
//using Org.BouncyCastle.OpenSsl;
namespace CentralControl.App_Code
{
public class PlcHelper
{
private S7Client Client;
private object plcLock = new object();
private object plcLockfor = new object();
private object plcLockRead = new object();
public PlcHelper()
{
Client = new S7Client();
}
public bool Connection(string ipaddress)
{
try
{
int Result;
int Rack = 0;
int Slot = 0;
Result = Client.ConnectTo(ipaddress, Rack, Slot);
if (Result == 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex) { return false; }
}
int plcaddressindex = 0;
string plcaddress1 = "";
public static readonly object lockSendMaterielNumByKB = new object();
int inProcessing = 0;
public object Read(PlcSetting data)
{
if (Interlocked.CompareExchange(ref inProcessing, 1, 0) == 0)
{
try
{
int DBNumber;
int Size;
int Result;
string strdbb = "";
if (data == null)
{
return null;
}
//寄存器位置
string[] plcaddress = data.PlcAddress.Split('.');
//取数据库DB位置
DBNumber = 0;
try
{
DBNumber = Convert.ToInt32(plcaddress[0].Replace("DB", ""));
}
catch (Exception ex)
{
}
int start = 0;
//判断点位数据类型
if (data.PlcDataType == 1)//字
{
start = Convert.ToInt32(plcaddress[1].Replace("DBW", ""));
Size = Convert.ToInt32(data.PlcValueLength);
//获取偏移量
lock (plcLock)
{
byte[] Buffer = new byte[65536];
//Result = Client.DBRead(DBNumber, start, Size, Buffer);
Result = Client.DBRead(DBNumber, start, Size, Buffer);
if (Result == 0)
return Buffer[0] << 8 | Buffer[1];
else
return null;
}
}
else if (data.PlcDataType == 0)//字符
{
lock (plcLock)
{
strdbb = "";
start = Convert.ToInt32(plcaddress[1].Replace("DBB", ""));
//设置长度
Size = System.Convert.ToInt32(data.PlcValueLength);
//获取偏移量
byte[] Buffer = new byte[13];
Result = Client.DBRead(DBNumber, start, Size, Buffer);
if (Result == 0)
{
try
{
if (Buffer != null && Buffer.Count() > 12)
{
strdbb += ((char)int.Parse(Buffer[2].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[3].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[4].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[5].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[6].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[7].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[8].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[9].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[10].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[11].ToString())).ToString();
strdbb += ((char)int.Parse(Buffer[12].ToString())).ToString();
}
else { return null; }
}
catch (Exception ex)
{
return null;
}
return strdbb;
}
else
return null;
}
}
else if (data.PlcDataType == 2)//字符
{
start = Convert.ToInt32(plcaddress[1].Replace("DBX", ""));
//设置长度
Size = System.Convert.ToInt32(plcaddress[2]);
//获取偏移量
lock (plcLock)
{
byte[] Buffer = new byte[8];
//Result = Client.DBRead(DBNumber, start, Size, Buffer);
Result = Client.DBRead(DBNumber, start, 1, Buffer);
if (Result == 0)
{
int ret = Convert.ToInt32(Buffer[0]);
string str = Convert.ToString(Convert.ToInt32(ret), 2).PadLeft(8, '0');
if (int.Parse(plcaddress[2]) == 0)
{
return str.Substring(7, 1);
}
else if (int.Parse(plcaddress[2]) == 1)
{
return str.Substring(6, 1);
}
else if (int.Parse(plcaddress[2]) == 2)
{
return str.Substring(5, 1);
}
else if (int.Parse(plcaddress[2]) == 3)
{
return str.Substring(4, 1);
}
else if (int.Parse(plcaddress[2]) == 4)
{
return str.Substring(3, 1);
}
else if (int.Parse(plcaddress[2]) == 5)
{
return str.Substring(2, 1);
}
else if (int.Parse(plcaddress[2]) == 6)
{
return str.Substring(1, 1);
}
else if (int.Parse(plcaddress[2]) == 7)
{
return str.Substring(0, 1);
}
}
else
return null;
}
}
return null;
}
catch (Exception ex)
{
Logger logger = new Logger();
logger.Log("plc读点位异常:" + ex.Message + ex.StackTrace);
return null;
}
finally
{
inProcessing = 0;
}
}
else
{
return null;
}
}
public static readonly object lockwrite = new object();
public bool Write(PlcSetting data, object value)
{
lock (lockwrite)
{
try
{
int DBNumber;
int Size;
int Result;
//寄存器位置
string[] plcaddress = data.PlcAddress.Split('.');
//取数据库DB位置
DBNumber = Convert.ToInt32(plcaddress[0].Replace("DB", ""));
//判断点位数据类型
if (data.PlcDataType == 1)
{
}
//设置长度
Size = System.Convert.ToInt32(data.PlcValueLength);
lock (plcLock)
{
byte[] Buffer = new byte[65536];
//获取偏移量
S7.SetWordAt(Buffer, 0, Convert.ToUInt16(value));
int start = Convert.ToInt32(plcaddress[1].Replace("DBW", ""));
Result = Client.DBWrite(DBNumber, start, Size, Buffer);
Buffer = null;
if (Result == 0)
{
return true;
}
}
}
catch (Exception ex)
{
Logger logger = new Logger();
logger.Log("plc点位异常:" + ex.Message + ex.StackTrace);
return false;
}
}
Logger logger1 = new Logger();
logger1.Log("plc点位写入10次 没有成功:");
return false;
}
private string HexDump(byte[] bytes, int Size)
{
if (bytes == null)
return null;
int bytesLength = Size;
int bytesPerLine = 16;
char[] HexChars = "0123456789ABCDEF".ToCharArray();
int firstHexColumn =
8 // 8 characters for the address
+ 3; // 3 spaces
int firstCharColumn = firstHexColumn
+ bytesPerLine * 3 // - 2 digit for the hexadecimal value and 1 space
+ (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
+ 2; // 2 spaces
int lineLength = firstCharColumn
+ bytesPerLine // - characters to show the ascii value
+ Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
char[] line = (new String(' ', lineLength - 2) + Environment.NewLine).ToCharArray();
int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
StringBuilder result = new StringBuilder(expectedLines * lineLength);
for (int i = 0; i < bytesLength; i += bytesPerLine)
{
line[0] = HexChars[(i >> 28) & 0xF];
line[1] = HexChars[(i >> 24) & 0xF];
line[2] = HexChars[(i >> 20) & 0xF];
line[3] = HexChars[(i >> 16) & 0xF];
line[4] = HexChars[(i >> 12) & 0xF];
line[5] = HexChars[(i >> 8) & 0xF];
line[6] = HexChars[(i >> 4) & 0xF];
line[7] = HexChars[(i >> 0) & 0xF];
int hexColumn = firstHexColumn;
int charColumn = firstCharColumn;
for (int j = 0; j < bytesPerLine; j++)
{
if (j > 0 && (j & 7) == 0) hexColumn++;
if (i + j >= bytesLength)
{
line[hexColumn] = ' ';
line[hexColumn + 1] = ' ';
line[charColumn] = ' ';
}
else
{
byte b = bytes[i + j];
line[hexColumn] = HexChars[(b >> 4) & 0xF];
line[hexColumn + 1] = HexChars[b & 0xF];
line[charColumn] = (b < 32 ? '·' : (char)b);
}
hexColumn += 3;
charColumn++;
}
result.Append(line);
}
return result.ToString();
}
}
}