时间戳丢失

本文关键字:时间戳 | 更新日期: 2023-09-27 17:53:03

我试图在文本框上显示收到的数据。但是我意识到,当数据在Br@y终端上接收时,它看起来很好(例如:14:02:33.43> T 1122.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")

希望这篇文章也能帮助到那些正在与这个问题作斗争的人。