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.

223 lines
6.5 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PLCManager
{
public class PlcOperation
{
/// <summary>
/// 通过key 写入
/// </summary>
/// <param name="equipkey"></param>
/// <param name="data"></param>
public void WriteByWord(string equipkey, object[] data)
{
PlcData.Instance.PlcWriteByEquipKey(equipkey, data);
}
public bool WriteByWord(int Block, object[] data)
{
return PlcData.Instance.PlcWriteByBlock(Block,0,data);
}
/// <summary>
/// 按位写入 method 0写入01写入1 2取反
/// </summary>
/// <param name="equipkey"></param>
/// <param name="bitlen">第几位</param>
/// <param name="method">0写入01写入1 2取反</param>
/// <param name="clear">是否将其他位清零</param>
/// <returns></returns>
public int writePLCbybit(string equipkey, int bitlen, int method,bool clear)
{
//ICSharpCode.Core.LoggingService.Debug("P获取中间上次值" + DateTime.Now.ToString("HH:mm:ss:fff"));
int oldvalue=0;
if (!clear)
{
try
{
oldvalue = GetLastValue(equipkey);
}
catch
{
oldvalue = 0;
}
}
int newvlaue = GetNewValue(oldvalue, bitlen, method);
object[] buff = new object[1];
buff[0] = newvlaue;
PlcData.Instance.PlcWriteByEquipKey(equipkey, buff);
return newvlaue;
}
/// <summary>
/// 获取当前值
/// </summary>
/// <param name="equipkey"></param>
/// <returns>当前值</returns>
public int GetLastValue(string equipkey)
{
PlcData.DataKeyValue abc = new PlcData.DataKeyValue(equipkey);
PlcData.DataKeyValue.Value abcd = abc.LastValuebeforeMath;
return abcd.ToInt();
}
/// <summary>
///
/// </summary>
/// <param name="oldvalue">原值</param>
/// <param name="bitlen">设置第几位</param>
/// <param name="m">0设bitlen为01设bitlen为1 2设bitlen取反</param>
/// <returns></returns>
private int GetNewValue(int oldvalue, int bitlen, int m)
{
switch (m)
{
case 0:
int i = 99999;
i= setBitValue0(oldvalue, bitlen);
return i;
case 1:
return setBitValue1(oldvalue, bitlen);
case 2:
return SetbitValueNegate(oldvalue, bitlen);
default:
return oldvalue;
}
}
/// <summary>
///int的将第bitlen位取反
/// </summary>
/// <param name="oldvalue"></param>
/// <param name="bitlen"></param>
/// <returns></returns>
private int SetbitValueNegate(int oldvalue, int bitlen)
{
return (oldvalue ^ (int)(1 << bitlen));
}
//将某位设为1
private int setBitValue1(int oldvalue, int bitlen)
{
return (oldvalue | (1 << bitlen));
}
//强某位设为0
private int setBitValue0(int oldvalue, int bitlen)
{
int a = (~(1 << bitlen));
return (a & oldvalue);
}
public Object[] floatToObject(float ff)
{
//正负符号默认为0
byte flag = 0;
if (ff < 0)
{
flag = 128;
ff = ff * -1;
}
byte[] floatByte = BitConverter.GetBytes(ff);
byte[] lowhByte = new byte[4];
lowhByte[0] = floatByte[0];
lowhByte[1] = floatByte[1];
lowhByte[2] = 0;
lowhByte[3] = 0;
byte[] highByte = new byte[4];
highByte[0] = floatByte[2];
highByte[1] = floatByte[3];
highByte[2] = 0;
highByte[3] = 0;
highByte[1] =(byte)( highByte[1] + flag);
UInt32 low = BitConverter.ToUInt32(lowhByte, 0);
int high = BitConverter.ToInt16(highByte, 0);
object[] buff = new object[2];
buff[0] = low;
buff[1] = high;
return buff;
}
private float convertTofloat(object[] buff)
{
//正负标志位
int flag = 1;
int b = Convert.ToInt32(buff[0]);
int a = Convert.ToInt32(buff[1]);
if (a >= 32768)
{
a = a - 32768;
flag = -1;
}
byte[] low = System.BitConverter.GetBytes(b);
byte[] high = System.BitConverter.GetBytes(a);
byte[] abc = new byte[4];
for (int i = 0; i < 2; i++)
{
abc[i] = low[i];
}
for (int i = 0; i < 2; i++)
{
abc[i + 2] = high[i];
}
float result= BitConverter.ToSingle(abc, 0);
return result * flag;
}
public int [] ReadBlockToInt(string Block,int len)
{
object [] buff=null;
PlcData.Instance.PlcReadbyBlock(Block, 0, len, out buff);
int[] dataValue = new int[buff.Length];
for (int i = 0; i < buff.Length; i++)
{
int ivalue = 0;
if (buff[i] != null && buff[i] != DBNull.Value
&& int.TryParse(buff[i].ToString(), out ivalue))
{
dataValue[i] = ivalue;
}
else
{
dataValue[i] = 0;
}
}
return dataValue;
}
public float[] ReadBlockToFloat(string Block,int len)
{
object [] buff=null;
PlcData.Instance.PlcReadbyBlock(Block, 0, len, out buff);
float[] floatdata = new float[buff.Length / 2];
for (int i = 0; i < buff.Length; i = i + 2)
{
object[] bu = new object[2];
bu[0] = buff[i];
bu[1] = buff[i + 1];
floatdata[i / 2] = convertTofloat(bu);
}
return floatdata;
}
}
}