我们可以将回调函数放在dll中吗?

发布于 2024-10-06 08:43:16 字数 695 浏览 4 评论 0原文

我正在尝试使用另一个使用 Unity3D 引擎的程序中的 .Net 串行端口方法。

我有 ac# 类库(dll),当串口接收到一些数据时,它会注册并实现回调函数。

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));
}

然后在我的另一个程序中,我创建了该类的一个实例。

我想知道库(dll)内的回调serial_DataReceived是否会被调用。我试图从另一个程序读取库内的 _comBuffer ,但它始终为空(即使当我在同一应用程序内使用控制台应用程序进行测试时数据正在到来)。

非常感谢任何帮助。

谢谢

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 same app).

Any help is greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挥剑断情 2024-10-13 08:43:16

如果我是类设计者,我会将数据放入回调中,因此如果您查看 SerialDataReceivedEventArgs 类,您会在其中看到它你正在缓冲数据吗?

因此,在收到事件并调用 read 后,您实际上会丢弃发送给您的数据并请求更多数据。

换句话说,是的,它是一个外部库这一事实与它没有按预期工作的事实没有任何关系。

<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>

倦话 2024-10-13 08:43:16

只要正确设置访问修饰符,方法位于单独的 DLL 中通常没有什么特别之处。

但是,根据您设置解决方案的方式,确保 bin 文件夹中具有最新的 DLL 可能会很麻烦。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文