Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB COM serialport
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.
Posted 31-Jan-13 4:00am
Comments
Matthew Faithfull at 31-Jan-13 10:04am
   
I once had a response like that from Microsoft to do with using a 3rd party Socket control years ago. The answer in my case was to declare the VB6 object 'with Events'
If you've already done that then I'm sorry not to be of any help.
flodpanter at 31-Jan-13 10:15am
   
Hi Matthew.
Thanks for your reply. I did declare it like that and it works like a charm. But when I execute the same EXE on a different machine, it throws the exception. Also, I have tried building the EXE on a different machine with the same effect.
Matthew Faithfull at 31-Jan-13 10:41am
   
OK I'm out of serious suggestions I'm afraid VB6 was a loooong time ago. If it works fine from a clean reboot standalone Release build executed by an ordinary i.e. non privelleged user on your machine but not on another, with the same version of the .Net runtime and the same VB runtime then it's down to Windows drivers, serial port hardware, hyperthreading being turned on, the number of cores on the machine, a nasty performance/timitg sensative bug or just back luck. I'm afraid you'll have to take your pick and go from there unless someone else has a better idea. Sergey may be right, it might be easier to redo one half or the other of the problem if its the connection that's the issue. Can you test the low level stuff, closer to the serial port, on it's own to make sure it's working?
Sergey Alexandrovich Kryukov at 31-Jan-13 10:33am
   
If you already use .NET, who needs VB6?
—SA
flodpanter at 1-Feb-13 2:09am
   
:-) nice try, but sadly, the old mothership (VB6) is not easily replaced by a brand new .NET based application. Would probably take several years to to that.
Sergey Alexandrovich Kryukov at 1-Feb-13 2:26am
   
This is not a 'try'... :-)
 
Easily or not, but going "easy" (not migrating far away from VB6 as soon as possible) you certainly doom yourself to disaster. What will you prefer, "easy" or "nowhere"? :-)
 
—SA
flodpanter at 1-Feb-13 2:50am
   
First of all, I would love to migrate. So many things cannot be done in VB6. No question about that. These things seem to be a constant battle between dev and management. Unfortunately "easy" is still the chosen path, but I think were slowly getting there...
Sergey Alexandrovich Kryukov at 1-Feb-13 2:51am
   
Better then nothing.
Good luck. :-)
—SA
sjelen at 31-Jan-13 10:37am
   
I don't think it's threading problem. If it was you would be getting error on development machine too.
If I understood correctly, you made COM component using C# and SerialPort. Then you use that component from VB6?
If so, I guess you forgot to register that component on other machines (or re-register when you changed something) and your app can't load it.
flodpanter at 1-Feb-13 6:22am
   
You could be right. Although everything else is working on other machines. I have not had to register anything on those machines and all other .NET integration works fine on those. The only place the problem shows is when I raise my event from within the Datareceived event.
flodpanter at 1-Feb-13 6:32am
   
Also, I have tried it on both dev machines and "management" machines without the IDE installed. It seems as only the machine that builds the exe can execute it without errors.
glennPattonWork at 31-Jan-13 10:45am
   
Umm if its VB6 why didn't you use MSComm? or did you need fast responce from the port (gathering burst data). The Net 2.0 uses the SerialPort class which is better
as it allows for interupt handling better and faster that the MSComm method so I would an interrupt apporach such as the below for the data recieved event
myComPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived); The unresponsive UI might be freed up with a doevents call, my memmory of VB6 is a little dim now but....
flodpanter at 1-Feb-13 2:14am
   
Hi Glenn, I will look into that. It might get things working.
glennPattonWork at 1-Feb-13 4:14am
   
Also I have just remembered the an old VB6 trick I used to do to free up the UI was to use a 'DoEvents' to free up the multitasking, as I remember it worked on some programs others which used networking (can't remember exactly what) but I found it could alter the way set events were handled (frame ordering mostly). It might work but when "they" start complaining it might be the DoEvents!
Glenn

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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}
  Permalink  
Comments
flodpanter at 6-Feb-13 11:46am
   
Hi Sjelen. In my post build event i do already use regasm to create the tlb-file. This is necessary for any VB6/.NET integration and already works.
sjelen at 8-Feb-13 5:40am
   
Yes and that is why your app works on dev machine - component is registered after the build.
Regasm does more than create .tlb, it also creates registry entries so Windows knows where to find your library.
If you want app to run on other machines you need to run regasm on each machine you want to use.
Or create installer to do that for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 1 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100