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.

181 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mesnac.Equips;
namespace Mesnac.Equip.FictitiousPlc.BinaryFilePlc.Default
{
public class Equip : BaseEquip
{
private int[][] _data = null;
private bool _isOpen = false; //是否打开连接
private DateTime _lastWriteTime = DateTime.Now;
#region BaseEquip 实现函数
/// <summary>
/// 打开设备
/// </summary>
/// <returns></returns>
public override bool Open()
{
if (this._isOpen == true)
{
return true;
}
this.State = false;
if (System.IO.File.Exists(BinaryFileHelper.Instance.FilePath))
{
try
{
this._data = Mesnac.Basic.SerializeHandler.Deserialize<int[][]>(BinaryFileHelper.Instance.FilePath);
this._lastWriteTime = this.GetLastWriteDate();
base.State = true;
this._isOpen = true;
}
catch (Exception ex)
{
base.State = false;
ICSharpCode.Core.LoggingService.Error(String.Format("打开二进制文件{0}设备失败:{1}", BinaryFileHelper.Instance.FilePath, ex.Message));
}
}
else
{
base.State = false;
ICSharpCode.Core.LoggingService.Error(String.Format("二进制文件设备不存在:{0}!", BinaryFileHelper.Instance.FilePath));
}
return base.State;
}
/// <summary>
/// 读取设备
/// </summary>
/// <param name="block"></param>
/// <param name="start"></param>
/// <param name="len"></param>
/// <param name="buff"></param>
/// <returns></returns>
public override bool Read(string block, int start, int len, out object[] buff)
{
bool result = false;
buff = new object[len];
try
{
if (!this.Open())
{
return false;
}
if (this._lastWriteTime != this.GetLastWriteDate())
{
this._data = Mesnac.Basic.SerializeHandler.Deserialize<int[][]>(BinaryFileHelper.Instance.FilePath);
this._lastWriteTime = this.GetLastWriteDate();
}
int iblock = 0;
if (int.TryParse(block, out iblock))
{
if (start + len <= this._data[iblock].Length)
{
for (int i = start; i < start + len; i++)
{
buff[i - start] = this._data[iblock][i];
}
result = true;
}
else
{
result = false;
ICSharpCode.Core.LoggingService.Error(String.Format("读设备块的长度不够,块号:{0},起始字:{1},长度:{2}", block, start, len));
}
}
else
{
result = false;
ICSharpCode.Core.LoggingService.Error(String.Format("读设备块不存在,块号:{0}", block));
}
this.State = result;
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(String.Format("读取二进制文件设备变量失败!-({0})", ex.Message));
this.State = false;
return false;
}
}
/// <summary>
/// 写入设备
/// </summary>
/// <param name="block"></param>
/// <param name="start"></param>
/// <param name="buff"></param>
/// <returns></returns>
public override bool Write(int block, int start, object[] buff)
{
bool result = false;
try
{
if (!this.Open())
{
return false;
}
if (block < this._data.Length)
{
if (start + buff.Length <= this._data[block].Length)
{
for (int i = start; i < start + buff.Length; i++)
{
int value = 0;
if (int.TryParse(buff[i - start].ToString(), out value))
{
this._data[block][i] = value;
}
}
Mesnac.Basic.SerializeHandler.Serialize<int[][]>(this._data, BinaryFileHelper.Instance.FilePath);
result = true;
}
else
{
result = false;
ICSharpCode.Core.LoggingService.Error(String.Format("写设备块的长度不够,块号:{0},起始字:{1},长度:{2}", block, start, buff));
}
}
else
{
result = false;
ICSharpCode.Core.LoggingService.Error(String.Format("写设备块不存在,块号:{0}", block));
}
return result;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(String.Format("写入二进制文件设备变量失败!-({0})", ex.Message));
return false;
}
}
/// <summary>
/// 关闭设备
/// </summary>
public override void Close()
{
this.State = false;
this._isOpen = false;
}
#endregion
/// <summary>
/// 获取文件最后修改时间
/// </summary>
/// <returns>返回文件最后修改时间</returns>
private DateTime GetLastWriteDate()
{
System.IO.FileInfo fi = new System.IO.FileInfo(BinaryFileHelper.Instance.FilePath);
return fi.LastWriteTime;
}
}
}