Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all,

i created a simple GUI which communicate serial port, if the project contains single web form it read the text and gives the output (loop back)and executed perfectly , but the problem is when i have multiple forms in my project i am unable to connect the port. (EX: in the first page i have some buttons if we click on button it goes to next page and in that page i want to communicate the port). how can i communicate serail port in further pages

thanks in advance

What I have tried:

i tried put a serial connector for every form and but it is not connected
Posted
Updated 31-Jan-17 2:51am
Comments
CHill60 31-Jan-17 8:14am    
Without seeing any of your code I couldn't even start to guess
PIEBALDconsult 31-Jan-17 8:25am    
That doesn't help. Please put the relevant parts (not the whole thing) in your question -- Improve question.
[no name] 31-Jan-17 8:14am    
You use the same object to communicate no matter what form is being displayed.
PIEBALDconsult 31-Jan-17 8:25am    
You should not have the communication code within your GUI code. It should be in its own class. Instantiate one object of that class and allow each Form to use that one instance.
Member 12941572 31-Jan-17 8:52am    
PROBLEM SOLVED THANK YOU ALL

Once you open a SerialPort, it is locked and you can't open a second instance on the same hardware - only one "thing" at a time can control the port. So create it in the "outermost" of your forms, and pass the instance of the SerialPort (or its encapsulating class if you follow PIEBALDconsult's directions) to each of the forms as it is created - the simplest way to do that is to use a constructor that accepts an instance and pass it when you create the form. (You can use a private constructor that takes no parameters to keep the designer happy but ensure that the form can't be instantiated in your app without supplying an instance).
 
Share this answer
 
The problem is that your form instance is holding the serial port open. Only ONE process and ONE SerialPort instance can have a port open at any one time. So, the first instance of your Form1 gets the serial port. If it doesn't close the port, every new instance will not be able to open the port. It's already open by the first instance of your form.

It's a really bad idea to have the serial port code in your form code. It should be in it's own class, managing the connection to the serial port and exposing methods so that any caller (your form instances) can tell it to send data or get notifications from it that data was received.
 
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