private void ProcessData(string data)
        {
            double weight;

            if (!double.TryParse(data, NumberStyles.Float, new CultureInfo("en-US"), out weight))
            {
                return;
            }

            WeightReceived?.Invoke(new WeightReceivedEventArgs(weight));
        }
예제 #2
0
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                weight = SerialPort.ReadExisting();
                weight = weight.Substring(0, 7);

                WeightReceived?.Invoke(weight, new EventArgs());
            }
            catch (TimeoutException) { }
        }
예제 #3
0
        private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                if (e.EventType == SerialData.Chars && this.Port.BytesToRead > 16)
                {
                    string str = this.Port.ReadExisting();
                    LogStringObject?.Invoke("DataReceived", str);
                    if (str.StartsWith("ST"))
                    {
                        var decGr = Convert.ToDecimal(str.Substring(4, 8));
                        WeightReceived?.Invoke(new WeightReceivedEventArgs(decGr / 1000));
                        LogStringObject?.Invoke("WeightReceivedEventArgs",
                                                (decGr / 1000).ToString(CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        if (!str.Contains("ST"))
                        {
                            return;
                        }
                        str = leftRotateShift(str, str.IndexOf("ST", StringComparison.Ordinal));
                        var decGr = Convert.ToDecimal(str.Substring(4, 8));
                        WeightReceived?.Invoke(new WeightReceivedEventArgs(decGr / 1000));
                        LogStringObject?.Invoke("WeightReceivedEventArgs",
                                                (decGr / 1000).ToString(CultureInfo.InvariantCulture));
                    }
                }

                //ST,+00000.00  g
            }
            catch (System.Exception ex)
            {
                LogInternalException?.Invoke("Port_DataReceived", ex);
            }
        }