using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ro.CF.Communication;
using System.Data;
using System.Threading;
using Spire.Xls;
namespace WriteLog2._0
{
class Program
{
/// <summary>
/// 作者:--
/// 时间:2021-12-27
/// 版本:V2.1
///
/// </summary>
public static string PLC_IP;
public static bool PLCOpened = false;
public static S7Connector.Base SPS;
public static bool[] GB = new bool[1000], GB1 = new bool[1000];
public static short[] GSI = new short[1000], GSI1 = new short[1000];
public static int[] GI = new int[1000], GI1 = new int[1000];
public static char[] GC = new char[1000], GC1 = new char[1000];
public static string strtochar;
public static byte[] GCB = new byte[1000], GCB1 = new byte[1000];
public static float[] GF = new float[1000], GF1 = new float[1000];
public static byte[] GBT = new byte[1000], GBT1 = new byte[1000];
public static short DBNrb, DBNrsi, DBNri, DBNrc, DBNrf, DBNrbt;
public static short Countb, Countsi, Counti, Countc, Countf, Countbt;
public static int Startb, Startsi, Starti, Startc, Startf, Startbt;
public static string[] strbool = new string[1000];
public static string[] strint = new string[1000];
public static string[] strshort = new string[1000];
public static string[] strchar = new string[1000];
public static string[] strbyte = new string[1000];
public static string[] strfloat = new string[1000];
static void Main(string[] args)
{
//Console.ReadKey();
Console.WriteLine(SavaProcess("软件版本V2.1 最后一次修改2021-12-26"));
//读取EXCEL中需要读取的PLC地址及数据信息
Readxlsx();
again:
//尝试与PLC建立连接
try
{
//与PLC建立连接
SPS = new Ro.CF.Communication.S7Connector.TCPIP(PLC_IP, 102, 1000, Ro.CF.Communication.S7Connector.TypeSeries.S71200);
PLCOpened = true;
//连接成功后将读取数据建立新线程
Thread PLCConnectThread = new Thread(PLCConnector);
PLCConnectThread.IsBackground = true;
PLCConnectThread.Start();
Console.WriteLine(SavaProcess("连接成功"));
}
catch (Exception E)
{
PLCOpened = false;
Console.WriteLine(SavaProcess("连接没成功"));
Console.WriteLine(SavaProcess(E.Message + " "
+ System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
+ "-" + System.Reflection.MethodBase.GetCurrentMethod().Name + "Comm with PLC Error" + "\r\n"));
}
//与PLC连接失败后间隔5s再次进行连接
if (PLCOpened==false)
{
Thread.Sleep(5000);
goto again;
}
//建立新线程进行写数据
Thread WriteTextThread = new Thread(Writetxt);
WriteTextThread.Start();
}
/// <summary>
/// 保存数据data到文件的处理过程
/// </summary>
/// <param name="data"></param>
public static string SavaProcess(string data)
{
System.DateTime currentTime = System.DateTime.Now;
//获取当前日期转换成ToFileTime
string strYDM = currentTime.ToString("yyyyMMdd");
//按照日期建立一个文件名
string FileName = "LOG" + strYDM + ".txt";
//设置记录时间
string datatime = currentTime.ToString("yyyy-MM-dd-HH:mm:ss.fff");
//设置目录
string CurDir = System.AppDomain.CurrentDomain.BaseDirectory + @"\\LogDir";
//判断路径是否存在
if (!System.IO.Directory.Exists(CurDir))
{
System.IO.Directory.CreateDirectory(CurDir);
}
//不存在就创建
String FilePath = CurDir + @"\\" + FileName;
//文件覆盖方式添加内容
System.IO.StreamWriter file = new System.IO.StreamWriter(FilePath, true);
//保存数据到文件
file.WriteLine(datatime + "==" + data);
//关闭文件
file.Close();
//释放对象
file.Dispose();
return datatime + " " + data;
}
static void Readxlsx()
{
try
{
Workbook wb = new Workbook();
//加载文件
wb.LoadFromFile("PLCconfig.xlsx");
//打开第一个表格
Worksheet sheet = wb.Worksheets[0];
//读取表格数据
CellRange range = sheet.Range["B1"];
PLC_IP = range.Value;
Console.WriteLine(SavaProcess("PLCIP " + PLC_IP));
range = sheet.Range["B3"];
DBNrb = Convert.ToInt16(range.Value);
range = sheet.Range["B4"];
Startb = Convert.ToInt32(range.Value);
range = sheet.Range["B5"];
Countb = Convert.ToInt16(range.Value);
Console.WriteLine(SavaProcess("Datatype Bool DBnumber " + DBNrb+" StartAdress "+ Startb+" Count "+ Countb));
range = sheet.Range["C3"];
DBNrbt = Convert.ToInt16(range.Value);
range = sheet.Range["C4"];
Startbt = Convert.ToInt32(range.Value);
range = sheet.Range["C5"];
Countbt = Convert.ToInt16(range.Value);
Console.WriteLine(SavaProcess("Datatype BYTE DBnumber " + DBNrbt + " StartAdress " + Startbt + " Count " + Countbt));
range = sheet.Range["D3"];
DBNrsi = Convert.ToInt16(range.Value);
range = sheet.Range["D4"];
Startsi = Convert.ToInt32(range.Value);
range = sheet.Range["D5"];
Countsi = Convert.ToInt16(range.Value);
Console.WriteLine(SavaProcess("Datatype INT DBnumber " + DBNrsi + " StartAdress " + Startsi + " Count " + Countsi));
range = sheet.Range["E3"];
DBNri = Convert.ToInt16(range.Value);
range = sheet.Range["E4"];
Starti = Convert.ToInt32(range.Value);
range = sheet.Range["E5"];
Counti = Convert.ToInt16(range.Value);
Console.WriteLine(SavaProcess("Datatype DINT DBnumber " + DBNri + " StartAdress " + Starti + " Count " + Counti));
range = sheet.Range["F3"];
DBNrc = Convert.ToInt16(range.Value);
range = sheet.Range["F4"];
Startc = Convert.ToInt32(range.Value);
range = sheet.Range["F5"];
Countc = Convert.ToInt16(range.Value);
Console.WriteLine(SavaProcess("Datatype CHAR DBnumber " + DBNrc + " StartAdress " + Startc + " Count " + Countc));
range = sheet.Range["G3"];
DBNrf = Convert.ToInt16(range.Value);
range = sheet.Range["G4"];
Startf = Convert.ToInt32(range.Value);
range = sheet.Range["G5"];
Countf = Convert.ToInt16(range.Value);
Console.WriteLine(SavaProcess("Datatype REAL DBnumber " + DBNrf + " StartAdress " + Startf + " Count " + Countf));
if (DBNrb != 0)
{
int k = Countb * 8;
for (int i = 0; i < k; i++)
{
int j = i + 6;
range = sheet.Range["B" + j];
strbool[i] = range.Value;
Console.WriteLine(SavaProcess("BOOL变量名称 " + i + " " + strbool[i]));
}
}
else
{
Console.WriteLine(SavaProcess("BOOL变量未使用 "));
}
if (DBNrbt != 0)
{
for (int i = 0; i < Countbt; i++)
{
int j = i + 6;
range = sheet.Range["C" + j];
strbyte[i] = range.Value;
Console.WriteLine(SavaProcess("BYTE变量名称 " + i + " " + strbyte[i]));
}
}
else
{
Console.WriteLine(SavaProcess("BYTE变量未使用 "));
}
if (DBNrsi != 0)
{
for (int i = 0; i < Countsi; i++)
{
int j = i + 6;
range = sheet.Range["D" + j];
strshort[i] = range.Value;
Console.WriteLine(SavaProcess("INT变量名称 " + i + " " + strshort[i]));
}
}
else
{
Console.WriteLine(SavaProcess("INT变量未使用 "));
}
if (DBNri != 0)
{
for (int i = 0; i < Counti; i++)
{
int j = i + 6;
range = sheet.Range["E" + j];
strint[i] = range.Value;
Console.WriteLine(SavaProcess("DINT变量名称 " + i + " " + strint[i]));
}
}
else
{
Console.WriteLine(SavaProcess("DINT变量未使用 "));
}
if (DBNrc != 0)
{
for (int i = 0; i < Countc; i++)
{
int j = i + 6;
range = sheet.Range["F" + j];
strchar[i] = range.Value;
Console.WriteLine(SavaProcess("CHAR变量名称 " + i + " " + strchar[i]));
}
}
else
{
Console.WriteLine(SavaProcess("CHAR变量未使用 "));
}
if (DBNrf != 0)
{
for (int i = 0; i < Countf; i++)
{
int j = i + 6;
range = sheet.Range["G" + j];
strfloat[i] = range.Value;
Console.WriteLine(SavaProcess("REAL变量名称 " + i + " " + strfloat[i]));
}
}
else
{
Console.WriteLine(SavaProcess("REAL变量未使用 "));
}
}
catch (Exception ex)
{
Console.WriteLine(SavaProcess(ex.Message + " "
+ System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
+ "-" + System.Reflection.MethodBase.GetCurrentMethod().Name + "\r\n@PLC Comm InitializeComponent\r\n"));
}
}
static void PLCConnector()
{
while (true)
{
if (SPS != null && SPS.Get_ConnectionState() != ConnectionState.Open)
{
try
{
SPS = new Ro.CF.Communication.S7Connector.TCPIP(PLC_IP, 102, 1000, Ro.CF.Communication.S7Connector.TypeSeries.S71200);
PLCOpened = true;
Console.WriteLine(SavaProcess("连接成功2"));
}
catch (Exception E)
{
PLCOpened = false;
Console.WriteLine(SavaProcess("连接没成功B"));
Console.WriteLine(SavaProcess(E.Message + " "
+ System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName
+ "-" + System.Reflection.MethodBase.GetCurrentMethod().Name + "Comm with PLC Error" + "\r\n"));
}
}
else
{
while (PLCOpened == true && SPS.Get_ConnectionState() == ConnectionState.Open)
{
try
{
//读PLC数据
//读取bool量
Ro.CF.Communication.S7Connector.DataTypes Read_Selected_DataType_Bool = new Ro.CF.Communication.S7Connector.DataTypes();
Read_Selected_DataType_Bool = Ro.CF.Communication.S7Connector.DataTypes.BOOL;
Ro.CF.Communication.S7Connector.DataTypes Read_Selected_DataType_Byte = new Ro.CF.Communication.S7Connector.DataTypes();
Read_Selected_DataType_Byte = Ro.CF.Communication.S7Connector.DataTypes.BYTE;
Ro.CF.Communication.S7Connector.DataTypes Read_Selected_DataType_INT = new Ro.CF.Communication.S7Connector.DataTypes();
Read_Selected_DataType_INT = Ro.CF.Communication.S7Connector.DataTypes.INT;
Ro.CF.Communication.S7Connector.DataTypes Read_Selected_DataType_DWORD = new Ro.CF.Communication.S7Connector.DataTypes();
Read_Selected_DataType_DWORD = Ro.CF.Communication.S7Connector.DataTypes.DWORD;
Ro.CF.Communication.S7Connector.DataTypes Read_Selected_DataType_REAL = new Ro.CF.Communication.S7Connector.DataTypes();
Read_Selected_DataType_REAL = Ro.CF.Communication.S7Connector.DataTypes.REAL;
Ro.CF.Communication.S7Connector.MemmoryTypes Read_Selected_MemmoryType_Datablock = new Ro.CF.Communication.S7Connector.MemmoryTypes();
Read_Selected_MemmoryType_Datablock = Ro.CF.Communication.S7Connector.MemmoryTypes.Datablock;
Ro.CF.Communication.S7Connector.MemmoryTypes Read_Selected_MemmoryType_Input = new Ro.CF.Communication.S7Connector.MemmoryTypes();
Read_Selected_MemmoryType_Input = Ro.CF.Communication.S7Connector.MemmoryTypes.Input;
Ro.CF.Communication.S7Connector.MemmoryTypes Read_Selected_MemmoryType_Output = new Ro.CF.Communication.S7Connector.MemmoryTypes();
Read_Selected_MemmoryType_Output = Ro.CF.Communication.S7Connector.MemmoryTypes.Output;
if (DBNrb != 0)
{
object Result = SPS.AG_Read(DBNrb, Startb, Countb, Read_Selected_DataType_Bool, Read_Selected_MemmoryType_Datablock);
GB = (bool[])Result;//和声明的GS=bool[192]的关系 X8倍
}
if (DBNrbt != 0)
{
object Result = SPS.AG_Read(DBNrbt, Startbt, Countbt, Read_Selected_DataType_Byte, Read_Selected_MemmoryType_Datablock);
GBT = (byte[])Result;
}
if (DBNrsi != 0)
{
object Result = SPS.AG_Read(DBNrsi, Startsi, Countsi, Read_Selected_DataType_INT, Read_Selected_MemmoryType_Datablock);
GSI = (short[])Result;
}
if (DBNri != 0)
{
object Result = SPS.AG_Read(DBNri, Starti, Counti, Read_Selected_DataType_DWORD, Read_Selected_MemmoryType_Datablock);
GI = (int[])Result;
}
if (DBNrf != 0)
{
object Result = SPS.AG_Read(DBNrf, Startf, Countf, Read_Selected_DataType_REAL, Read_Selected_MemmoryType_Datablock);
GF = (float[])Result;
}
if (DBNrc != 0)
{
object Result = SPS.AG_Read(DBNrc, Startc, Countc, Read_Selected_DataType_Byte, Read_Selected_MemmoryType_Datablock);
GCB = (byte[])Result;
}
}
catch (Exception e)
{
Console.WriteLine(SavaProcess(e.ToString() + "SPS.AG_READ ERROR"));
}
}
}
Thread.Sleep(1000);
}
}
static void Writetxt()
{
while (true)
{
int k = Countb * 8;
for (int i = 0; i <k; i++)
{
if (GB[i] != GB1[i])
{
Console.WriteLine(SavaProcess(strbool[i] + " " + GB1[i] + "--->" + GB[i]));
GB1[i] = GB[i];
}
}
for (int i = 0; i < Countbt; i++)
{
if (GBT[i] != GBT1[i])
{
Console.WriteLine(SavaProcess(strbyte[i] + " " + GBT1[i] + "--->" + GBT[i]));
GBT1[i] = GBT[i];
}
}
for (int i = 0; i < Countsi; i++)
{
if (GSI[i] != GSI1[i])
{
Console.WriteLine(SavaProcess(strshort[i] + " " + GSI1[i] + "--->" + GSI[i]));
GSI1[i] = GSI[i];
}
}
for (int i = 0; i < Counti; i++)
{
if (GI[i] != GI1[i])
{
Console.WriteLine(SavaProcess(strint[i] + " " + GI1[i] + "--->" + GI[i]));
GI1[i] = GI[i];
}
}
for (int i = 0; i < Countf; i++)
{
if (GF[i] != GF1[i])
{
Console.WriteLine(SavaProcess(strfloat[i] + " " + GF1[i] + "--->" + GF[i]));
GF1[i] = GF[i];
}
}
strtochar = Convert.ToString(System.Text.Encoding.ASCII.GetString(GCB));
GC = strtochar.ToCharArray();
for (int i = 0; i < Countc; i++)
{
if (GC[i] != GC1[i])
{
Console.WriteLine(SavaProcess(strchar[i] + " " + GC1[i] + "--->" + GC[i]));
GC1[i] = GC[i];
}
}
}
}
}
}