namespace ProjectOne
{
public partial class MainForm : Form
{
private void StatusBtn_Click(object sender, EventArgs e)
{
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:
int LineCounter= 0;
public void SerialPortInApp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
DataToSetandGet = serialPortForApp.ReadExisting();
if (this.InvokeRequired)
{
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