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.IO.Ports;
using System.Diagnostics;
namespace klokske
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (!mySerialPort.IsOpen)
{
mySerialPort.Open();
rtRX.Text = "Port Opened";
}
else
rtRX.Text = "Port busy";
}
DateTime dateTime;
private void AnalogClock_Load(object sender, System.EventArgs e)
{
dateTime = DateTime.Parse(myData);
}
private string myData;
private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
myData = mySerialPort.ReadExisting();
this.Invoke(new EventHandler(displayText));
}
private void displayText(object o, EventArgs e)
{
rtRX.AppendText(myData);
}
}
}
として、ReadExisting()
のみ受信バッファに現在あるものを返します。 DataReceived
イベントはランダムに発生する可能性があります。このイベントが発生している場合は、まだ探している文字がすべてない場合があります。メッセージ全体を表示してからテキストを表示するまで、文字列を作成する必要があります。
char ESC = (char)27;
char CR = (char)13;
char LF = (char)10;
StringBuilder sb = new StringBuilder();
//in my case, the data im expected is ended with a Line Feed (LF)
//so I'll key on LF before I send my message
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string Data = serialPort1.ReadExisting();
foreach (char c in Data)
{
if (c == LF)
{
sb.Append(c);
this.Invoke(new EventHandler(sb.toString()));
}
else
{
//else, we append the char to our string that we are building
sb.Append(c);
}
}
}
[mcve](**はあなたの持つコードではありません)を入力し、**具体的な**質問をお願いします。現在、あなたが実際に何を求めているのかは言うまでもありません。あなたの特定の問題はどこですか? – dymanoid
おそらく[DateTime.ParseExact](https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx)を使用しますか? – Fildor
誰もReadExisting()がどのように動作するかを理解していません。奇妙なことに、それは素晴らしい名前を持っています。受信バッファ内に*存在する*を返します。それは決して正確には「00:10:23」ではありません。通常は1〜2文字が得られますが、シリアルポートは遅いです。それらを数えることは必須で、いつもRead()で最もうまくいきます。 –