C#,VB6和COM端口交互 [英] C#, VB6, and COM-port interaction

查看:106
本文介绍了C#,VB6和COM端口交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我有一个VB6应用程序,通过COMClass使用.NET代码从串口读取。为此,我使用.NET SerialPort类。当触发SerialPort.Datareceived事件时,我引发了我的VB6应用程序订阅的无事件事件。



这一切都在开发过程中完美运行。当我在本地计算机上执行我的application.exe时,它也正在工作。当我在另一台机器上执行application.exe时,我得到一个System.Reflection.TargetException,说对象与目标类型不匹配



Microsoft声明SerialPort.Datareceived事件是在另一个线程上引发的,我相信这可能是问题所在。你可以在这里阅读:http://msdn.microsoft.com/da-dk/library/system.io.ports.serialport.datareceived.aspx



到现在为止我认为我不能使用基于事件的方法,同时在我的VB6应用程序中保持响应式UI,但除了从我的VB6应用程序中轮询一些共享存储之外还有哪些替代方案?



非常感谢任何建议。

Hi everybody.

I have a VB6 application that uses .NET code through a COMClass to read from the serial port. For this I use the .NET SerialPort class. When the SerialPort.Datareceived event is triggered I raise an paraemterless event that my VB6 application subscribes to.

All this is working perfectly during development. It is also working when I execute my application.exe on my local machine. When I execute the application.exe on another machine I get a System.Reflection.TargetException saying "object does not match target type"

Microsoft states that the SerialPort.Datareceived event is raised on another thread and I believe this could be the problem. You can read about it here: http://msdn.microsoft.com/da-dk/library/system.io.ports.serialport.datareceived.aspx

By now Im thinking that I cannot use an event-based approach and at the same time keep a responsive UI in my VB6 application, but what alternatives are there besides polling some shared storage from my VB6 application?

Any suggestions are greatly appreciated.

推荐答案

您只能在构建的机器上运行应用程序,因为VS会自动注册构建中的组件。

您可以检查:项目属性 - 构建选项卡 - 注册COM Interop - 如果已经选中,则选择comp。在每个版本上注册。



为了能够在其他机器上运行应用程序,您需要使用regasm.exe手动注册组件或创建安装程序会为你做这件事。

在命令行上使用:

regasm.exe -tlb -codebase dllname.dll



regasm.exe位于您的.NET安装文件夹中(通常为C:\ Windows \ Microsoftoft.NET \ Framework \ {.NET version}
You''re able to run the app only on the machine that builds because VS automatically registers component on build.
You can check: Project Properties - Build Tab - "Register for COM Interop" - if ti''s checked then comp. is registered on every build.

To be able to run the app on other machines you need to use "regasm.exe" to manually register the component or create installer which will do that for you.
On command line use:
regasm.exe -tlb -codebase dllname.dll

regasm.exe is in your .NET install folder (usually C:\Windows\Microsoft.NET\Framework\{.NET version}


这篇关于C#,VB6和COM端口交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆