添加电子称程序
定义一个委托:
delegate void ElectronicScales(string str); ElectronicScales strc;
在初始化电子称:
#region 电子称设置 public void Electronic() { //初始化窗口程序接受电子秤信息 try { serialPort1.PortName = "COM3"; switch (Share.GetCompanyId()) { case 1: serialPort1.BaudRate = 1200; break; default: serialPort1.BaudRate = 2400; break; } strc = new ElectronicScales(UpdateTextBox); serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived); serialPort1.Close(); serialPort1.Open(); } catch { } } private void UpdateTextBox(string weight) { TrueWeight = int.Parse(weight); //TrueWeight界面文本框 } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { var strweight = ""; Thread.Sleep(200); string com = serialPort1.ReadExisting(); char[] ch = com.Substring(com.IndexOf("=") + 1, 6).ToCharArray(); Array.Reverse(ch); strweight = Convert.ToInt32(new string(ch)).ToString(); this.Invoke(strc, new string[] { strweight }); } catch { } } #endregion
COM3是端口号,需要把带你呢的端口号设置成COM3