public void getWeight() { lastCmd = Command.GetWeight; byte[] buffer; buffer = new byte[2]; buffer[0] = 0x1B; buffer[1] = 0x50; lastScaledWeight = null; sendMessage(buffer, buffer.Length); }
scaleResult getScaleInfo(string data) { scaleResult sr = new scaleResult(); sr.deviceSerial = deviceSerial; if (data.Length != 14) { return(null); } string strWeight = data.Substring(0, 10); string strUnit = data.Substring(11); double value = 0.0; if (double.TryParse(strWeight.Replace(" ", ""), System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value)) { sr.WeightValue = value; sr.WeightEvent = DateTime.Now; if (strUnit.Equals("ct ")) { sr.bValueStable = true; sr.unit = scaleResult.Unit.Carats; } else if (strUnit.Equals("g ")) { sr.bValueStable = true; sr.unit = scaleResult.Unit.Grams; } else { sr.bValueStable = false; sr.unit = scaleResult.Unit.None; } return(sr); } else { return(null); } }
/*public string getSerial() * { * lastCmd = Command.GetSerial; * lastSerialRead = null; * byte[] buffer; * buffer = new byte[4]; * buffer[0] = 0x1B; * buffer[1] = 0x78; * buffer[2] = 0x32; * buffer[3] = 0x5F; * sendMessage(buffer, buffer.Length); * System.Threading.Thread.Sleep(250); * return lastSerialRead; * } * public string getModel() * { * lastCmd = Command.GetModel; * lastModelRead = null; * byte[] buffer; * buffer = new byte[4]; * buffer[0] = 0x1B; * buffer[1] = 0x78; * buffer[2] = 0x31; * buffer[3] = 0x5F; * sendMessage(buffer, buffer.Length); * System.Threading.Thread.Sleep(250); * return lastModelRead; * } * * public void setTare() * { * lastCmd = Command.SetTare; * byte[] buffer; * buffer = new byte[2]; * buffer[0] = 0x1B; * buffer[1] = 0x54; * sendMessage(buffer, buffer.Length); * }*/ private void OnDataReceived(object sender, SerialDataReceivedEventArgs e) { while (true) { if (!serialPort.IsOpen) { return; } inboundBuffer += serialPort.ReadExisting(); int frameEnd = inboundBuffer.IndexOf(CHAR_EndOfFrameHF, 0); if (frameEnd < 0) { break; } // A full message is available. string message = inboundBuffer.Substring(0, frameEnd - 1); inboundBuffer = inboundBuffer.Substring(frameEnd + 1); dataRead = message; if (lastCmd == Command.GetWeight) { lastScaledWeight = getScaleInfo(dataRead); } else if (lastCmd == Command.GetSerial) { lastSerialRead = dataRead.TrimStart().Substring(0, 8); } else if (lastCmd == Command.GetModel) { lastModelRead = dataRead; } if (NotifyWeightEvent != null) { NotifyWeightEvent(this, deviceSerial, dataRead); } } }