我很确定只有我做错了。
我Form1
有一个按钮,onclick 调用我的 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
(我想我必须是?),我无法访问任何东西?那么解决这个问题的最佳方法是什么?