1
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
JimB
  • 11
  • 5
  • 1
    You do not need, and shouldn't put [tags in your question title](https://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles). The tagging system is more than adequate (the little blue things at the bottom of your question). Also see the help on [tagging](https://stackoverflow.com/help/tagging): "The only time you should use tags in your title is when they are organic to the conversational tone of the title." (it includes a list of formats you shouldn't use if you include tags in your title, which includes [tag] [question title]) – ProgrammingLlama Aug 15 '18 at 00:53
  • 1
    I've added an answer for you. In future, I recommend creating an [mcve]. Your question has a lot of secondary information which isn't relevant to your question. – ProgrammingLlama Aug 15 '18 at 01:08

1 Answers1

1

It seems that your question is simply asking how to use Substring. You've stated that you receive the following error:

startIndex cannot be larger than length of string

This is telling you that in str.Substring(idx), idx >= str.Length.

To resolve this you can simply perform the following evaluation:

LineCounter.ToString() + (DataToSetandGet.Length > n ? DataToSetandGet.Substring(n) : string.Empty);

Which will use Substring where the length of the string permits, or otherwise return an empty string. Or you can use some other evaluation to ensure that idx isn't more than the length of the string - 1 (indexes are 0-based).

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    @JimB Please see [here](https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the). It's because you're trying to update a UI component from a different thread to the UI thread. – ProgrammingLlama Aug 15 '18 at 05:59
  • @JimB Not sure what's wrong there. I recommend asking a new question about it - it's been some time since I worked with WinForms I'm afraid. – ProgrammingLlama Aug 16 '18 at 00:27
  • I recommend asking a new question. It really isn't related to this question and it's not my area of expertise. – ProgrammingLlama Aug 16 '18 at 04:14