Browse Source

称调整。

master
yibo 7 years ago
parent
commit
1d39343478
9 changed files with 224 additions and 80 deletions
  1. +17
    -9
      WinFormControl/UWeightControl.cs
  2. +52
    -0
      WinFormControl/Utils/AoHaoSi3000DataFormat.cs
  3. +2
    -1
      WinFormControl/Utils/DataFormatBase.cs
  4. +27
    -9
      WinFormControl/Utils/IND560DataFormat.cs
  5. +36
    -17
      WinFormControl/Utils/Xk3124DataFormat.cs
  6. +44
    -21
      WinFormControl/Utils/Xk3190A9DataFormat.cs
  7. +44
    -22
      WinFormControl/Utils/Xk3190D10DataFormat.cs
  8. +1
    -1
      WinFormControl/WeightSettingFrom.cs
  9. +1
    -0
      WinFormControl/WinFormControl.csproj

+ 17
- 9
WinFormControl/UWeightControl.cs View File

@ -37,7 +37,16 @@ namespace WinFormControl
} }
[Browsable(false)] [Browsable(false)]
public decimal Weight { get { return decimal.Parse(lblChengZhong.Text); } }
public decimal Weight
{
get
{
var v = decimal.Parse(lblChengZhong.Text);
if (config != null)
v -= (config.Discont ?? 0);
return v;
}
}
protected override void OnLoad(EventArgs e) protected override void OnLoad(EventArgs e)
{ {
@ -124,6 +133,9 @@ namespace WinFormControl
case "IND560": case "IND560":
_dataFormat = new IND560DataFormat(); _dataFormat = new IND560DataFormat();
break; break;
case "AHS3000":
_dataFormat = new AoHaoSi3000DataFormat();
break;
case "Xk3124": case "Xk3124":
case "IND231": case "IND231":
_dataFormat = new Xk3124DataFormat(); _dataFormat = new Xk3124DataFormat();
@ -170,10 +182,6 @@ namespace WinFormControl
Thread.Sleep(10); Thread.Sleep(10);
} }
char[] buffer = new char[availableCount]; char[] buffer = new char[availableCount];
if (!weightPort.IsOpen)
{
continue;
}
weightPort.Read(buffer, 0, availableCount); weightPort.Read(buffer, 0, availableCount);
foreach (var c in buffer) foreach (var c in buffer)
{ {
@ -182,7 +190,7 @@ namespace WinFormControl
_dataStrBuilder.Clear(); _dataStrBuilder.Clear();
_dataStrBuilder.Append(c); _dataStrBuilder.Append(c);
} }
else if (c == _dataFormat.Endchar && _dataStrBuilder.Length == _dataFormat.DataLength - 1)
else if (c == _dataFormat.Endchar || _dataStrBuilder.Length > _dataFormat.Bufsize)
{ {
_dataStrBuilder.Append(c); _dataStrBuilder.Append(c);
bool isStatic; bool isStatic;
@ -200,7 +208,7 @@ namespace WinFormControl
if (str != "0") if (str != "0")
{ {
if (ReceivedValue != null) if (ReceivedValue != null)
ReceivedValue(v);
ReceivedValue(v - (config.Discont ?? 0));
} }
})); }));
} }
@ -222,7 +230,7 @@ namespace WinFormControl
if (str != "0") if (str != "0")
{ {
if (ReceivedValue != null) if (ReceivedValue != null)
ReceivedValue(num);
ReceivedValue(num - (config.Discont ?? 0));
} }
})); }));
} }
@ -230,7 +238,7 @@ namespace WinFormControl
} }
_dataStrBuilder.Clear(); _dataStrBuilder.Clear();
} }
else if (_dataStrBuilder.Length != 0)
else
{ {
_dataStrBuilder.Append(c); _dataStrBuilder.Append(c);
} }


+ 52
- 0
WinFormControl/Utils/AoHaoSi3000DataFormat.cs View File

@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormControl
{
internal class AoHaoSi3000DataFormat : DataFormatBase
{
public override int DataLength
{
get { return 10; }
}
public override char Beginchar
{
get { return (char)0x80; }//这种数据没有固定的开始标志
}
public override char Endchar
{
get { return (char)0x67; }
}
public override short Bufsize
{
get { return 36; }
}
public override string ParseData(string buf, out bool isStatic)
{
isStatic = false;
return string.Empty;
}
public override bool ParseAscii(string buf, out string weight, out bool isStatic)
{
isStatic = true;
weight = buf.Replace("kg", "").Replace((char)0x0D, (char)0x20).Replace((char)0x0A, (char)0x20).Replace((char)0x3F, (char)0x20);
weight = weight.Trim();
return true;
}
public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr)
{
weight = "";
isStatic = false;
subStr = "";
return false;
}
}
}

+ 2
- 1
WinFormControl/Utils/DataFormatBase.cs View File

@ -10,7 +10,7 @@ namespace WinFormControl
{ {
char Beginchar { get; } char Beginchar { get; }
char Endchar { get; } char Endchar { get; }
int DataLength { get; }
short Bufsize { get; }
bool ParseAscii(string buf, out string weight, out bool isStatic); bool ParseAscii(string buf, out string weight, out bool isStatic);
bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr); bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr);
} }
@ -32,6 +32,7 @@ namespace WinFormControl
get; get;
} }
public abstract short Bufsize { get; }
public const short BUFSIZE = 36; public const short BUFSIZE = 36;
// 根据开始字符找出 一个完整的数据帧 // 根据开始字符找出 一个完整的数据帧


+ 27
- 9
WinFormControl/Utils/IND560DataFormat.cs View File

@ -1,32 +1,50 @@
using System;
using System.Linq;
namespace WinFormControl namespace WinFormControl
{ {
internal class IND560DataFormat : DataFormatBase {
public override int DataLength {
internal class IND560DataFormat : DataFormatBase
{
public override int DataLength
{
get { return 10; } get { return 10; }
} }
public override char Beginchar {
get { return (char)0x0A; }//这种数据没有固定的开始标志
public override char Beginchar
{
get { return (char)0x80; }//这种数据没有固定的开始标志
} }
public override char Endchar {
get { return (char)0x0D; }
public override char Endchar
{
get { return (char)0x67; }
} }
public override short Bufsize
{
get { return 36; }
}
public override string ParseData(string buf, out bool isStatic) public override string ParseData(string buf, out bool isStatic)
{ {
isStatic = false; isStatic = false;
return string.Empty; return string.Empty;
} }
public override bool ParseAscii(string buf, out string weight, out bool isStatic) {
public override bool ParseAscii(string buf, out string weight, out bool isStatic)
{
isStatic = true; isStatic = true;
weight = buf.Replace("kg", "").Replace((char)0x0D, (char)0x20).Replace((char)0x0A, (char)0x20);
weight = buf.Replace("kg", "").Replace((char)0x0D, (char)0x20).Replace((char)0x0A, (char)0x20).Replace((char)0x3F, (char)0x20);
weight = weight.Trim(); weight = weight.Trim();
if (weight.Any(x => x == ' '))
{
var arr = weight.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length > 1)
weight = arr[1];
}
return true; return true;
} }
public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr) {
public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr)
{
weight = ""; weight = "";
isStatic = false; isStatic = false;
subStr = ""; subStr = "";


+ 36
- 17
WinFormControl/Utils/Xk3124DataFormat.cs View File

@ -4,7 +4,8 @@ namespace WinFormControl
{ {
internal class Xk3124DataFormat : DataFormatBase internal class Xk3124DataFormat : DataFormatBase
{ {
public override int DataLength {
public override int DataLength
{
get { return 17; } get { return 17; }
} }
@ -12,13 +13,19 @@ namespace WinFormControl
{ {
get { return (char)0x02; } get { return (char)0x02; }
} }
public override char Endchar public override char Endchar
{ {
get { return (char)0x0D; ; } get { return (char)0x0D; ; }
} }
public override string ParseData(string buf, out bool isStatic) {
public override short Bufsize
{
get { return 36; }
}
public override string ParseData(string buf, out bool isStatic)
{
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
char swa = buf[1]; // 状态字A char swa = buf[1]; // 状态字A
@ -31,21 +38,25 @@ namespace WinFormControl
char[] num = new char[6]; char[] num = new char[6];
for (int i = 0; i < 6; i++) {
for (int i = 0; i < 6; i++)
{
num[i] = buf[i + 4]; num[i] = buf[i + 4];
} }
if (dotIndex < 0) { // 不考虑精确到十,百位
if (dotIndex < 0)
{ // 不考虑精确到十,百位
return str.ToString(); return str.ToString();
} }
for (int i = 0; i < 6 - dotIndex; i++) {
for (int i = 0; i < 6 - dotIndex; i++)
{
str.Append(num[i]); str.Append(num[i]);
} }
str.Append('.'); str.Append('.');
for (int i = 0; i < dotIndex; i++) {
for (int i = 0; i < dotIndex; i++)
{
str.Append(num[6 - dotIndex + i]); str.Append(num[6 - dotIndex + i]);
} }
@ -53,33 +64,40 @@ namespace WinFormControl
string result = str.ToString().Trim(); string result = str.ToString().Trim();
// 取消前面多余的0 // 取消前面多余的0
for (int i = 0; i < result.Length; i++) {
if (result[i] != '0') {
for (int i = 0; i < result.Length; i++)
{
if (result[i] != '0')
{
result = result.Substring(i, result.Length - i); result = result.Substring(i, result.Length - i);
break; break;
} }
} }
if (result.IndexOf('.') == 0) {
if (result.IndexOf('.') == 0)
{
result = result.Insert(0, "0"); result = result.Insert(0, "0");
} }
if (result.IndexOf('.') == result.Length - 1) {
if (result.IndexOf('.') == result.Length - 1)
{
result = result.Remove(result.IndexOf('.'), 1); result = result.Remove(result.IndexOf('.'), 1);
} }
if (!isPositive) { // 负数
if (!isPositive)
{ // 负数
result = result.Insert(0, "-"); result = result.Insert(0, "-");
} }
return result; return result;
} }
public override bool ParseAscii(string buf, out string weight, out bool isStatic) {
public override bool ParseAscii(string buf, out string weight, out bool isStatic)
{
isStatic = false; isStatic = false;
weight = FindDataFrame(buf, DataLength );
weight = FindDataFrame(buf, DataLength);
if (string.IsNullOrEmpty(weight)) {
if (string.IsNullOrEmpty(weight))
{
return false; return false;
} }
@ -88,7 +106,8 @@ namespace WinFormControl
return true; return true;
} }
public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr) {
public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr)
{
weight = ""; weight = "";
isStatic = false; isStatic = false;
subStr = ""; subStr = "";


+ 44
- 21
WinFormControl/Utils/Xk3190A9DataFormat.cs View File

@ -3,19 +3,28 @@ namespace WinFormControl
internal class Xk3190A9DataFormat : DataFormatBase internal class Xk3190A9DataFormat : DataFormatBase
{ {
public override int DataLength {
public override int DataLength
{
get { return 12; } get { return 12; }
} }
public override char Beginchar {
public override char Beginchar
{
get { return (char)0x02; } get { return (char)0x02; }
} }
public override char Endchar {
get { return (char)0x03;}
public override char Endchar
{
get { return (char)0x03; }
} }
public override string ParseData(string buf, out bool isStatic) {
public override short Bufsize
{
get { return 36; }
}
public override string ParseData(string buf, out bool isStatic)
{
string weight; string weight;
// 小数位数0-4 // 小数位数0-4
int dot = (short)(0x0F & buf[8]); int dot = (short)(0x0F & buf[8]);
@ -26,18 +35,21 @@ namespace WinFormControl
isStatic = true; // 默认 为 稳定 isStatic = true; // 默认 为 稳定
// buffer[1] 符号位 // buffer[1] 符号位
if (buf[1] == '-') {
if (buf[1] == '-')
{
weight = weight.Insert(0, "-"); weight = weight.Insert(0, "-");
} }
return weight; return weight;
} }
public override bool ParseAscii(string buf, out string weight, out bool isStatic) {
public override bool ParseAscii(string buf, out string weight, out bool isStatic)
{
isStatic = false; isStatic = false;
weight = FindDataFrame(buf, DataLength );
weight = FindDataFrame(buf, DataLength);
if (string.IsNullOrEmpty(weight)) {
if (string.IsNullOrEmpty(weight))
{
return false; return false;
} }
@ -46,36 +58,44 @@ namespace WinFormControl
return true; return true;
} }
private static string InsertDot(string weight, int dotBits) {
private static string InsertDot(string weight, int dotBits)
{
string str = weight.TrimStart(new[] { string str = weight.TrimStart(new[] {
'0' '0'
}); });
str = str.Trim(); str = str.Trim();
if (dotBits > 0) {
if (dotBits > 0)
{
//str = str.Insert(str.Length - dotBits, "."); //str = str.Insert(str.Length - dotBits, ".");
if (string.IsNullOrEmpty(str)) {
if (string.IsNullOrEmpty(str))
{
str = "0"; str = "0";
str = str.Insert(str.Length, "."); str = str.Insert(str.Length, ".");
for (int i = 0; i < dotBits; i++) {
for (int i = 0; i < dotBits; i++)
{
str = str.Insert(str.Length, "0"); str = str.Insert(str.Length, "0");
} }
} else
}
else
str = str.Insert(str.Length - dotBits, "."); str = str.Insert(str.Length - dotBits, ".");
} }
if (str.IndexOf(".") == 0) {
if (str.IndexOf(".") == 0)
{
str = str.Insert(0, "0"); str = str.Insert(0, "0");
} }
return str; return str;
} }
public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr) {
public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr)
{
isStatic = false; isStatic = false;
weight = FindDataFrame(buf, DataLength , out subStr);
weight = FindDataFrame(buf, DataLength, out subStr);
if (string.IsNullOrEmpty(weight)) {
if (string.IsNullOrEmpty(weight))
{
return false; return false;
} }
@ -85,19 +105,22 @@ namespace WinFormControl
return true; return true;
} }
// 根据开始字符找出 一个完整的数据帧 // 根据开始字符找出 一个完整的数据帧
public string FindDataFrame(string buf, int fSize , out string subStr) {
public string FindDataFrame(string buf, int fSize, out string subStr)
{
var bufSize = buf.Length; var bufSize = buf.Length;
subStr = ""; subStr = "";
int index = buf.IndexOf(Beginchar); // 查找开始字符的索引 int index = buf.IndexOf(Beginchar); // 查找开始字符的索引
if (index < 0 || (index + fSize) > bufSize) {
if (index < 0 || (index + fSize) > bufSize)
{
return string.Empty; return string.Empty;
} }
string data = buf.Substring(index, fSize); string data = buf.Substring(index, fSize);
subStr = buf.Substring(index + fSize); subStr = buf.Substring(index + fSize);
// 检查结束字符 // 检查结束字符
if (data[fSize - 1] != Endchar) {
if (data[fSize - 1] != Endchar)
{
return string.Empty; return string.Empty;
} }


+ 44
- 22
WinFormControl/Utils/Xk3190D10DataFormat.cs View File

@ -3,18 +3,26 @@ namespace WinFormControl
internal class Xk3190D10DataFormat : DataFormatBase internal class Xk3190D10DataFormat : DataFormatBase
{ {
public override int DataLength {
public override int DataLength
{
get { return 12; } get { return 12; }
} }
public override char Beginchar {
public override char Beginchar
{
get { return (char)0x02; } get { return (char)0x02; }
} }
public override char Endchar {
public override char Endchar
{
get { return (char)0x0D; ; } get { return (char)0x0D; ; }
} }
public override short Bufsize
{
get { return 24; }
}
public override string ParseData(string buf, out bool isStatic) {
public override string ParseData(string buf, out bool isStatic)
{
string weight; string weight;
// 小数位数0-4 // 小数位数0-4
int dot = (short)(0x0F & buf[8]); int dot = (short)(0x0F & buf[8]);
@ -25,19 +33,24 @@ namespace WinFormControl
isStatic = true; // 默认 为 稳定 isStatic = true; // 默认 为 稳定
// buffer[1] 符号位 // buffer[1] 符号位
if (buf[1] == '-') {
if (buf[1] == '-')
{
weight = weight.Insert(0, "-"); weight = weight.Insert(0, "-");
} else {
}
else
{
} }
return weight; return weight;
} }
public override bool ParseAscii(string buf, out string weight, out bool isStatic) {
public override bool ParseAscii(string buf, out string weight, out bool isStatic)
{
isStatic = false; isStatic = false;
weight = FindDataFrame(buf, DataLength);
weight = FindDataFrame(buf, DataLength);
if (string.IsNullOrEmpty(weight)) {
if (string.IsNullOrEmpty(weight))
{
return false; return false;
} }
@ -46,51 +59,60 @@ namespace WinFormControl
return true; return true;
} }
private static string InsertDot(string weight, int dotBits) {
private static string InsertDot(string weight, int dotBits)
{
string str = weight.TrimStart(new[] { string str = weight.TrimStart(new[] {
'0' '0'
}); });
str = str.Trim(); str = str.Trim();
if (dotBits > 0) {
if (dotBits > 0)
{
//str = str.Insert(str.Length - dotBits, "."); //str = str.Insert(str.Length - dotBits, ".");
if (string.IsNullOrEmpty(str)) {
if (string.IsNullOrEmpty(str))
{
str = "0"; str = "0";
str = str.Insert(str.Length, "."); str = str.Insert(str.Length, ".");
for (int i = 0; i < dotBits; i++) {
for (int i = 0; i < dotBits; i++)
{
str = str.Insert(str.Length, "0"); str = str.Insert(str.Length, "0");
} }
} else
}
else
str = str.Insert(str.Length - dotBits, "."); str = str.Insert(str.Length - dotBits, ".");
} }
if (str.IndexOf(".") == 0) {
if (str.IndexOf(".") == 0)
{
str = str.Insert(0, "0"); str = str.Insert(0, "0");
} }
return str; return str;
} }
public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr) {
public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr)
{
isStatic = false; isStatic = false;
weight = FindDataFrame(buf, DataLength );
weight = FindDataFrame(buf, DataLength);
subStr = ""; subStr = "";
if (string.IsNullOrEmpty(weight)) {
if (string.IsNullOrEmpty(weight))
{
return false; return false;
} }
weight = ParseData(weight, out isStatic); weight = ParseData(weight, out isStatic);
return true; return true;
} }
#region 1.3 验证int奇数偶数 #region 1.3 验证int奇数偶数
/// <summary> /// <summary>
/// 1.3 验证int奇数偶数 /// 1.3 验证int奇数偶数
/// </summary> /// </summary>
/// <param name="num"></param> /// <param name="num"></param>
/// <returns></returns> /// <returns></returns>
public bool isJO(int num) {
public bool isJO(int num)
{
int a = num % 2; int a = num % 2;
if (a == 0) if (a == 0)
return true; return true;


+ 1
- 1
WinFormControl/WeightSettingFrom.cs View File

@ -12,7 +12,7 @@ namespace WinFormControl
{ {
public partial class WeightSettingFrom : Form public partial class WeightSettingFrom : Form
{ {
List<string> weight = new List<string> { "IND560", "Xk3124", "Xk3190A9", "Xk3190D10", "IND231" };
List<string> weight = new List<string> { "IND560", "Xk3124", "Xk3190A9", "Xk3190D10", "IND231","AHS3000" };
List<string> com = new List<string> { "COM1", "COM2", "COM3", "COM4", "COM5" }; List<string> com = new List<string> { "COM1", "COM2", "COM3", "COM4", "COM5" };
List<string> rate = new List<string> { "1200", "2400", "4800", "7200", "9600" }; List<string> rate = new List<string> { "1200", "2400", "4800", "7200", "9600" };
List<string> bit = new List<string> { "5", "6", "7", "8" }; List<string> bit = new List<string> { "5", "6", "7", "8" };


+ 1
- 0
WinFormControl/WinFormControl.csproj View File

@ -57,6 +57,7 @@
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
</Compile> </Compile>
<Compile Include="Utils\AoHaoSi3000DataFormat.cs" />
<Compile Include="UTimerLabel.cs"> <Compile Include="UTimerLabel.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>


Loading…
Cancel
Save