using HslCommunication.Core;
using HslCommunication.Serial;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HslCommunication.BasicFramework;
namespace HslCommunication.Profinet.LSIS
{
///
/// Inverter Of PC
///
public class LS_Bus : SerialDeviceBase
{
#region Constructor
///
///
///
public LS_Bus()
{
WordLength = 1;
ByteTransform = new RegularByteTransform();
}
#endregion
#region Public Member
///
/// PLC Station No.
///
public byte Station { get; set; } = 0x05;
#endregion
#region Read Write Support
///
///
///
///
///
///
public override OperateResult Read(string address, ushort length)
{
OperateResult command = BuildReadByteCommand(Station, address, length);
if (!command.IsSuccess) return command;
OperateResult read = ReadBase(command.Content);
if (!read.IsSuccess) return read;
return ExtractActualData(read.Content, true);
}
///
///
///
///
///
///
public override OperateResult Write(string address, byte[] value)
{
OperateResult command = BuildWriteByteCommand(Station, address, value);
if (!command.IsSuccess) return command;
OperateResult read = ReadBase(command.Content);
if (!read.IsSuccess) return read;
return ExtractActualData(read.Content, false);
}
#endregion
#region Object Override
///
///
///
///
public override string ToString()
{
return $"LS_Bus";
}
#endregion
#region Static Helper
///
/// Inverter continuous reading (R)
/// This is a function of continuous reading of designated amount of PLC data from designated address number.
///
/// plc station
/// address, for example: 0100
/// read length
/// command bytes
private static OperateResult BuildReadByteCommand(byte station, string address, ushort length)
{
var analysisResult = XGBFastEnet.AnalysisAddress(address);
if (!analysisResult.IsSuccess) return OperateResult.CreateFailedResult(analysisResult);
List command = new List();
command.Add(0x05); // ENQ
command.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
command.Add(0x52); // command R Read inverter variable of Word.
command.AddRange(Encoding.ASCII.GetBytes(analysisResult.Content));//Address of inverter
command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)length));
int sum = 0;
for (int i = 1; i < command.Count; i++)
{
sum += command[i];
}
command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)sum));
command.Add(0x04); // EOT
return OperateResult.CreateSuccessResult(command.ToArray());
}
///
/// Continuous writing to inverter device (W)
///
/// plc station
/// address, for example: 0100
/// source value
/// command bytes
private static OperateResult BuildWriteByteCommand(byte station, string address, byte[] value)
{
var analysisResult = XGBFastEnet.AnalysisAddress(address);
if (!analysisResult.IsSuccess) return OperateResult.CreateFailedResult(analysisResult);
List command = new List();
command.Add(0x05); // ENQ
command.AddRange(SoftBasic.BuildAsciiBytesFrom(station));
command.Add(0x57); // command W Write inverter variable of Word.
command.Add(0x06); // Device Length
command.AddRange(Encoding.ASCII.GetBytes(analysisResult.Content));
command.AddRange(SoftBasic.BytesToAsciiBytes(value));
int sum = 0;
for (int i = 1; i < command.Count; i++)
{
sum += command[i];
}
command.AddRange(SoftBasic.BuildAsciiBytesFrom((byte)sum));
command.Add(0x04); // EOT
return OperateResult.CreateSuccessResult(command.ToArray());
}
///
/// Extract actual data form plc response
///
/// response data
/// read
/// result
public static OperateResult ExtractActualData(byte[] response, bool isRead)
{
try
{
if (isRead)
{
if (response[0] == 0x06)
{
byte[] buffer = new byte[response.Length - 13];
Array.Copy(response, 10, buffer, 0, buffer.Length);
return OperateResult.CreateSuccessResult(SoftBasic.AsciiBytesToBytes(buffer));
}
else
{
byte[] buffer = new byte[response.Length - 9];
Array.Copy(response, 6, buffer, 0, buffer.Length);
return new OperateResult(BitConverter.ToUInt16(SoftBasic.AsciiBytesToBytes(buffer), 0), "Data:" + SoftBasic.ByteToHexString(response));
}
}
else
{
if (response[0] == 0x06)
{
return OperateResult.CreateSuccessResult(new byte[0]);
}
else
{
byte[] buffer = new byte[response.Length - 9];
Array.Copy(response, 6, buffer, 0, buffer.Length);
return new OperateResult(BitConverter.ToUInt16(SoftBasic.AsciiBytesToBytes(buffer), 0), "Data:" + SoftBasic.ByteToHexString(response));
}
}
}
catch (Exception ex)
{
return new OperateResult(ex.Message);
}
}
#endregion
}
}