在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → c#获取电子称数据 源码下载

c#获取电子称数据 源码下载

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.16M
  • 下载次数:62
  • 浏览次数:396
  • 发布时间:2017-04-21
  • 实例类别:C#语言基础
  • 发 布 人:gzldaq
  • 文件格式:.zip
  • 所需积分:3
 相关标签: 电子称对接

实例介绍

【实例简介】电子称对接

【实例截图】

【核心代码】


using SerialScale.Properties;
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
namespace SerialScale
{
	public class MainForm : Form
	{
		private delegate void RunCallback();
		private delegate void ScanEndCallback(bool found);
		private delegate void ScanP1Callback(int vto);
		private delegate void ScanP2Callback(int vto);
		private byte[] Protocol_A_Buffer = new byte[14];
		private int Protocol_A_Lng;
		private byte[] Protocol_B_Buffer = new byte[6];
		private int Protocol_B_Lng;
		private byte[] Protocol_C_Buffer = new byte[18];
		private int Protocol_C_Lng;
		private byte[] Protocol_D_Buffer = new byte[8];
		private int Protocol_D_Lng;
		private byte[] Protocol_E_Buffer = new byte[12];
		private int Protocol_E_Lng;
		private byte[] Protocol_F_Buffer = new byte[22];
		private int Protocol_F_Lng;
		private string gweight = "";
		private string gunitprice = "";
		private string gtotal = "";
		private string ginfo = "";
		private int gPt = -1;
		private DateTime delay = DateTime.Now;
		private byte[] ghex;
		private int spcount;
		private bool run;
		private bool scanning;
		private IContainer components;
		private ComboBox baudComboBox;
		private ComboBox serialComboBox;
		private Button startButton;
		private NotifyIcon notifyIcon;
		private SerialPort serialPort;
		private ContextMenuStrip contextMenuStrip;
		private ToolStripMenuItem startToolStripMenuItem;
		private ToolStripMenuItem exitToolStripMenuItem;
		private ToolStripMenuItem showToolStripMenuItem;
		private ComboBox protocolComboBox;
		private Button scanButton;
		private Label weight_Label;
		private Label unitPrice_label;
		private Label total_Label;
		private TextBox weight_TextBox;
		private TextBox unitPrice_TextBox;
		private TextBox total_TextBox;
		private Label weightinfo_Label;
		private TextBox hex_textBox;
		public MainForm()
		{
			this.InitializeComponent();
		}
		private void MainForm_Load(object sender, EventArgs e)
		{
			this.serialComboBox.Items.AddRange(SerialPort.GetPortNames());
			this.serialComboBox.Text = Settings.Default.PortName;
			if (this.serialComboBox.SelectedIndex == -1)
			{
				this.serialComboBox.SelectedIndex = ((this.serialComboBox.Items.Count > 0) ? 0 : -1);
			}
		}
		private int BCDtoINT(byte v)
		{
			if ((v & 15) > 9)
			{
				throw new Exception();
			}
			if (v / 16 > 9)
			{
				throw new Exception();
			}
			return (int)((v & 15)   (v & 240) / 16 * 10);
		}
		private bool Protocol_A(byte[] input)
		{
			int start = -1;
			int start2 = -1;
			bool withvalue = false;
			for (int pos = input.Length - 1; pos >= 0; pos--)
			{
				if (input[pos] == 255)
				{
					if (start >= 0)
					{
						start2 = pos;
						break;
					}
					start = pos;
				}
			}
			if (this.Protocol_A_Lng == 0)
			{
				if (start2 >= 0)
				{
					start = start2;
				}
				if (start < 0)
				{
					return false;
				}
			}
			else
			{
				if (start2 >= 0)
				{
					start = start2;
					this.Protocol_A_Lng = 0;
				}
				else
				{
					start = 0;
				}
			}
			while (start < input.Length)
			{
				this.Protocol_A_Buffer[this.Protocol_A_Lng] = input[start];
				if (this.Protocol_A_Lng != 0 || input[start] == 255)
				{
					this.Protocol_A_Lng  ;
				}
				if (this.Protocol_A_Lng == this.Protocol_A_Buffer.Length)
				{
					try
					{
						string ctinfo = this.Protocol_A_Buffer[13].ToString()   ":";
						if ((this.Protocol_A_Buffer[1] & 16) != 0)
						{
							ctinfo  = "稳定";
						}
						else
						{
							ctinfo  = "不稳定";
						}
						decimal ctweight = this.BCDtoINT(this.Protocol_A_Buffer[2])   this.BCDtoINT(this.Protocol_A_Buffer[3]) * 100   this.BCDtoINT(this.Protocol_A_Buffer[4]) * 10000;
						if ((this.Protocol_A_Buffer[1] & 8) != 0)
						{
							ctweight = -ctweight;
						}
						for (int dot = (int)(this.Protocol_A_Buffer[1] & 7); dot > 1; dot--)
						{
							ctweight /= 10m;
						}
						decimal ctunitprice = this.BCDtoINT(this.Protocol_A_Buffer[6])   this.BCDtoINT(this.Protocol_A_Buffer[7]) * 100   this.BCDtoINT(this.Protocol_A_Buffer[8]) * 10000;
						for (int dot2 = (int)(this.Protocol_A_Buffer[5] & 7); dot2 > 1; dot2--)
						{
							ctunitprice /= 10m;
						}
						decimal cttotal = this.BCDtoINT(this.Protocol_A_Buffer[10])   this.BCDtoINT(this.Protocol_A_Buffer[11]) * 100   this.BCDtoINT(this.Protocol_A_Buffer[12]) * 10000;
						for (int dot3 = (int)(this.Protocol_A_Buffer[9] & 7); dot3 > 1; dot3--)
						{
							cttotal /= 10m;
						}
						this.gtotal = cttotal.ToString();
						this.gunitprice = ctunitprice.ToString();
						this.gweight = ctweight.ToString();
						this.ginfo = ctinfo;
						this.ghex = this.Protocol_A_Buffer;
						withvalue = true;
					}
					catch (Exception)
					{
					}
					this.Protocol_A_Lng = 0;
				}
				start  ;
			}
			return withvalue;
		}
		private bool Protocol_B(byte[] input)
		{
			int start = -1;
			int start2 = -1;
			bool withvalue = false;
			for (int pos = input.Length - 1; pos >= 0; pos--)
			{
				if (input[pos] == 255)
				{
					if (start >= 0)
					{
						start2 = pos;
						break;
					}
					start = pos;
				}
			}
			if (this.Protocol_B_Lng == 0)
			{
				if (start2 >= 0)
				{
					start = start2;
				}
				if (start < 0)
				{
					return false;
				}
			}
			else
			{
				if (start2 >= 0)
				{
					start = start2;
					this.Protocol_B_Lng = 0;
				}
				else
				{
					start = 0;
				}
			}
			while (start < input.Length)
			{
				this.Protocol_B_Buffer[this.Protocol_B_Lng] = input[start];
				if (this.Protocol_B_Lng != 0 || input[start] == 255)
				{
					this.Protocol_B_Lng  ;
				}
				if (this.Protocol_B_Lng == this.Protocol_B_Buffer.Length)
				{
					try
					{
						string ctinfo;
						if ((this.Protocol_B_Buffer[1] & 64) != 0)
						{
							ctinfo = "稳定";
						}
						else
						{
							ctinfo = "不稳定";
						}
						decimal ctweight = this.BCDtoINT(this.Protocol_B_Buffer[2])   this.BCDtoINT(this.Protocol_B_Buffer[3]) * 100   this.BCDtoINT(this.Protocol_B_Buffer[4]) * 10000;
						if ((this.Protocol_B_Buffer[1] & 32) != 0)
						{
							ctweight = -ctweight;
						}
						for (int dot = (int)(this.Protocol_B_Buffer[1] & 7); dot > 1; dot--)
						{
							ctweight /= 10m;
						}
						decimal ctunitprice = 0m;
						this.gtotal = 0m.ToString();
						this.gunitprice = ctunitprice.ToString();
						this.gweight = ctweight.ToString();
						this.ginfo = ctinfo;
						this.ghex = this.Protocol_B_Buffer;
						withvalue = true;
					}
					catch (Exception)
					{
					}
					this.Protocol_B_Lng = 0;
				}
				start  ;
			}
			return withvalue;
		}
		private bool Protocol_C(byte[] input)
		{
			int start = -1;
			int start2 = -1;
			bool withvalue = false;
			for (int pos = input.Length - 1; pos >= 0; pos--)
			{
				if (input[pos] == 87)
				{
					if (start >= 0)
					{
						start2 = pos;
						break;
					}
					start = pos;
				}
			}
			if (this.Protocol_C_Lng == 0)
			{
				if (start2 >= 0)
				{
					start = start2;
				}
				if (start < 0)
				{
					return false;
				}
			}
			else
			{
				if (start2 >= 0)
				{
					start = start2;
					this.Protocol_C_Lng = 0;
				}
				else
				{
					start = 0;
				}
			}
			while (start < input.Length)
			{
				this.Protocol_C_Buffer[this.Protocol_C_Lng] = input[start];
				if (this.Protocol_C_Lng != 0 || input[start] == 87)
				{
					this.Protocol_C_Lng  ;
				}
				if (this.Protocol_C_Lng == this.Protocol_C_Buffer.Length)
				{
					try
					{
						if (this.Protocol_C_Buffer[16] == 13 && this.Protocol_C_Buffer[17] == 10)
						{
							string ctinfo;
							if (this.Protocol_C_Buffer[2] == 83 && this.Protocol_C_Buffer[3] == 84)
							{
								ctinfo = "稳定";
							}
							else
							{
								if (this.Protocol_C_Buffer[2] == 85 && this.Protocol_C_Buffer[3] == 83)
								{
									ctinfo = "不稳定";
								}
								else
								{
									if (this.Protocol_C_Buffer[2] == 79 && this.Protocol_C_Buffer[3] == 76)
									{
										ctinfo = "超重";
									}
									else
									{
										ctinfo = "未知";
									}
								}
							}
							string s = "";
							for (int i = 5; i < 14; i  )
							{
								s  = (char)this.Protocol_C_Buffer[i];
							}
							s.Trim();
							decimal ctweight = decimal.Parse(s);
							if (this.Protocol_C_Buffer[4] == 45)
							{
								ctweight = -ctweight;
							}
							decimal ctunitprice = 0m;
							this.gtotal = 0m.ToString();
							this.gunitprice = ctunitprice.ToString();
							this.gweight = ctweight.ToString();
							this.ginfo = ctinfo;
							this.ghex = this.Protocol_C_Buffer;
							withvalue = true;
						}
					}
					catch (Exception)
					{
					}
					this.Protocol_C_Lng = 0;
				}
				start  ;
			}
			return withvalue;
		}
		private bool Protocol_D(byte[] input)
		{
			int start = -1;
			int start2 = -1;
			bool withvalue = false;
			for (int pos = input.Length - 1; pos >= 0; pos--)
			{
				if (input[pos] == 61)
				{
					if (start >= 0)
					{
						start2 = pos;
						break;
					}
					start = pos;
				}
			}
			if (this.Protocol_D_Lng == 0)
			{
				if (start2 >= 0)
				{
					start = start2;
				}
				if (start < 0)
				{
					return false;
				}
			}
			else
			{
				if (start2 >= 0)
				{
					start = start2;
					this.Protocol_D_Lng = 0;
				}
				else
				{
					start = 0;
				}
			}
			while (start < input.Length)
			{
				this.Protocol_D_Buffer[this.Protocol_D_Lng] = input[start];
				if (this.Protocol_D_Lng != 0 || input[start] == 61)
				{
					this.Protocol_D_Lng  ;
				}
				if (this.Protocol_D_Lng == this.Protocol_D_Buffer.Length)
				{
					try
					{
						string ctinfo = "";
						string s = "";
						for (int i = 6; i > 0; i--)
						{
							s  = (char)this.Protocol_D_Buffer[i];
						}
						s.Trim();
						decimal ctweight = decimal.Parse(s);
						if (this.Protocol_D_Buffer[7] == 45)
						{
							ctweight = -ctweight;
						}
						decimal ctunitprice = 0m;
						this.gtotal = 0m.ToString();
						this.gunitprice = ctunitprice.ToString();
						this.gweight = ctweight.ToString();
						this.ginfo = ctinfo;
						this.ghex = this.Protocol_D_Buffer;
						withvalue = true;
					}
					catch (Exception)
					{
					}
					this.Protocol_D_Lng = 0;
				}
				start  ;
			}
			return withvalue;
		}
		private bool Protocol_E(byte[] input)
		{
			int start = -1;
			int start2 = -1;
			bool withvalue = false;
			for (int pos = input.Length - 1; pos >= 0; pos--)
			{
				if (input[pos] == 2)
				{
					if (start >= 0)
					{
						start2 = pos;
						break;
					}
					start = pos;
				}
			}
			if (this.Protocol_E_Lng == 0)
			{
				if (start2 >= 0)
				{
					start = start2;
				}
				if (start < 0)
				{
					return false;
				}
			}
			else
			{
				if (start2 >= 0)
				{
					start = start2;
					this.Protocol_E_Lng = 0;
				}
				else
				{
					start = 0;
				}
			}
			while (start < input.Length)
			{
				this.Protocol_E_Buffer[this.Protocol_E_Lng] = input[start];
				if (this.Protocol_E_Lng != 0 || input[start] == 2)
				{
					this.Protocol_E_Lng  ;
				}
				if (this.Protocol_E_Lng == this.Protocol_E_Buffer.Length)
				{
					try
					{
						string ctinfo = "";
						string a = ((int)(this.Protocol_E_Buffer[1] ^ this.Protocol_E_Buffer[2] ^ this.Protocol_E_Buffer[3] ^ this.Protocol_E_Buffer[4] ^ this.Protocol_E_Buffer[5] ^ this.Protocol_E_Buffer[6] ^ this.Protocol_E_Buffer[7] ^ this.Protocol_E_Buffer[8])).ToString("X2");
						string b = (char)this.Protocol_E_Buffer[9]   (char)this.Protocol_E_Buffer[10] "";
						if (this.Protocol_E_Buffer[11] == 3 && a == b)
						{
							string s = "";
							for (int i = 2; i < 8; i  )
							{
								s  = (char)this.Protocol_E_Buffer[i];
							}
							s.Trim();
							decimal ctweight = decimal.Parse(s);
							if (this.Protocol_E_Buffer[2] == 45)
							{
								ctweight = -ctweight;
							}
							for (int j = 0; j < (int)(this.Protocol_E_Buffer[8] - 48); j  )
							{
								ctweight /= 10m;
							}
							decimal ctunitprice = 0m;
							this.gtotal = 0m.ToString();
							this.gunitprice = ctunitprice.ToString();
							this.gweight = ctweight.ToString();
							this.ginfo = ctinfo;
							this.ghex = this.Protocol_E_Buffer;
							withvalue = true;
						}
					}
					catch (Exception)
					{
					}
					this.Protocol_E_Lng = 0;
				}
				start  ;
			}
			return withvalue;
		}
		private bool Protocol_F(byte[] input)
		{
			int start = -1;
			int start2 = -1;
			bool withvalue = false;
			for (int pos = input.Length - 1; pos >= 0; pos--)
			{
				if (input[pos] == 10)
				{
					if (start >= 0)
					{
						start2 = pos;
						break;
					}
					start = pos;
				}
			}
			if (this.Protocol_F_Lng == 0)
			{
				if (start2 >= 0)
				{
					start = start2;
				}
				if (start < 0)
				{
					return false;
				}
			}
			else
			{
				if (start2 >= 0)
				{
					start = start2;
					this.Protocol_F_Lng = 0;
				}
				else
				{
					start = 0;
				}
			}
			while (start < input.Length)
			{
				this.Protocol_F_Buffer[this.Protocol_F_Lng] = input[start];
				if (this.Protocol_F_Lng != 0 || input[start] == 10)
				{
					this.Protocol_F_Lng  ;
				}
				if (this.Protocol_F_Lng == this.Protocol_F_Buffer.Length)
				{
					try
					{
						string ctinfo = "";
						if (this.Protocol_F_Buffer[1] == 13 && this.Protocol_F_Buffer[7] == 32 && this.Protocol_F_Buffer[8] == 32 && this.Protocol_F_Buffer[14] == 32 && this.Protocol_F_Buffer[15] == 32)
						{
							string s = "";
							for (int i = 2; i < 7; i  )
							{
								s  = (char)this.Protocol_F_Buffer[i];
							}
							s.Trim();
							decimal ctweight = decimal.Parse(s) / 1000m;
							s = "";
							for (int j = 9; j < 14; j  )
							{
								s  = (char)this.Protocol_F_Buffer[j];
							}
							s.Trim();
							decimal ctunitprice = decimal.Parse(s) / 100m;
							s = "";
							for (int k = 16; k < 22; k  )
							{
								s  = (char)this.Protocol_F_Buffer[k];
							}
							s.Trim();
							this.gtotal = (decimal.Parse(s) / 100m).ToString();
							this.gunitprice = ctunitprice.ToString();
							this.gweight = ctweight.ToString();
							this.ginfo = ctinfo;
							this.ghex = this.Protocol_F_Buffer;
							withvalue = true;
						}
					}
					catch (Exception)
					{
					}
					this.Protocol_F_Lng = 0;
				}
				start  ;
			}
			return withvalue;
		}
		private void Send()
		{
			this.weight_TextBox.Text = this.gweight;
			this.unitPrice_TextBox.Text = this.gunitprice;
			this.total_TextBox.Text = this.gtotal;
			this.weightinfo_Label.Text = this.ginfo;
			this.protocolComboBox.SelectedIndex = this.gPt;
			this.delay = DateTime.Now.AddSeconds(2.0);
			string hex = "";
			if (this.ghex != null && this.ghex.Length > 0)
			{
				if (this.protocolComboBox.SelectedIndex == 2)
				{
					hex = "ASCII:";
					for (int i = 0; i < this.ghex.Length; i  )
					{
						hex  = (char)this.ghex[i];
					}
				}
				else
				{
					hex = "HEX:";
					for (int j = 0; j < this.ghex.Length; j  )
					{
						hex = hex   this.ghex[j].ToString("X2")   ' ';
					}
				}
			}
			this.hex_textBox.Text = hex;
		}
		private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{
			int count = this.serialPort.BytesToRead;
			this.spcount  = count;
			if (count <= 0)
			{
				return;
			}
			byte[] arr = new byte[count];
			this.serialPort.Read(arr, 0, count);
			if (this.delay > DateTime.Now)
			{
				if (this.gPt == 0)
				{
					if (!this.Protocol_A(arr))
					{
						return;
					}
				}
				else
				{
					if (this.gPt == 1)
					{
						if (this.Protocol_A(arr))
						{
							this.gPt = 0;
						}
						else
						{
							if (!this.Protocol_B(arr))
							{
								return;
							}
						}
					}
					else
					{
						if (this.gPt == 2)
						{
							if (!this.Protocol_C(arr))
							{
								return;
							}
						}
						else
						{
							if (this.gPt == 3)
							{
								if (!this.Protocol_D(arr))
								{
									return;
								}
							}
							else
							{
								if (this.gPt == 4)
								{
									if (!this.Protocol_E(arr))
									{
										return;
									}
								}
								else
								{
									if (this.gPt == 5 && !this.Protocol_F(arr))
									{
										return;
									}
								}
							}
						}
					}
				}
			}
			else
			{
				if (this.Protocol_A(arr))
				{
					this.gPt = 0;
				}
				else
				{
					if (this.Protocol_B(arr))
					{
						this.gPt = 1;
					}
					else
					{
						if (this.Protocol_C(arr))
						{
							this.gPt = 2;
						}
						else
						{
							if (this.Protocol_D(arr))
							{
								this.gPt = 3;
							}
							else
							{
								if (this.Protocol_E(arr))
								{
									this.gPt = 4;
								}
								else
								{
									if (!this.Protocol_F(arr))
									{
										return;
									}
									this.gPt = 5;
								}
							}
						}
					}
				}
			}
			if (!this.scanning)
			{
				base.Invoke(new MainForm.RunCallback(this.Send));
			}
		}
		private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.Stop();
			Settings.Default.Save();
		}
		private void startButton_Click(object sender, EventArgs e)
		{
			this.Run();
		}
		private void Run()
		{
			if (!this.run)
			{
				this.Start();
				this.run = true;
				return;
			}
			this.Stop();
			this.run = false;
		}
		private void Start()
		{
			this.Text = "紫蝶科技:串口秤重 - 启用";
			this.startToolStripMenuItem.Text = (this.startButton.Text = "禁用");
			this.scanButton.Enabled = false;
			int baudRate;
			if (!int.TryParse(Settings.Default.BaudRate, out baudRate))
			{
				baudRate = 9600;
			}
			this.serialPort.PortName = Settings.Default.PortName;
			this.serialPort.BaudRate = baudRate;
			this.serialPort.Open();
		}
		private void Stop()
		{
			this.serialPort.Close();
			Thread.Sleep(500);
			this.startToolStripMenuItem.Text = (this.startButton.Text = "启用");
			this.Text = "紫蝶科技:串口秤重 - 禁用";
			this.scanButton.Enabled = true;
		}
		private void MainForm_SizeChanged(object sender, EventArgs e)
		{
		}
		private void cancelButton_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		private void notifyIcon_DoubleClick(object sender, EventArgs e)
		{
			this.notifyIcon.Visible = false;
			base.Visible = true;
			base.WindowState = FormWindowState.Normal;
		}
		private void ScanEnd(bool found)
		{
			if (base.InvokeRequired)
			{
				base.Invoke(new MainForm.ScanEndCallback(this.ScanEnd), new object[]
				{
					found
				});
				return;
			}
			this.startButton.Enabled = true;
			this.scanButton.Enabled = true;
			this.serialComboBox.Enabled = (this.baudComboBox.Enabled = true);
			this.scanning = false;
			if (found)
			{
				this.startButton.PerformClick();
				return;
			}
			MessageBox.Show("扫描失败");
		}
		private void ScanP1(int vto)
		{
			if (base.InvokeRequired)
			{
				base.Invoke(new MainForm.ScanP1Callback(this.ScanP1), new object[]
				{
					vto
				});
				return;
			}
			this.serialComboBox.SelectedIndex = vto;
		}
		private void ScanP2(int vto)
		{
			if (base.InvokeRequired)
			{
				base.Invoke(new MainForm.ScanP2Callback(this.ScanP2), new object[]
				{
					vto
				});
				return;
			}
			this.baudComboBox.SelectedIndex = vto;
		}
		private void ScanBasic()
		{
			for (int i = 0; i < this.serialComboBox.Items.Count; i  )
			{
				this.ScanP1(i);
				this.spcount = 0;
				try
				{
					this.serialPort.PortName = this.serialComboBox.Items[i].ToString();
					this.serialPort.BaudRate = 9600;
					this.serialPort.Open();
					int di = 0;
					while (di < Settings.Default.ScanTime && this.spcount <= 0)
					{
						Thread.Sleep(100);
						di  ;
					}
				}
				catch (Exception)
				{
					this.spcount = 0;
				}
				this.serialPort.Close();
				if (this.spcount != 0)
				{
					this.gPt = -1;
					for (int j = 0; j < this.baudComboBox.Items.Count; j  )
					{
						this.ScanP2(j);
						int baudRate;
						if (int.TryParse(this.baudComboBox.Items[j].ToString(), out baudRate))
						{
							this.serialPort.BaudRate = baudRate;
							this.serialPort.Open();
							int pta = -1;
							int ptb = -1;
							for (int di2 = 0; di2 < Settings.Default.ScanTime; di2  )
							{
								if (this.gPt >= 0)
								{
									if (this.gPt == pta && this.gPt == ptb)
									{
										break;
									}
									ptb = pta;
									pta = this.gPt;
									this.gPt = -1;
								}
								Thread.Sleep(100);
							}
							this.serialPort.Close();
							if (this.gPt >= 0)
							{
								this.ScanEnd(true);
								return;
							}
						}
					}
				}
			}
			this.ScanEnd(false);
		}
		private void scanButton_Click(object sender, EventArgs e)
		{
			this.Text = "紫蝶科技:串口秤重 - 扫描";
			this.startButton.Enabled = false;
			this.scanButton.Enabled = false;
			this.serialComboBox.Enabled = (this.baudComboBox.Enabled = false);
			this.scanning = true;
			this.serialComboBox.Items.Clear();
			this.serialComboBox.Items.AddRange(SerialPort.GetPortNames());
			Thread thread = new Thread(new ThreadStart(this.ScanBasic));
			thread.Start();
		}
		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.run)
			{
				this.Stop();
				this.run = false;
			}
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			this.components = new Container();
			ComponentResourceManager resources = new ComponentResourceManager(typeof(MainForm));
			this.startButton = new Button();
			this.notifyIcon = new NotifyIcon(this.components);
			this.contextMenuStrip = new ContextMenuStrip(this.components);
			this.startToolStripMenuItem = new ToolStripMenuItem();
			this.exitToolStripMenuItem = new ToolStripMenuItem();
			this.showToolStripMenuItem = new ToolStripMenuItem();
			this.serialPort = new SerialPort(this.components);
			this.protocolComboBox = new ComboBox();
			this.baudComboBox = new ComboBox();
			this.serialComboBox = new ComboBox();
			this.scanButton = new Button();
			this.weight_Label = new Label();
			this.unitPrice_label = new Label();
			this.total_Label = new Label();
			this.weight_TextBox = new TextBox();
			this.unitPrice_TextBox = new TextBox();
			this.total_TextBox = new TextBox();
			this.weightinfo_Label = new Label();
			this.hex_textBox = new TextBox();
			this.contextMenuStrip.SuspendLayout();
			base.SuspendLayout();
			this.startButton.Location = new Point(205, 80);
			this.startButton.Name = "startButton";
			this.startButton.Size = new Size(75, 23);
			this.startButton.TabIndex = 11;
			this.startButton.Text = "启用";
			this.startButton.UseVisualStyleBackColor = true;
			this.startButton.Click  = new EventHandler(this.startButton_Click);
			this.notifyIcon.ContextMenuStrip = this.contextMenuStrip;
			this.notifyIcon.Icon = (Icon)resources.GetObject("notifyIcon.Icon");
			this.notifyIcon.Text = "串口击键";
			this.notifyIcon.DoubleClick  = new EventHandler(this.notifyIcon_DoubleClick);
			this.contextMenuStrip.Items.AddRange(new ToolStripItem[]
			{
				this.startToolStripMenuItem,
				this.exitToolStripMenuItem,
				this.showToolStripMenuItem
			});
			this.contextMenuStrip.Name = "contextMenuStrip";
			this.contextMenuStrip.Size = new Size(101, 70);
			this.startToolStripMenuItem.Name = "startToolStripMenuItem";
			this.startToolStripMenuItem.Size = new Size(100, 22);
			this.startToolStripMenuItem.Text = "开始";
			this.startToolStripMenuItem.Click  = new EventHandler(this.startButton_Click);
			this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
			this.exitToolStripMenuItem.Size = new Size(100, 22);
			this.exitToolStripMenuItem.Text = "显示";
			this.exitToolStripMenuItem.Click  = new EventHandler(this.notifyIcon_DoubleClick);
			this.showToolStripMenuItem.Name = "showToolStripMenuItem";
			this.showToolStripMenuItem.Size = new Size(100, 22);
			this.showToolStripMenuItem.Text = "退出";
			this.showToolStripMenuItem.Click  = new EventHandler(this.cancelButton_Click);
			this.serialPort.DataReceived  = new SerialDataReceivedEventHandler(this.serialPort_DataReceived);
			this.protocolComboBox.Enabled = false;
			this.protocolComboBox.FormattingEnabled = true;
			this.protocolComboBox.Items.AddRange(new object[]
			{
				"协议A",
				"协议B",
				"协议C",
				"协议D",
				"协议E",
				"协议F"
			});
			this.protocolComboBox.Location = new Point(120, 38);
			this.protocolComboBox.Name = "protocolComboBox";
			this.protocolComboBox.Size = new Size(160, 20);
			this.protocolComboBox.TabIndex = 10;
			this.protocolComboBox.Text = "协议A";
			this.baudComboBox.DataBindings.Add(new Binding("Text", Settings.Default, "BaudRate", true, DataSourceUpdateMode.OnPropertyChanged));
			this.baudComboBox.FormattingEnabled = true;
			this.baudComboBox.Items.AddRange(new object[]
			{
				"300 ",
				"600",
				"1200",
				"2400",
				"4800",
				"9600",
				"19200",
				"38400",
				"43000",
				"56000",
				"57600",
				"115200"
			});
			this.baudComboBox.Location = new Point(120, 12);
			this.baudComboBox.Name = "baudComboBox";
			this.baudComboBox.Size = new Size(160, 20);
			this.baudComboBox.TabIndex = 10;
			this.baudComboBox.Text = Settings.Default.BaudRate;
			this.serialComboBox.DataBindings.Add(new Binding("Text", Settings.Default, "PortName", true, DataSourceUpdateMode.OnPropertyChanged));
			this.serialComboBox.FormattingEnabled = true;
			this.serialComboBox.Location = new Point(12, 12);
			this.serialComboBox.Name = "serialComboBox";
			this.serialComboBox.Size = new Size(93, 20);
			this.serialComboBox.TabIndex = 9;
			this.serialComboBox.Text = Settings.Default.PortName;
			this.scanButton.Location = new Point(120, 80);
			this.scanButton.Name = "scanButton";
			this.scanButton.Size = new Size(75, 23);
			this.scanButton.TabIndex = 11;
			this.scanButton.Text = "扫描";
			this.scanButton.UseVisualStyleBackColor = true;
			this.scanButton.Click  = new EventHandler(this.scanButton_Click);
			this.weight_Label.AutoSize = true;
			this.weight_Label.Location = new Point(41, 145);
			this.weight_Label.Name = "weight_Label";
			this.weight_Label.Size = new Size(29, 12);
			this.weight_Label.TabIndex = 14;
			this.weight_Label.Text = "重量";
			this.unitPrice_label.AutoSize = true;
			this.unitPrice_label.Location = new Point(41, 177);
			this.unitPrice_label.Name = "unitPrice_label";
			this.unitPrice_label.Size = new Size(29, 12);
			this.unitPrice_label.TabIndex = 14;
			this.unitPrice_label.Text = "单价";
			this.total_Label.AutoSize = true;
			this.total_Label.Location = new Point(41, 209);
			this.total_Label.Name = "total_Label";
			this.total_Label.Size = new Size(29, 12);
			this.total_Label.TabIndex = 14;
			this.total_Label.Text = "合计";
			this.weight_TextBox.Location = new Point(120, 139);
			this.weight_TextBox.Name = "weight_TextBox";
			this.weight_TextBox.ReadOnly = true;
			this.weight_TextBox.Size = new Size(160, 21);
			this.weight_TextBox.TabIndex = 15;
			this.unitPrice_TextBox.Location = new Point(120, 170);
			this.unitPrice_TextBox.Name = "unitPrice_TextBox";
			this.unitPrice_TextBox.ReadOnly = true;
			this.unitPrice_TextBox.Size = new Size(160, 21);
			this.unitPrice_TextBox.TabIndex = 15;
			this.total_TextBox.Location = new Point(120, 204);
			this.total_TextBox.Name = "total_TextBox";
			this.total_TextBox.ReadOnly = true;
			this.total_TextBox.Size = new Size(160, 21);
			this.total_TextBox.TabIndex = 15;
			this.weightinfo_Label.AutoSize = true;
			this.weightinfo_Label.Location = new Point(122, 120);
			this.weightinfo_Label.Name = "weightinfo_Label";
			this.weightinfo_Label.Size = new Size(29, 12);
			this.weightinfo_Label.TabIndex = 14;
			this.weightinfo_Label.Text = "稳定";
			this.hex_textBox.Location = new Point(43, 237);
			this.hex_textBox.Name = "hex_textBox";
			this.hex_textBox.ReadOnly = true;
			this.hex_textBox.Size = new Size(237, 21);
			this.hex_textBox.TabIndex = 15;
			base.AcceptButton = this.startButton;
			base.AutoScaleDimensions = new SizeF(6f, 12f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(292, 275);
			base.Controls.Add(this.unitPrice_TextBox);
			base.Controls.Add(this.total_TextBox);
			base.Controls.Add(this.hex_textBox);
			base.Controls.Add(this.weight_TextBox);
			base.Controls.Add(this.weightinfo_Label);
			base.Controls.Add(this.weight_Label);
			base.Controls.Add(this.startButton);
			base.Controls.Add(this.unitPrice_label);
			base.Controls.Add(this.total_Label);
			base.Controls.Add(this.baudComboBox);
			base.Controls.Add(this.protocolComboBox);
			base.Controls.Add(this.serialComboBox);
			base.Controls.Add(this.scanButton);
			base.FormBorderStyle = FormBorderStyle.FixedSingle;
			base.Icon = (Icon)resources.GetObject("$this.Icon");
			base.MaximizeBox = false;
			base.Name = "MainForm";
			this.Text = "紫蝶科技:串口秤重 - 禁用";
			base.FormClosing  = new FormClosingEventHandler(this.MainForm_FormClosing);
			base.FormClosed  = new FormClosedEventHandler(this.MainForm_FormClosed);
			base.Load  = new EventHandler(this.MainForm_Load);
			base.SizeChanged  = new EventHandler(this.MainForm_SizeChanged);
			this.contextMenuStrip.ResumeLayout(false);
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
} 


标签: 电子称对接

实例下载地址

c#获取电子称数据 源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警