Reputation: 19
I have code reading from serial port randomly, and I want get specific data in specific locations, but the port sending the data line by line continuously, like that
H91F O69F S124F D-1F H94F O70F S119F D-2F H95F O69F S119F D0F H90F O70F S122F D2F H91F O69F S123F D3F H93F O69F S122F D5F H94F O71F S122F D2F H92F O69F S120F D1F H92F O69F S121F D1F H91F O69F S122F D1F H91F O70F S120F D-2F H95F O73F S121F D-4F H90F O69F S124F D-2F H94F O72F S123F D-3F H90F O70F S119F D-5F H94F O72F S122F D-6F H95F O69F S122F D-9F H90F O69F S123F D-11F
send line by line from H
to last F
H91F O69F S124F D-1F
then another line H94F O70F S119F D-2F
I need to get the number between each letters like between H
and F
and between O
and F
, the numbers
SerialPort serialPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
serialPort.Open();
string datasize = serialPort.ReadTo("D");
string firstdata = "";
firstdata = serialPort.ReadExisting();
string data = "";
int firsth = 0;
int firstd = 0;
int lastf = 0;
firsth = firstdata.IndexOf("H");
firstd = firstdata.IndexOf("D" , startIndex: firsth);
lastf = firstdata.IndexOf("F" , startIndex: firstd);
data = firstdata.Substring(firsth, lastf + 1);
// MessageBox.Show(data);
int h = data.IndexOf("H");
int firstF = data.IndexOf("F" , startIndex: h);
string hdata = data.Substring(h + 1, firstF - h - 1);
// string hstring = Regex.Match(hdata, @"\d+").Value;
PulseBox.Text = hdata;
int o = data.IndexOf("O");
int secondF = data.IndexOf("F", startIndex: o);
string odata = data.Substring(o + 1, secondF - o - 1);
// string ostring = Regex.Match(odata, @"\d+").Value;
OxyBox.Text = odata;
int s = data.IndexOf("S");
int thirdF = data.IndexOf("F" , startIndex: s);
string sdata = data.Substring(s + 1, thirdF - s - 1);
// string sstring = Regex.Match(sdata, @"\d+").Value;
SystolicPressure.Text = sdata;
int d = data.IndexOf("D");
int fourthF = data.IndexOf("F", startIndex: d);
string ddata = data.Substring(d + 1, fourthF - d - 1);
// string dstring = Regex.Match(data, @"\d+").Value;
DiastolicPressure.Text = ddata;
Upvotes: 0
Views: 270
Reputation: 4545
As you are processing a fixed pattern, you should use a regular expression (see https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference for a reference to regular expressions). In your case this would be:
var regex = new Regex(@"H(?<H>[\-\d]+)F O(?<O>[\-\d]+)F S(?<S>[\-\d]+)F D(?<D>[\-\d]+)F");
var input = serialPort.ReadExisting(); // e.g. "H91F O69F S124F D-1F H94F O70F S119F D-2F H95F O69F S119F D0F H90F O70F S122F D2F H91F O69F S123F D3F H93F O69F S122F D5F H94F O71F S122F D2F H92F O69F S120F D1F H92F O69F S121F D1F H91F O69F S122F D1F H91F O70F S120F D-2F H95F O73F S121F D-4F H90F O69F S124F D-2F H94F O72F S123F D-3F H90F O70F S119F D-5F H94F O72F S122F D-6F H95F O69F S122F D-9F H90F O69F S123F D-11F";
If you then apply this regex to your input, it will return all matches:
var matches = regex.Matches(input);
foreach (Match match in matches)
{
// match.Value will be your complete match, e.g. "H91F O69F S124F D-1F"
// match.Groups["H"].Value will contain the value for H, e.g. "91"
// match.Groups["O"].Value will contain the value for O, e.g. "69"
// match.Groups["S"].Value will contain the value for S, e.g. "124"
// match.Groups["D"].Value will contain the value for D, e.g. "-1"
}
Upvotes: 1