Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
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();
     if (this.InvokeRequired)
     {
      Invoke(new MethodInvoker(delegate {
        Textbox1.AppendText(LineCounter.ToString()+ DataToSetandGet
                                                     .Replace("\n", " ")
                                                     .Replace("\r", "\r\n"));
           }));
     }

  }
 }
}


What I have tried:

C#
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 .

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

befor 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
//1 0 record <==if LineCounter=1 but if LineCounter=0 it set first line as zero
//1 0 record and next line as one
//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
Posted
Updated 14-Aug-18 13:50pm
v7

private int _counter = 0;

...

Textbox_One.AppendText( ++_counter.ToString() + DataToSetandGet.Substring(1).Replace("\r", "\r\n")); 
 
Share this answer
 
Comments
JimB_ 9-Aug-18 19:01pm    
HI Gerry it definitely work. However, the command that I send it request to read records in the device memory, It is why the respond always start with zero. and also I have this error 'startIndex cannot be larger than length of string'. Now I have to find the way how to remove that zero because now the response looks like this:
0 0 record
1 0 record
2 0 record
3 0 record
4 0 record
5 0 record

please I been reading and it looks like I have to check for what is in DataToSetandGet then IndexOf("\r", i)) != -1) apply the DataToSetandGet.Substring(1) and finally invoke the answer Textbox_One.AppendText( ++_counter.ToString()+ DataToSetandGet.Replace("\r", "\r\n"))

thank you If any more answers I will appreciate
[no name] 11-Aug-18 20:01pm    
The substring(1) ships over the "0", but you need to insure there is data in the buffer in the first place.
Sorry but I do not understand your problem.

It is unknown what happens here:
Quote:
currentDataToShow.serialPortForApp.Write( list2 )

If I ignore the unknown and output list2 instead I get something like this:
Quote:
1 ?
2 D
3

But the question is do you want to write this to currentDataToShow.serialPortForApp.Write( list2 )?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900