|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace PLCManager
|
|
|
|
|
{
|
|
|
|
|
public class floatConvert
|
|
|
|
|
{
|
|
|
|
|
public Object[] floatToObject(float ff)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//正负符号,默认为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;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|