これを間違った方法で行っているのは私だけだと確信しています。
onclick が私Form1
の serialConn.cs で呼び出されるメソッドを呼び出すボタンがありますconnect()
。
public static bool connect(string comPort) {
BTserial = new SerialPort(comPort, 9600, Parity.None, 8, StopBits.One);
BTserial.Open();
if (BTserial.IsOpen) {
BTserial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedEvent);
return true;
} else {
return false;
}
}
private static void DataReceivedEvent(object sender, SerialDataReceivedEventArgs e) {
Debug.WriteLine("Data Incomming!");
// Check if Chars are received
if (e.EventType == SerialData.Chars) {
Debug.WriteLine("Chars!");
// Create new buffer
byte[] ReadBuffer = new byte[BTserial.BytesToRead];
// Read bytes from buffer
BTserial.Read(ReadBuffer, 0, ReadBuffer.Length);
BTserial.DiscardInBuffer();
// Encode to string
string data = bytesToString(ReadBuffer);
ReadBuffer = null;
data = null;
}
}
そして、それはすべて良いですが、データが受信されたら、それを私のコントローラーにTextBox
印刷したいForm1
.. では、これにどのようにアプローチするのが最善の方法でしょうか?DataReceivedEvent()
static