private void weightLoadBtn_Click(object sender, EventArgs e) { try { if (GlobalAccess.UseMockWeighter) { itemCountBox.Value = 100; itemWeightBox.Text = Convert.ToString(30.0m); return; } if (!alreadySendUnitWeight) { msgBox.Text = "请重新发送原单位重量"; return; } weightLoadBtn.Enabled = false; sendMessageBtn.Enabled = false; WeightData data = Weighter_Big.Instance.GetWeightData(); weightLoadBtn.Enabled = true; sendMessageBtn.Enabled = true; if (data != null) { if (data.isAnding) { itemCountBox.Value = Convert.ToInt32(data.count); itemWeightBox.Text = data.weight.ToString("F4"); msgBox.Clear(); itemCountBox.Focus(); itemCountBox.Select(); } else { msgBox.Text = "计量数值未安定,请重新读取"; itemCountBox.Value = itemCountBox.Minimum; itemWeightBox.Text = string.Empty; } } else { msgBox.Text = "通信错误"; } } catch (Exception ex) { weightLoadBtn.Enabled = true; sendMessageBtn.Enabled = true; msgBox.Text = ex.Message; } }
private void receiveMessageBtn_Click(object sender, EventArgs e) { try { if (!GlobalAccess.UseMockWeighter) { if (!isWeighted) { msgBox.Text = "请先执行计量器送信"; return; } receiveMessageBtn.Enabled = false; WeightData data = Weighter_Big.Instance.GetWeightData(); receiveMessageBtn.Enabled = true; if (data != null) { if (data.isAnding) { msgBox.Clear(); decimal count = data.count; SetRange(count); pickingCountBox.Text = count.ToString("F0"); } else { msgBox.Text = "计量数值未安定,请重新读取"; } } else { msgBox.Text = "通信错误"; } } else { pickingCountBox.Text = "30"; if (planCount == 0) { planCount = 1; } SetRange(30); } } catch (Exception ex) { receiveMessageBtn.Enabled = true; msgBox.Text = ex.Message; } }
private decimal GetUnitWeight() { if (!GlobalAccess.UseMockWeighter) { WeightData data = Weighter_Small.Instance.GetWeightData(); if (data != null) { return(data.weight); } return(-1); } else { return(0.4m); } }
private decimal GetWeight() { int retryTimes = 1; int retryInterval = 10000; try { retryTimes = Int32.Parse(AppConfig.Get("RetryTimes")); retryInterval = Int32.Parse(AppConfig.Get("RetryInterval")) * 1000; } catch (Exception) { } if (!GlobalAccess.UseMockWeighter) { for (int i = 0; i < retryTimes; i++) { for (int j = 0; j < 10; j++) { WeightData data = Weighter_Big.Instance.GetWeightData(); if (data != null) { if (data.isAnding) { return(data.weight); } else { if (j == 9) { return(-2); } continue; } } break; } Thread.Sleep(retryInterval); } return(-1); } else { return(5.0m); } }
private void button1_Click(object sender, EventArgs e) { try { Weighter_Small.Instance.SendItemWeight(7.77m); Weighter_Small.Instance.SendBucketWeight(99.9m); WeightData data = Weighter_Small.Instance.GetWeightData(); textBox1.AppendText(data.weight.ToString()); textBox1.AppendText("\r\n"); textBox1.AppendText(data.count.ToString()); textBox1.AppendText("\r\n"); } catch (Exception ex) { textBox1.AppendText(ex.Message + "\r\n"); } }
//public bool SendItemWeight(decimal itemWeight) //{ // try // { // if (SendENQ()) // { // byte[] weightCommand = WeighterCommand.ITEM_WEIGHT; // byte[] weightBytes = MakeWeightData(itemWeight); // for (int i = 0; i < 8; i++) // { // weightCommand[weightCommand.Length - 9 + i] = weightBytes[i]; // } // Write(weightCommand); // Thread.Sleep(latency); // byte[] bytes = Read(); // if (IsEqual(bytes, WeighterCommand.ACK1)) // { // SendEOT(); // } // } // } // finally // { // sp.Close(); // } // return false; //} //public bool SendBucketWeight(decimal bucketWeight) //{ // try // { // if (SendENQ()) // { // byte[] weightCommand = WeighterCommand.BUCKUT_WEIGHT; // byte[] weightBytes = MakeWeightData(bucketWeight); // weightBytes[5] = 48; // for (int i = 0; i < 8; i++) // { // weightCommand[weightCommand.Length - 9 + i] = weightBytes[i]; // } // Write(weightCommand); // Thread.Sleep(latency); // byte[] bytes = Read(); // if (IsEqual(bytes, WeighterCommand.ACK1)) // { // SendEOT(); // } // } // } // finally // { // sp.Close(); // } // return false; //} public WeightData GetWeightData() { try { if (SendENQ()) { if (SendWeightRequest()) { SendEOT(); Thread.Sleep(latency); byte[] bytes = Read(); if (IsEqual(bytes, WeighterCommand.ENQ)) { Write(WeighterCommand.ACK0); //Thread.Sleep(latency); bytes = ReadLong(); if (bytes.Length >= 32) { try { Write(WeighterCommand.ACK1); Read(); } finally { sp.Close(); } WeightData data = DoWeightData(bytes); //ThreadPool.QueueUserWorkItem(new WaitCallback(DoTheEnd)); return(data); } } } } } catch (Exception) { sp.Close(); throw; } return(null); }
private WeightData DoWeightData(byte[] bytes) { WeightData data = new WeightData(); int pm = bytes[8] - 48; StringBuilder sb = new StringBuilder(); for (int i = 9; i < 15; i++) { sb.Append((bytes[i] - 48).ToString()); } data.count = int.Parse(sb.ToString()); sb = new StringBuilder(); for (int i = 17; i < 23; i++) { sb.Append((bytes[i] - 48).ToString()); } decimal weight = decimal.Parse(sb.ToString()); //重量 int e = bytes[24] - 48; //指数 if (e > 0) { weight = weight / Convert.ToDecimal(Math.Pow(10, e)); } if (pm == 1) { data.weight = 0 - weight; } else { data.weight = weight; } int anding = bytes[7] - 48; if (anding == 0 || anding == 4) { data.isAnding = true; } else { data.isAnding = false; } return(data); }