Windowsアプリケーションフォームを使用して、シリアルポートからデータを受信しています。フォーム内でSerialportDataReceived
イベントを発生させることができます。しかし、私が欲しいのは、シリアルポートイベントを別のクラスに配置し、データをフォームに戻すことです。
eventhandler
シリアルポートの受信データを含むクラスは次のとおりです。
class SerialPortEvent
{
public void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
SerialPort sp = new SerialPort();
//no. of data at the port
int ByteToRead = sp.BytesToRead;
//create array to store buffer data
byte[] inputData = new byte[ByteToRead];
//read the data and store
sp.Read(inputData, 0, ByteToRead);
}
catch (SystemException ex)
{
MessageBox.Show(ex.Message, "Data Received Event");
}
}
}
データを受信したときにこのクラスをフォームにリンクするにはどうすればよいですか?メインプログラムでイベントを開催する必要がありますか、それともフォーム自体でイベントを開催する必要がありますか?
私が今呼び出している方法は、メインで以下のとおりです。
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
SerialPort mySerialPort = new SerialPort("COM81");
SerialPortEvent ReceivedData = new SerialPortEvent();
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(ReceivedData.mySerialPort_DataReceived);
myserialPort.open();
}
シリアルポート受信イベントでは何も受信されません。
私が間違っていることはありますか?