| @ -0,0 +1,42 @@ | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| using System.Windows.Forms; | |||||
| namespace BWP.WinFormControl | |||||
| { | |||||
| public class UDataGridView : DataGridView | |||||
| { | |||||
| public UDataGridView() | |||||
| { | |||||
| this.AllowUserToAddRows = false; | |||||
| this.AllowUserToDeleteRows = false; | |||||
| this.AllowUserToResizeColumns = false; | |||||
| this.AllowUserToResizeRows = false; | |||||
| this.BackgroundColor = System.Drawing.Color.White; | |||||
| this.ColumnHeadersHeight = 24; | |||||
| this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing; | |||||
| this.MultiSelect = false; | |||||
| this.RowHeadersVisible = false; | |||||
| this.RowTemplate.Height = 23; | |||||
| this.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; | |||||
| this.AlternatingRowsDefaultCellStyle = new DataGridViewCellStyle() { BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(235)))), ((int)(((byte)(235)))), ((int)(((byte)(235))))) }; | |||||
| this.ColumnHeadersDefaultCellStyle = new DataGridViewCellStyle() | |||||
| { | |||||
| Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft, | |||||
| BackColor = System.Drawing.SystemColors.Control, | |||||
| Font = new System.Drawing.Font("宋体", 12F), | |||||
| ForeColor = System.Drawing.SystemColors.WindowText, | |||||
| SelectionBackColor = System.Drawing.SystemColors.Highlight, | |||||
| SelectionForeColor = System.Drawing.SystemColors.HighlightText, | |||||
| WrapMode = System.Windows.Forms.DataGridViewTriState.True, | |||||
| }; | |||||
| this.RowsDefaultCellStyle = new DataGridViewCellStyle() { SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(66)))), ((int)(((byte)(163)))), ((int)(((byte)(218))))) }; | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,57 @@ | |||||
| namespace BWP.WinFormControl.WeightDataFormat | |||||
| { | |||||
| interface IDataFormat { | |||||
| char Beginchar { get; } | |||||
| char Endchar { 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); | |||||
| } | |||||
| public abstract class DataFormatBase : IDataFormat { | |||||
| public abstract int DataLength { | |||||
| get; | |||||
| } | |||||
| public abstract char Beginchar { | |||||
| get; | |||||
| } | |||||
| public abstract char Endchar { | |||||
| get; | |||||
| } | |||||
| public abstract short Bufsize { get; } | |||||
| public const short BUFSIZE = 36; | |||||
| // 根据开始字符找出 一个完整的数据帧 | |||||
| public string FindDataFrame(string buf, int fSize) { | |||||
| var bufSize = buf.Length; | |||||
| if (fSize > bufSize) { | |||||
| return string.Empty; | |||||
| } | |||||
| int index = buf.IndexOf(Beginchar); // 查找开始字符的索引 | |||||
| if (index < 0 || (index + fSize) > bufSize) { | |||||
| return string.Empty; | |||||
| } | |||||
| string data = buf.Substring(index, fSize); | |||||
| // 检查结束字符 | |||||
| if (data[fSize - 1] != Endchar) { | |||||
| return string.Empty; | |||||
| } | |||||
| return data; | |||||
| } | |||||
| public abstract string ParseData(string buf, out bool isStatic); | |||||
| public abstract bool ParseAscii(string buf, out string weight, out bool isStatic); | |||||
| public abstract bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,40 @@ | |||||
| namespace BWP.WinFormControl.WeightDataFormat | |||||
| { | |||||
| public class IND560DataFormat : DataFormatBase { | |||||
| public override int DataLength { | |||||
| get { return 10; } | |||||
| } | |||||
| public override char Beginchar { | |||||
| get { return (char)0x80; }//这种数据没有固定的开始标志 | |||||
| } | |||||
| public override char Endchar { | |||||
| get { return (char)0x0A; ; } | |||||
| } | |||||
| public override short Bufsize { | |||||
| get { return 10; } | |||||
| } | |||||
| 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); | |||||
| 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; | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,104 @@ | |||||
| using System.Text; | |||||
| namespace BWP.WinFormControl.WeightDataFormat | |||||
| { | |||||
| public class Xk3124DataFormat : DataFormatBase { | |||||
| public override int DataLength { | |||||
| get { return 17; } | |||||
| } | |||||
| public override char Beginchar | |||||
| { | |||||
| get { return (char)0x02; } | |||||
| } | |||||
| public override char Endchar | |||||
| { | |||||
| get { return (char)0x0D; ; } | |||||
| } | |||||
| 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 | |||||
| char swb = buf[2]; // 状态字B | |||||
| int dotIndex = (swa & 0x07) - 2; // 小数点位置 | |||||
| bool isPositive = (((swb >> 1) & 0x01) == 0); // 符号:是否为正数 | |||||
| isStatic = (((swb >> 3) & 0x01) == 0); | |||||
| char[] num = new char[6]; | |||||
| for (int i = 0; i < 6; i++) { | |||||
| num[i] = buf[i + 4]; | |||||
| } | |||||
| if (dotIndex < 0) { // 不考虑精确到十,百位 | |||||
| return str.ToString(); | |||||
| } | |||||
| for (int i = 0; i < 6 - dotIndex; i++) { | |||||
| str.Append(num[i]); | |||||
| } | |||||
| str.Append('.'); | |||||
| for (int i = 0; i < dotIndex; i++) { | |||||
| str.Append(num[6 - dotIndex + i]); | |||||
| } | |||||
| // 去掉空格 | |||||
| string result = str.ToString().Trim(); | |||||
| // 取消前面多余的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) { | |||||
| result = result.Insert(0, "0"); | |||||
| } | |||||
| if (result.IndexOf('.') == result.Length - 1) { | |||||
| result = result.Remove(result.IndexOf('.'), 1); | |||||
| } | |||||
| if (!isPositive) { // 负数 | |||||
| result = result.Insert(0, "-"); | |||||
| } | |||||
| return result; | |||||
| } | |||||
| public override bool ParseAscii(string buf, out string weight, out bool isStatic) { | |||||
| isStatic = false; | |||||
| weight = FindDataFrame(buf, DataLength ); | |||||
| if (string.IsNullOrEmpty(weight)) { | |||||
| return false; | |||||
| } | |||||
| weight = ParseData(weight, out isStatic); | |||||
| return true; | |||||
| } | |||||
| public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr) { | |||||
| weight = ""; | |||||
| isStatic = false; | |||||
| subStr = ""; | |||||
| return false; | |||||
| } | |||||
| } | |||||
| //IND560单次读入 | |||||
| } | |||||
| @ -0,0 +1,110 @@ | |||||
| namespace BWP.WinFormControl.WeightDataFormat | |||||
| { | |||||
| public class Xk3190A9DataFormat : DataFormatBase { | |||||
| public override int DataLength { | |||||
| get { return 12; } | |||||
| } | |||||
| public override char Beginchar { | |||||
| get { return (char)0x02; } | |||||
| } | |||||
| public override char Endchar { | |||||
| get { return (char)0x03; ; } | |||||
| } | |||||
| 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]); | |||||
| weight = buf.Substring(2, 6).Trim(); | |||||
| // insert dot | |||||
| weight = InsertDot(weight, dot); | |||||
| isStatic = true; // 默认 为 稳定 | |||||
| // buffer[1] 符号位 | |||||
| if (buf[1] == '-') { | |||||
| weight = weight.Insert(0, "-"); | |||||
| } | |||||
| return weight; | |||||
| } | |||||
| public override bool ParseAscii(string buf, out string weight, out bool isStatic) { | |||||
| isStatic = false; | |||||
| weight = FindDataFrame(buf, DataLength ); | |||||
| if (string.IsNullOrEmpty(weight)) { | |||||
| return false; | |||||
| } | |||||
| weight = ParseData(weight, out isStatic); | |||||
| return true; | |||||
| } | |||||
| private static string InsertDot(string weight, int dotBits) { | |||||
| string str = weight.TrimStart(new[] { | |||||
| '0' | |||||
| }); | |||||
| str = str.Trim(); | |||||
| if (dotBits > 0) { | |||||
| //str = str.Insert(str.Length - dotBits, "."); | |||||
| if (string.IsNullOrEmpty(str)) { | |||||
| str = "0"; | |||||
| str = str.Insert(str.Length, "."); | |||||
| for (int i = 0; i < dotBits; i++) { | |||||
| str = str.Insert(str.Length, "0"); | |||||
| } | |||||
| } else | |||||
| str = str.Insert(str.Length - dotBits, "."); | |||||
| } | |||||
| 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) { | |||||
| isStatic = false; | |||||
| weight = FindDataFrame(buf, DataLength , out subStr); | |||||
| if (string.IsNullOrEmpty(weight)) { | |||||
| return false; | |||||
| } | |||||
| weight = ParseData(weight, out isStatic); | |||||
| return true; | |||||
| } | |||||
| // 根据开始字符找出 一个完整的数据帧 | |||||
| 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) { | |||||
| return string.Empty; | |||||
| } | |||||
| string data = buf.Substring(index, fSize); | |||||
| subStr = buf.Substring(index + fSize); | |||||
| // 检查结束字符 | |||||
| if (data[fSize - 1] != Endchar) { | |||||
| return string.Empty; | |||||
| } | |||||
| return data; | |||||
| } | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,106 @@ | |||||
| namespace BWP.WinFormControl.WeightDataFormat | |||||
| { | |||||
| public class Xk3190D10DataFormat : DataFormatBase { | |||||
| public override int DataLength { | |||||
| get { return 12; } | |||||
| } | |||||
| public override char Beginchar { | |||||
| get { return (char)0x02; } | |||||
| } | |||||
| public override char Endchar { | |||||
| get { return (char)0x0D; ; } | |||||
| } | |||||
| public override short Bufsize | |||||
| { | |||||
| get { return 24; } | |||||
| } | |||||
| public override string ParseData(string buf, out bool isStatic) { | |||||
| string weight; | |||||
| // 小数位数0-4 | |||||
| int dot = (short)(0x0F & buf[8]); | |||||
| weight = buf.Substring(2, 6).Trim(); | |||||
| // insert dot | |||||
| weight = InsertDot(weight, dot); | |||||
| isStatic = true; // 默认 为 稳定 | |||||
| // buffer[1] 符号位 | |||||
| if (buf[1] == '-') { | |||||
| weight = weight.Insert(0, "-"); | |||||
| } else { | |||||
| } | |||||
| return weight; | |||||
| } | |||||
| public override bool ParseAscii(string buf, out string weight, out bool isStatic) { | |||||
| isStatic = false; | |||||
| weight = FindDataFrame(buf, DataLength); | |||||
| if (string.IsNullOrEmpty(weight)) { | |||||
| return false; | |||||
| } | |||||
| weight = ParseData(weight, out isStatic); | |||||
| return true; | |||||
| } | |||||
| private static string InsertDot(string weight, int dotBits) { | |||||
| string str = weight.TrimStart(new[] { | |||||
| '0' | |||||
| }); | |||||
| str = str.Trim(); | |||||
| if (dotBits > 0) { | |||||
| //str = str.Insert(str.Length - dotBits, "."); | |||||
| if (string.IsNullOrEmpty(str)) { | |||||
| str = "0"; | |||||
| str = str.Insert(str.Length, "."); | |||||
| for (int i = 0; i < dotBits; i++) { | |||||
| str = str.Insert(str.Length, "0"); | |||||
| } | |||||
| } else | |||||
| str = str.Insert(str.Length - dotBits, "."); | |||||
| } | |||||
| 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) { | |||||
| isStatic = false; | |||||
| weight = FindDataFrame(buf, DataLength ); | |||||
| subStr = ""; | |||||
| if (string.IsNullOrEmpty(weight)) { | |||||
| return false; | |||||
| } | |||||
| weight = ParseData(weight, out isStatic); | |||||
| return true; | |||||
| } | |||||
| #region 1.3 验证int奇数偶数 | |||||
| /// <summary> | |||||
| /// 1.3 验证int奇数偶数 | |||||
| /// </summary> | |||||
| /// <param name="num"></param> | |||||
| /// <returns></returns> | |||||
| public bool isJO(int num) { | |||||
| int a = num % 2; | |||||
| if (a == 0) | |||||
| return true; | |||||
| else | |||||
| return false; | |||||
| } | |||||
| #endregion | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,23 @@ | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Runtime.InteropServices; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace BWP.WinFormControl | |||||
| { | |||||
| public class WinApiSendMessage | |||||
| { | |||||
| [DllImport("User32.dll", EntryPoint = "SendMessage")] | |||||
| public static extern int SendMessage( | |||||
| int hWnd, // handle to destination window | |||||
| int Msg, // message | |||||
| int wParam, // first message parameter | |||||
| int lParam // second message parameter | |||||
| ); | |||||
| [DllImport("User32.dll", EntryPoint = "FindWindow")] | |||||
| public static extern int FindWindow(string lpClassName, string lpWindowName); | |||||
| } | |||||
| } | |||||