using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; using System.IO; using System.IO.Ports; namespace WindowsFormsApplication1 { public partial class ReadCOMportData : Form { public ReadCOMportData() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { string[] scaleval = new string[] { }; string[] tempScaless = new string [20]; string word = "", tempScale = ""; decimal _scalebalance = 0 ; for (int i = 1; i <= 20; i++) { this.serialPort1.Open(); String data = this.serialPort1.ReadLine(); int buff = this.serialPort1.ReadByte(); string x = Regex.Replace(data, @"[^0-9]+", "#"); char[] delimiter1 = new char[] { '#' }; // <-- Split on these // ... Use StringSplitOptions.None. scaleval = x.Split(delimiter1); word += i + "--> " + ":" + buff + " : " + String.Join(Environment.NewLine, scaleval[2]) + Environment.NewLine; tempScaless[i-1] = scaleval[2]; //string[] myStrings = ;// ... this.resTxt.Text = word; //if (i == 20) //{ // IGrouping max2 = tempScaless.GroupBy(n => n) // .OrderByDescending(g => g.Count()) // .First(); // _scalebalance = Convert.ToDecimal(max2.Key); //} this.serialPort1.Close(); } IGrouping max2 = tempScaless.GroupBy(n => n) .OrderByDescending(g => g.Count()) .First(); _scalebalance = Convert.ToDecimal(max2.Key); lbShow.Text = _scalebalance.ToString("#,##0.00"); } catch (Exception ex) { this.resTxt.Text = "Error:"+ex.Message; } } private void label1_Click(object sender, EventArgs e) { } private void ReadCOMportData_Load(object sender, EventArgs e) { } private void reqTxt_TextChanged(object sender, EventArgs e) { } private void panel1_Paint(object sender, PaintEventArgs e) { } private decimal Calbalance(decimal valscale) { decimal bal = 0; return bal; } } }