缺少时间戳

时间:2015-06-03 09:27:07

标签: c# time serial-port

我正在尝试显示文本框中收到的数据。但我意识到,当在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();
    }

}

}

1 个答案:

答案 0 :(得分:0)

基本上,我错了,说时间戳最初包含在我收到的数据中。我需要在我自己的时间戳中添加..这是作为我何时收到的记录以下数据。

结果,这个

缺少了什么
string time = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.ff")

希望这也可以帮助那些正在努力解决问题的人。