From 1d39343478d6da243264ae3454c38be1cf0b4732 Mon Sep 17 00:00:00 2001
From: yibo <361071264@qq.com>
Date: Mon, 16 Apr 2018 16:48:12 +0800
Subject: [PATCH] =?UTF-8?q?=E7=A7=B0=E8=B0=83=E6=95=B4=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
WinFormControl/UWeightControl.cs | 26 +++++---
WinFormControl/Utils/AoHaoSi3000DataFormat.cs | 52 +++++++++++++++
WinFormControl/Utils/DataFormatBase.cs | 3 +-
WinFormControl/Utils/IND560DataFormat.cs | 36 +++++++---
WinFormControl/Utils/Xk3124DataFormat.cs | 53 ++++++++++-----
WinFormControl/Utils/Xk3190A9DataFormat.cs | 65 ++++++++++++------
WinFormControl/Utils/Xk3190D10DataFormat.cs | 66 ++++++++++++-------
WinFormControl/WeightSettingFrom.cs | 2 +-
WinFormControl/WinFormControl.csproj | 1 +
9 files changed, 224 insertions(+), 80 deletions(-)
create mode 100644 WinFormControl/Utils/AoHaoSi3000DataFormat.cs
diff --git a/WinFormControl/UWeightControl.cs b/WinFormControl/UWeightControl.cs
index c97fdad..22aff58 100644
--- a/WinFormControl/UWeightControl.cs
+++ b/WinFormControl/UWeightControl.cs
@@ -37,7 +37,16 @@ namespace WinFormControl
}
[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)
{
@@ -124,6 +133,9 @@ namespace WinFormControl
case "IND560":
_dataFormat = new IND560DataFormat();
break;
+ case "AHS3000":
+ _dataFormat = new AoHaoSi3000DataFormat();
+ break;
case "Xk3124":
case "IND231":
_dataFormat = new Xk3124DataFormat();
@@ -170,10 +182,6 @@ namespace WinFormControl
Thread.Sleep(10);
}
char[] buffer = new char[availableCount];
- if (!weightPort.IsOpen)
- {
- continue;
- }
weightPort.Read(buffer, 0, availableCount);
foreach (var c in buffer)
{
@@ -182,7 +190,7 @@ namespace WinFormControl
_dataStrBuilder.Clear();
_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);
bool isStatic;
@@ -200,7 +208,7 @@ namespace WinFormControl
if (str != "0")
{
if (ReceivedValue != null)
- ReceivedValue(v);
+ ReceivedValue(v - (config.Discont ?? 0));
}
}));
}
@@ -222,7 +230,7 @@ namespace WinFormControl
if (str != "0")
{
if (ReceivedValue != null)
- ReceivedValue(num);
+ ReceivedValue(num - (config.Discont ?? 0));
}
}));
}
@@ -230,7 +238,7 @@ namespace WinFormControl
}
_dataStrBuilder.Clear();
}
- else if (_dataStrBuilder.Length != 0)
+ else
{
_dataStrBuilder.Append(c);
}
diff --git a/WinFormControl/Utils/AoHaoSi3000DataFormat.cs b/WinFormControl/Utils/AoHaoSi3000DataFormat.cs
new file mode 100644
index 0000000..dcd3c90
--- /dev/null
+++ b/WinFormControl/Utils/AoHaoSi3000DataFormat.cs
@@ -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;
+ }
+ }
+}
diff --git a/WinFormControl/Utils/DataFormatBase.cs b/WinFormControl/Utils/DataFormatBase.cs
index bcaf82b..3e8ad90 100644
--- a/WinFormControl/Utils/DataFormatBase.cs
+++ b/WinFormControl/Utils/DataFormatBase.cs
@@ -10,7 +10,7 @@ namespace WinFormControl
{
char Beginchar { 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, out string subStr);
}
@@ -32,6 +32,7 @@ namespace WinFormControl
get;
}
+ public abstract short Bufsize { get; }
public const short BUFSIZE = 36;
// 根据开始字符找出 一个完整的数据帧
diff --git a/WinFormControl/Utils/IND560DataFormat.cs b/WinFormControl/Utils/IND560DataFormat.cs
index 412544e..0d18af3 100644
--- a/WinFormControl/Utils/IND560DataFormat.cs
+++ b/WinFormControl/Utils/IND560DataFormat.cs
@@ -1,32 +1,50 @@
+using System;
+using System.Linq;
namespace WinFormControl
{
- internal class IND560DataFormat : DataFormatBase {
- public override int DataLength {
+ internal class IND560DataFormat : DataFormatBase
+ {
+ public override int DataLength
+ {
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)
{
isStatic = false;
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;
- 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();
+ if (weight.Any(x => x == ' '))
+ {
+ var arr = weight.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
+ if (arr.Length > 1)
+ weight = arr[1];
+ }
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 = "";
isStatic = false;
subStr = "";
diff --git a/WinFormControl/Utils/Xk3124DataFormat.cs b/WinFormControl/Utils/Xk3124DataFormat.cs
index bfbb805..3096847 100644
--- a/WinFormControl/Utils/Xk3124DataFormat.cs
+++ b/WinFormControl/Utils/Xk3124DataFormat.cs
@@ -4,7 +4,8 @@ namespace WinFormControl
{
internal class Xk3124DataFormat : DataFormatBase
{
- public override int DataLength {
+ public override int DataLength
+ {
get { return 17; }
}
@@ -12,13 +13,19 @@ namespace WinFormControl
{
get { return (char)0x02; }
}
-
+
public override char Endchar
{
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();
char swa = buf[1]; // 状态字A
@@ -31,21 +38,25 @@ namespace WinFormControl
char[] num = new char[6];
- for (int i = 0; i < 6; i++) {
+ for (int i = 0; i < 6; i++)
+ {
num[i] = buf[i + 4];
}
- if (dotIndex < 0) { // 不考虑精确到十,百位
+ if (dotIndex < 0)
+ { // 不考虑精确到十,百位
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('.');
- for (int i = 0; i < dotIndex; i++) {
+ for (int i = 0; i < dotIndex; i++)
+ {
str.Append(num[6 - dotIndex + i]);
}
@@ -53,33 +64,40 @@ namespace WinFormControl
string result = str.ToString().Trim();
// 取消前面多余的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);
break;
}
}
- if (result.IndexOf('.') == 0) {
+ if (result.IndexOf('.') == 0)
+ {
result = result.Insert(0, "0");
}
- if (result.IndexOf('.') == result.Length - 1) {
+ if (result.IndexOf('.') == result.Length - 1)
+ {
result = result.Remove(result.IndexOf('.'), 1);
}
- if (!isPositive) { // 负数
+ if (!isPositive)
+ { // 负数
result = result.Insert(0, "-");
}
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;
- weight = FindDataFrame(buf, DataLength );
+ weight = FindDataFrame(buf, DataLength);
- if (string.IsNullOrEmpty(weight)) {
+ if (string.IsNullOrEmpty(weight))
+ {
return false;
}
@@ -88,7 +106,8 @@ namespace WinFormControl
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 = "";
isStatic = false;
subStr = "";
diff --git a/WinFormControl/Utils/Xk3190A9DataFormat.cs b/WinFormControl/Utils/Xk3190A9DataFormat.cs
index bc959e8..e33a1cf 100644
--- a/WinFormControl/Utils/Xk3190A9DataFormat.cs
+++ b/WinFormControl/Utils/Xk3190A9DataFormat.cs
@@ -3,19 +3,28 @@ namespace WinFormControl
internal class Xk3190A9DataFormat : DataFormatBase
{
- public override int DataLength {
+ public override int DataLength
+ {
get { return 12; }
}
- public override char Beginchar {
+ public override char Beginchar
+ {
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;
// Сλ0-4
int dot = (short)(0x0F & buf[8]);
@@ -26,18 +35,21 @@ namespace WinFormControl
isStatic = true; // Ĭ Ϊ ȶ
// buffer[1] λ
- if (buf[1] == '-') {
+ if (buf[1] == '-')
+ {
weight = weight.Insert(0, "-");
}
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;
- weight = FindDataFrame(buf, DataLength );
+ weight = FindDataFrame(buf, DataLength);
- if (string.IsNullOrEmpty(weight)) {
+ if (string.IsNullOrEmpty(weight))
+ {
return false;
}
@@ -46,36 +58,44 @@ namespace WinFormControl
return true;
}
- private static string InsertDot(string weight, int dotBits) {
+ private static string InsertDot(string weight, int dotBits)
+ {
string str = weight.TrimStart(new[] {
'0'
});
str = str.Trim();
- if (dotBits > 0) {
+ if (dotBits > 0)
+ {
//str = str.Insert(str.Length - dotBits, ".");
- if (string.IsNullOrEmpty(str)) {
+ if (string.IsNullOrEmpty(str))
+ {
str = "0";
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");
}
- } else
+ }
+ else
str = str.Insert(str.Length - dotBits, ".");
}
- if (str.IndexOf(".") == 0) {
+ if (str.IndexOf(".") == 0)
+ {
str = str.Insert(0, "0");
}
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;
- weight = FindDataFrame(buf, DataLength , out subStr);
+ weight = FindDataFrame(buf, DataLength, out subStr);
- if (string.IsNullOrEmpty(weight)) {
+ if (string.IsNullOrEmpty(weight))
+ {
return false;
}
@@ -85,19 +105,22 @@ namespace WinFormControl
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;
subStr = "";
int index = buf.IndexOf(Beginchar); // ҿʼַ
- if (index < 0 || (index + fSize) > bufSize) {
+ if (index < 0 || (index + fSize) > bufSize)
+ {
return string.Empty;
}
string data = buf.Substring(index, fSize);
subStr = buf.Substring(index + fSize);
// ַ
- if (data[fSize - 1] != Endchar) {
+ if (data[fSize - 1] != Endchar)
+ {
return string.Empty;
}
diff --git a/WinFormControl/Utils/Xk3190D10DataFormat.cs b/WinFormControl/Utils/Xk3190D10DataFormat.cs
index bdc11f0..9202c0b 100644
--- a/WinFormControl/Utils/Xk3190D10DataFormat.cs
+++ b/WinFormControl/Utils/Xk3190D10DataFormat.cs
@@ -3,18 +3,26 @@ namespace WinFormControl
internal class Xk3190D10DataFormat : DataFormatBase
{
- public override int DataLength {
+ public override int DataLength
+ {
get { return 12; }
}
- public override char Beginchar {
+ public override char Beginchar
+ {
get { return (char)0x02; }
}
- public override char Endchar {
+ public override char Endchar
+ {
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;
// Сλ0-4
int dot = (short)(0x0F & buf[8]);
@@ -25,19 +33,24 @@ namespace WinFormControl
isStatic = true; // Ĭ Ϊ ȶ
// buffer[1] λ
- if (buf[1] == '-') {
+ if (buf[1] == '-')
+ {
weight = weight.Insert(0, "-");
- } else {
-
+ }
+ else
+ {
+
}
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;
- weight = FindDataFrame(buf, DataLength);
+ weight = FindDataFrame(buf, DataLength);
- if (string.IsNullOrEmpty(weight)) {
+ if (string.IsNullOrEmpty(weight))
+ {
return false;
}
@@ -46,51 +59,60 @@ namespace WinFormControl
return true;
}
- private static string InsertDot(string weight, int dotBits) {
+ private static string InsertDot(string weight, int dotBits)
+ {
string str = weight.TrimStart(new[] {
'0'
});
str = str.Trim();
- if (dotBits > 0) {
+ if (dotBits > 0)
+ {
//str = str.Insert(str.Length - dotBits, ".");
- if (string.IsNullOrEmpty(str)) {
+ if (string.IsNullOrEmpty(str))
+ {
str = "0";
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");
}
- } else
+ }
+ else
str = str.Insert(str.Length - dotBits, ".");
}
- if (str.IndexOf(".") == 0) {
+ if (str.IndexOf(".") == 0)
+ {
str = str.Insert(0, "0");
}
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;
- weight = FindDataFrame(buf, DataLength );
+ weight = FindDataFrame(buf, DataLength);
subStr = "";
- if (string.IsNullOrEmpty(weight)) {
+ if (string.IsNullOrEmpty(weight))
+ {
return false;
}
weight = ParseData(weight, out isStatic);
-
+
return true;
}
-
+
#region 1.3 ֤intż
///
/// 1.3 ֤intż
///
///
///
- public bool isJO(int num) {
+ public bool isJO(int num)
+ {
int a = num % 2;
if (a == 0)
return true;
diff --git a/WinFormControl/WeightSettingFrom.cs b/WinFormControl/WeightSettingFrom.cs
index 7126c54..c17b196 100644
--- a/WinFormControl/WeightSettingFrom.cs
+++ b/WinFormControl/WeightSettingFrom.cs
@@ -12,7 +12,7 @@ namespace WinFormControl
{
public partial class WeightSettingFrom : Form
{
- List weight = new List { "IND560", "Xk3124", "Xk3190A9", "Xk3190D10", "IND231" };
+ List weight = new List { "IND560", "Xk3124", "Xk3190A9", "Xk3190D10", "IND231","AHS3000" };
List com = new List { "COM1", "COM2", "COM3", "COM4", "COM5" };
List rate = new List { "1200", "2400", "4800", "7200", "9600" };
List bit = new List { "5", "6", "7", "8" };
diff --git a/WinFormControl/WinFormControl.csproj b/WinFormControl/WinFormControl.csproj
index d1ef811..20b07d5 100644
--- a/WinFormControl/WinFormControl.csproj
+++ b/WinFormControl/WinFormControl.csproj
@@ -57,6 +57,7 @@
True
Resources.resx
+
Component