html表单提交与串口,串口数据收到事件从表单

单独的类我正在使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值