如何对来自 SerialPort 的传入数据的行进行编号?
How to number the rows of incoming data from SerialPort?
我正在从串行端口读取数据并保存在文本文件中。
保存在文本文件中后,我的数据如下所示:
150101 05:01:29,4 0030;0000;00;00;00;00;0000;0000;80;10;E008
150101 05:01:29,5 0030;0000;00;00;00;00;0000;0000;80;10;E008
150101 05:01:29,6 0030;0000;00;00;00;00;0000;0000;00;10;E008
我想对保存的文本文件中的行进行编号,如下所示:
150101 05:01:29,4 0030;0000;00;00;00;00;0000;0000;80;10;E008;1
150101 05:01:29,5 0030;0000;00;00;00;00;0000;0000;80;10;E008;2
150101 05:01:29,6 0030;0000;00;00;00;00;0000;0000;00;10;E008,3
我的代码如下:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadLine();
this.Invoke(new EventHandler(DisplayText));
AppendToFile(RxString);
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString + Environment.NewLine);
}
private void AppendToFile(string toAppend)
{
string myFilePath = @"C: \Users\Glenn\Desktop\data1.txt";
File.AppendAllText(myFilePath, toAppend + Environment.NewLine);
}
有人可以帮忙吗?
您可以维护一个 class 级行计数器:
private int _currentRowNum = 1;
然后您将在 AppendToFile
方法中执行此操作:
File.AppendAllLines(myFilePath, new string[] {string.Format("{0};{1}", toAppend, _currentRowNum.ToString())});
_currentRowNum++;
这可能是simple.Here我得到了所有行;在末尾添加数字并写入新文件。
string[] lines = File.ReadAllLines("../../Data/test.txt", Encoding.UTF8);
List<string> list = new List<string>();
int lineCount=1;
foreach(string line in lines)
{
list.Add(line + ";" + lineCount++);
}
File.WriteAllLines("../../Data/test.txt", list.ToArray());
让我们好好看看 class 您发布的内容,以及您需要添加的内容以获得行号:
public class SerialPortReader
{
private const _filePath = @"C:\Users\Glenn\Desktop\data1.txt";
private Int32 _lineNumber = 1;
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadLine();
this.Invoke(new EventHandler(DisplayText));
AppendToFile(RxString);
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString + Environment.NewLine);
}
private void AppendToFile(string toAppend)
{
var line = String.Format("{0};{1}\n", toAppend, _lineNumber);
File.AppendAllText(_filePath, line);
_lineNumber++;
}
}
我正在从串行端口读取数据并保存在文本文件中。 保存在文本文件中后,我的数据如下所示:
150101 05:01:29,4 0030;0000;00;00;00;00;0000;0000;80;10;E008
150101 05:01:29,5 0030;0000;00;00;00;00;0000;0000;80;10;E008
150101 05:01:29,6 0030;0000;00;00;00;00;0000;0000;00;10;E008
我想对保存的文本文件中的行进行编号,如下所示:
150101 05:01:29,4 0030;0000;00;00;00;00;0000;0000;80;10;E008;1
150101 05:01:29,5 0030;0000;00;00;00;00;0000;0000;80;10;E008;2
150101 05:01:29,6 0030;0000;00;00;00;00;0000;0000;00;10;E008,3
我的代码如下:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadLine();
this.Invoke(new EventHandler(DisplayText));
AppendToFile(RxString);
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString + Environment.NewLine);
}
private void AppendToFile(string toAppend)
{
string myFilePath = @"C: \Users\Glenn\Desktop\data1.txt";
File.AppendAllText(myFilePath, toAppend + Environment.NewLine);
}
有人可以帮忙吗?
您可以维护一个 class 级行计数器:
private int _currentRowNum = 1;
然后您将在 AppendToFile
方法中执行此操作:
File.AppendAllLines(myFilePath, new string[] {string.Format("{0};{1}", toAppend, _currentRowNum.ToString())});
_currentRowNum++;
这可能是simple.Here我得到了所有行;在末尾添加数字并写入新文件。
string[] lines = File.ReadAllLines("../../Data/test.txt", Encoding.UTF8);
List<string> list = new List<string>();
int lineCount=1;
foreach(string line in lines)
{
list.Add(line + ";" + lineCount++);
}
File.WriteAllLines("../../Data/test.txt", list.ToArray());
让我们好好看看 class 您发布的内容,以及您需要添加的内容以获得行号:
public class SerialPortReader
{
private const _filePath = @"C:\Users\Glenn\Desktop\data1.txt";
private Int32 _lineNumber = 1;
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadLine();
this.Invoke(new EventHandler(DisplayText));
AppendToFile(RxString);
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString + Environment.NewLine);
}
private void AppendToFile(string toAppend)
{
var line = String.Format("{0};{1}\n", toAppend, _lineNumber);
File.AppendAllText(_filePath, line);
_lineNumber++;
}
}