CSharp - 时间戳丢失

  显示原文与译文双语对照的内容

我正在尝试显示在文本框中收到的数据。 但是我意识到在Br@y终端上接收数据时,它看起来很不错( 比如。 14: 02: 33 。43> T 11 22.32 ) 但在软件上运行时间戳丢失。

我是不是漏掉了什么导致这一切?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace SerialCom
{
public partial class Form1 : Form
{
 string RxString;//Variable

 public Form1()
 {
 InitializeComponent();
 }


 private void btnStart_Click(object sender, EventArgs e)
 {
 serialPort1.PortName ="COM4";
 serialPort1.BaudRate = 9600;

 serialPort1.Open();
 if (serialPort1.IsOpen)
 {
 btnStart.Enabled = false;
 btnStop.Enabled = true;
 txtData.ReadOnly = false;
 }

 }

 private void btnStop_Click(object sender, EventArgs e)
 {
 if (serialPort1.IsOpen)
 {
 serialPort1.Close();
 btnStart.Enabled = true;
 btnStop.Enabled = false;
 txtData.ReadOnly = true;
 }

 }

 private void txtData_KeyPress(object sender, KeyPressEventArgs e)
 {

 if (!serialPort1.IsOpen) return;//If the port is closed, don't try to send a character.
 char[] buff = new char[8];//If the port is Open, declare a char[] array with one element.
 buff[0] = e.KeyChar;//Load element 0 with the key character.
 serialPort1.Write(buff, 0, 1);//Send the one character buffer.
 e.Handled = true;//Set the KeyPress event as handled so the character won't
//display locally. If you want it to display, omit the next line.
 }

 private void DisplayText(object sender, EventArgs e)
 {
 txtData.AppendText(RxString);
 }

 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
 {
 RxString = serialPort1.ReadExisting();
 this.Invoke(new EventHandler(DisplayText));

 StreamWriter MyStreamWriter = new StreamWriter(@"c:testing.txt",true);//True tell SW to append to file instead of overwriting
 MyStreamWriter.Write(RxString);
 MyStreamWriter.Flush();
 MyStreamWriter.Close();
 }

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
 if (serialPort1.IsOpen)
 serialPort1.Close();
 }

}

}

时间: 原作者:

基本上,我认为时间戳最初包括在我收到的数据中是错误的。 我需要在我自己的时间里添加时间戳。 这是作为我实际收到以下数据的记录的一个记录。

因此,缺少的是


string time = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ff")

希望这能帮助那些正在解决问题的人。

原作者:
...