开发者

Can we put callback function inside dll?

开发者 https://www.devze.com 2023-01-29 14:06 出处:网络
I\'m trying to use .Net serialport methods from another program which uses Unity3D engine. I\'ve a c# class library (dll) that register and implement a callback function when the serial port receives

I'm trying to use .Net serialport methods from another program which uses Unity3D engine.

I've a c# class library (dll) that register and implement a callback function when the serial port receives some data.

public CSerialDll()
{         
   _serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);
}

void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
   int bytes = _serial.BytesToRead;
   _comBuffer = new byte[bytes];
   _serial.Read(_comBuffer, 0, bytes);
   Console.WriteLine("data recv: " + System.Text.ASCIIEncoding.ASCII.GetString(_comBuffer));
}

And then in my another program, I create an instance of the class.

I want to know whether my call back serial_DataReceived inside the library (dll) will be called or not. I'm trying to read _comBuffer inside the library from another program and it always empty (even though the data are coming when I tested with a console app inside the s开发者_如何学JAVAame app).

Any help is greatly appreciated.

Thanks


<HUNCH>

If I was a class designer, i would put the data into the callback, so if you look at the SerialDataReceivedEventArgs class, you'll see that inside it is you buffer with the data.

So, after you get an event, and you then call read, you actually discard the data that is sent to you and request more.

In other words, yes, the fact that it is an external library doesn't have anything to do with the fact it isn't working as it should.

</HUNCH>


There's typically nothing particularly special about methods being in a separate DLL, as long as access modifiers are set correctly.

However depending on how you set up your solution it can be a pain making sure you have the latest DLL in your bin folder.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号