单独的类我正在使用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();
}
没有。
有什么我做错了吗?
2013-02-21
Liban
+0
不要捕获'SystemException'。相反,赶上'例外'。另外,如果你想真正知道发生了什么,显示'ex.ToString()' –
2013-02-21 14:56:36
+0
你不会从* new * SerialPort对象读取太多内容。将* sender *发送到SerialPort以使用实际生成事件的实例。 –
2013-02-21 15:06:43
635

被折叠的 条评论
为什么被折叠?



