Click here to Skip to main content
15,889,784 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys,

I am working in windows application. We use barcode reader to login the customers. Current architect of application is in each form SerialPoet Class object is created and from DataRecieve Event data is received. Previous Developer was not able to make a singleton class SerialPort and give access data in every form. He created object of serialport class in every form to access the serialport data. Now, I want to change the architecture of the application.I want to create a single class which create object of serial port and this object provides data to current Active form.So, I created Single Class SerialPortCommunication in which I create object of SerialPort class and I Call the Serialport.open() method but I am getting error when I am calling the method "Exception has been thrown by the target of an invocation". Please give me basic idea how I can I make best of architecture.

Above question might be tricky or not easily understandable .please free to ask me questions.
Posted
Comments
DJPJVS 6-May-14 8:44am    
So to summarise...
You are rewriting Architecture of a existing system in Windows Forms.
You want to get advice on how to approach this challenge?
You need to understand what your exception says?
What is it you need help with?
RAHUL(10217975) 6-May-14 8:49am    
Yes, I dont want to create SerialPort Class object in each form..just one SerialPort class and that should provide the serial port data to current Active form
[no name] 14-May-14 12:48pm    
Most probably this will be your fast and dirty solution: Use Invoke to send the data to the forms.

We can't answer that directly: you have the code and we don't!
But...start by looking at the actual exception object: specifically, look at the Inner Exception - this normally contains details of the actual exception, which should help you narrow down the cause.

Sorry - but at the moment, that and the debugger are your best friends!
 
Share this answer
 
Your static class will look someting like this

C#
public class SerialPortClass
 {
     private static SerialPortClass instance;

     public static SerialPortClass GetInstance()
     {
         return instance ?? new SerialPortClass();
     }

     SerialPort port;
     //Rest of your serial port class members and methods here....

     public void SendData(byte[] data)
     {
         port.Write(data,0,data.Count());
     }

     protected void Connect()
     {
         port = new SerialPort("COM1");
         //portsettings
         //port.BaudRate = 1024;
     }
 }


Your implementation or calls inside the other forms will look like

C#
SerialPortClass.GetInstance().Write(data);
 
Share this answer
 
Comments
Mohammad Reza Valadkhani 17-May-14 1:22am    
Replace
SerialPortClass.GetInstance().Write(data);
With
SerialPortClass.GetInstance().SendData(data);
The obvious issue with having multiple forms accessing a any sort of serial class or even device is working out how to deal with who is handling the data.

I mean the serial object whatever it looks like holds data does the first form to ask for it get it or are the forms expected to callback and say ok next data I have handled that one. Then you have the issue of partial data somehow the serial stream must have data blocks of some form with end of data markers and the forms are obviously needing to parse that data with a complete block not a half block.

That leads me to the obvious conclusion none of the ideas above will really work well.

It would appear to me you are trying to make a message queue which for all purposes looks very much like a standard windows queue and that would give you a good clue to do this in a very elegant way.

1.) Design your own message queue system
2.) Attach your own private message into the standard windows handler

Personally I would go for option 2 the windows message handler is well behaved and you know how it will behave so all you do is make your own private WM command and say for example attach the parsed data to the LPARAM. Microsoft makes this easy they expect you to do it.

WM_MYPRIVATEMSG (WM_USER + 0x0001) 


You use standard windows PostMessage and SendMessage commands to post them up to a specific form or system wide if you want simply based on window handle. You can even Register the message using RegisterWindowMessage if you really want and make the serial object as it's own thread or application.

The point is it stays with one serial object playing around with the serial port and the forms just handle a new WM_MYPRIVATEMSG which will be posted out in usual way.

Hell you could get really fancy and send message back to the serial object by using the same technique if you need to control it.

What all that does it makes the whole the structure portable and consistent and will be supportable over time because there are many many applications that work that way and it is the recommended way to do it.
 
Share this answer
 
v3
if i were you i would write windows Service for handling serial port and then use some key points for communication between windows form and windows service application.
but for your case it`s better to write up your code then guys can help you.
 
Share this answer
 
I made SerialPort reader into a separate static class. Then I created a datareceive event in that class and fire it every time data is received. And that event I subscribe in each form and now when I scan the card datarecived event is firing in every form.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900