2014-09-29 update 1

This commit is contained in:
parallelbgls@outlook.com
2014-09-29 10:57:40 +08:00
parent f173faf693
commit 20e142b701
2 changed files with 242 additions and 64 deletions

View File

@@ -85,6 +85,7 @@ namespace ModBus.Net
} }
catch catch
{ {
serialPort1.Close();
return null; return null;
} }
} }
@@ -115,6 +116,8 @@ namespace ModBus.Net
} }
public byte[] ReadMsg() public byte[] ReadMsg()
{
try
{ {
if (!serialPort1.IsOpen) if (!serialPort1.IsOpen)
{ {
@@ -128,7 +131,12 @@ namespace ModBus.Net
Array.Copy(data, 0, returndata, 0, i); Array.Copy(data, 0, returndata, 0, i);
serialPort1.Close(); serialPort1.Close();
return returndata; return returndata;
}
catch (Exception)
{
serialPort1.Close();
return null;
}
} }
#endregion #endregion

View File

@@ -95,7 +95,7 @@ namespace ModBus.Net
return BitConverter.GetBytes(value); return BitConverter.GetBytes(value);
} }
public byte GetByte(byte[] data, ref int pos) public virtual byte GetByte(byte[] data, ref int pos)
{ {
byte t = data[pos]; byte t = data[pos];
pos += 1; pos += 1;
@@ -139,25 +139,37 @@ namespace ModBus.Net
public virtual ulong GetULong(byte[] data, ref int pos) public virtual ulong GetULong(byte[] data, ref int pos)
{ {
ulong t = BitConverter.ToUInt64(data, 0); ulong t = BitConverter.ToUInt64(data, pos);
pos += 8; pos += 8;
return t; return t;
} }
public virtual float GetFloat(byte[] data, ref int pos) public virtual float GetFloat(byte[] data, ref int pos)
{ {
float t = BitConverter.ToSingle(data, 0); float t = BitConverter.ToSingle(data, pos);
pos += 4; pos += 4;
return t; return t;
} }
public virtual double GetDouble(byte[] data, ref int pos) public virtual double GetDouble(byte[] data, ref int pos)
{ {
double t = BitConverter.ToDouble(data, 0); double t = BitConverter.ToDouble(data, pos);
pos += 8; pos += 8;
return t; return t;
} }
public virtual bool[] GetBits(byte[] data, ref int pos)
{
bool[] t = new bool[8];
byte temp = data[pos];
for (int i = 0; i < 8; i++)
{
t[i] = temp%2 > 0;
temp /= 2;
}
return t;
}
public byte[] ObjectArrayToByteArray(object[] contents) public byte[] ObjectArrayToByteArray(object[] contents)
{ {
bool b = false; bool b = false;
@@ -170,7 +182,7 @@ namespace ModBus.Net
{ {
b = true; b = true;
IEnumerable<object> contentArray = IEnumerable<object> contentArray =
ArrayList.Adapter((Array)content).ToArray(typeof(object)).OfType<object>(); ArrayList.Adapter((Array) content).ToArray(typeof (object)).OfType<object>();
newContentsList.AddRange(contentArray); newContentsList.AddRange(contentArray);
} }
else else
@@ -182,9 +194,40 @@ namespace ModBus.Net
if (b) return ObjectArrayToByteArray(newContentsList.ToArray()); if (b) return ObjectArrayToByteArray(newContentsList.ToArray());
//把参数一个一个翻译为相对应的字节,然后拼成一个队列 //把参数一个一个翻译为相对应的字节,然后拼成一个队列
var translateTarget = new List<byte>(); var translateTarget = new List<byte>();
bool lastIsBool = false;
byte boolToByteTemp = 0;
int boolToByteCount = 0;
foreach (object content in contents) foreach (object content in contents)
{ {
string t = content.GetType().ToString(); string t = content.GetType().ToString();
if (t == "System.Boolean")
{
if (boolToByteCount >= 8)
{
translateTarget.Add(boolToByteTemp);
boolToByteCount = 0;
boolToByteTemp = 0;
}
lastIsBool = true;
if (_littleEndian)
{
boolToByteTemp += (byte) ((bool) content ? Math.Pow(2, boolToByteCount) : 0);
}
else
{
boolToByteTemp = (byte) (boolToByteTemp*2 + ((bool) content ? 1 : 0));
}
boolToByteCount++;
}
else
{
if (lastIsBool)
{
translateTarget.Add(boolToByteTemp);
boolToByteCount = 0;
boolToByteTemp = 0;
lastIsBool = false;
}
switch (t) switch (t)
{ {
case "System.Int16": case "System.Int16":
@@ -238,13 +281,105 @@ namespace ModBus.Net
} }
} }
} }
}
//最后把队列转换为数组 //最后把队列转换为数组
return translateTarget.ToArray(); return translateTarget.ToArray();
} }
public int GetBit(ushort number, int pos) public object[] ByteArrayToObjectArray(byte[] contents,
IEnumerable<KeyValuePair<Type, int>> translateTypeAndCount)
{ {
List<object> translation = new List<object>();
int count = 0;
foreach (var translateUnit in translateTypeAndCount)
{
for (int i = 0; i < translateUnit.Value; i++)
{
if (count >= contents.Length) break;
try
{
switch (translateUnit.Key.ToString())
{
case "System.Int16":
{
short value = Instance.GetShort(contents, ref count);
translation.Add(value);
break;
}
case "System.Int32":
{
int value = Instance.GetInt(contents, ref count);
translation.Add(value);
break;
}
case "System.Int64":
{
long value = Instance.GetLong(contents, ref count);
translation.Add(value);
break;
}
case "System.UInt16":
{
ushort value = Instance.GetUShort(contents, ref count);
translation.Add(value);
break;
}
case "System.UInt32":
{
uint value = Instance.GetUInt(contents, ref count);
translation.Add(value);
break;
}
case "System.UInt64":
{
ulong value = Instance.GetULong(contents, ref count);
translation.Add(value);
break;
}
case "System.Single":
{
float value = Instance.GetFloat(contents, ref count);
translation.Add(value);
break;
}
case "System.Double":
{
double value = Instance.GetDouble(contents, ref count);
translation.Add(value);
break;
}
case "System.Byte":
{
byte value = Instance.GetByte(contents, ref count);
translation.Add(value);
break;
}
case "System.Boolean":
{
bool[] value = Instance.GetBits(contents, ref count);
for (int j = 0; j < value.Length; j++)
{
translation.Add(value[j]);
}
break;
}
default:
{
throw new NotImplementedException("没有实现除整数以外的其它转换方式");
}
}
}
catch (Exception)
{
count = contents.Length;
}
}
}
return translation.ToArray();
}
public bool GetBit(ushort number, int pos)
{
if (pos < 0 && pos > 15) throw new IndexOutOfRangeException(); if (pos < 0 && pos > 15) throw new IndexOutOfRangeException();
int ans = number % 2; int ans = number % 2;
int i = 15; int i = 15;
@@ -254,7 +389,30 @@ namespace ModBus.Net
number /= 2; number /= 2;
i--; i--;
} }
return ans; return ans > 0;
}
public ushort SetBit(ushort number, int pos, bool setBit)
{
int creation = 0;
if (setBit)
{
for (int i = 0; i < 16; i++)
{
creation *= 2;
if (i == pos) creation++;
}
return (ushort) (number | creation);
}
else
{
for (int i = 0; i < 16; i++)
{
creation *= 2;
if (i != pos) creation++;
}
return (ushort) (number & creation);
}
} }
} }
@@ -364,6 +522,18 @@ namespace ModBus.Net
return t; return t;
} }
public override bool[] GetBits(byte[] data, ref int pos)
{
bool[] t = new bool[8];
byte temp = data[pos];
for (int i = 0; i < 8; i++)
{
t[8 - i] = temp%2 > 0;
temp /= 2;
}
return t;
}
private Byte[] Reverse(Byte[] data) private Byte[] Reverse(Byte[] data)
{ {
Array.Reverse(data); Array.Reverse(data);