namespace ProjectOne
{
public partial class MainForm : Form
{
private void StatusBtn_Click(object sender, EventArgs e)
{
//command sent to serial port and responds a series of lines
string StatusInfoToshow = "?D\r";
string commForMeter = string.Format(StatusInfoToshow);
try
{
if (statusofMeter.serialPortForApp.IsOpen)
{
bool doesContain = commForMeter.Contains("\r");
if (doesContain)
{
foreach (char s in commForMeter)
{
currentDataToShow.serialPortForApp.Write(s.ToString());
}
}
}
}
catch (Exception)
{
statusofMeter.ShowDataInScreenTxtb.Text = "TimeOUT Exception";
}
}
int LineCounter= 0;
public void SerialPortInApp_DataReceived(object
sender,SerialDataReceivedEventArgs
e)
{
DataToSetandGet = serialPortForApp.ReadExisting();
int stratstring = 3;
int endstring = DataToSetandGet.LastIndexOf('\r');
int lenghtsub = endstring - stratstring;
string clash = DataToSetandGet.Substring(stratstring, lenghtsub );
ShowDataInScreenTxtb.AppendText(clash.ToString().Replace("\n", "
").Replace("\r", "\r\n"));
//I have this error
//System.ArgumentOutOfRangeException: 'startIndex cannot be larger than
// length of string.Parameter name: startIndex'
}
}
}
if I implement this:
int LineCounter= 0;
public void SerialPortInApp_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
try
{
DataToSetandGet = serialPortForApp.ReadExisting();
if (this.InvokeRequired)
{
//I insert the the increment numbering index for each new line displayed
//in the textbox but index start at 0 not 1, if I set LineCounter=1 it
//print 1 instead of 0 and the 1 again for next line
Invoke(new MethodInvoker(delegate {
Textbox_One.AppendText(LineCounter.ToString() +
DataToSetandGet.Replace("\n", " ").Replace("\r", "\r\n"));
}));
}
catch (Exception)
{
Textbox_One.Text = "TimeOUT error";
}
}
my button send a command to be listened in serial port and send an answer. the responds in serial port will show records read from a sensor, and each line(readings from sensor in device connected to port) always start with a zero "0" string character at the beginning of each line. I would like to Remove() or Substring() that zero "0" when it displays each line.
//I cannot use Substring()
LineCounter.ToString()+ DataToSetandGet.Substring(n position in string)
because give me an error
//'startIndex cannot be larger than length of string:
//this error is because it reads like:
//0 record\r//0 record\r//0 record\r//0 record\r//0 record
Invoke(new MethodInvoker(delegate {
Textbox1.AppendText(LineCounter.ToString()+
DataToSetandGet.Replace("\n", " ").Replace("\r", "\r\n"));
}));
The "\r" carriage return is because the command
//before using LineCounter it displays
//0 record
//0 record
//0 record
//0 record
//0 record
after using counter it displays:How to position index to start at 1 instead of 0 and next line as one
//1 0 record <==if LineCounter=1 but if LineCounter=0 it set first line
//1 0 record
//2 0 record
//3 0 record
//4 0 record
correct display should be like: How to Implement Substring() or Remove()
//1 record
//2 record
//3 record
//4 record
//5 record