Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi..,
now am using 2 forms.these two forms contains serialport control.when loading the first form i opened the serialport1.whenever i got a character'M' from serialport i write character 'A' to this serialport after that i opened the form2 using the code application Application.Run(new Form1()); after that i want to send data to the first form's serialports datareceived event.but when i send data serialport1's data received event doesnt invoke.... what is the problem behind this?? i cant understand..pls help me...
Posted
Comments
Sergey Alexandrovich Kryukov 12-Aug-14 2:08am    
What is this, "serialport control"?
Your problem is that you are doing weird things, instead of explaining what you want to achieve and what is your problem.
First, focus on what to achieve, not on "how".
—SA
Member 10994712 12-Aug-14 2:38am    
i want to receive values from one serialport,and when i receive the value, i want to display another form and after that receive value from same serialport in second form
Sergey Alexandrovich Kryukov 12-Aug-14 2:52am    
Why those two forms? It does not look like nice UI design? Anyway, it does not matter, what form...
Can you receive the values from serial port, did you try? Then use a separate thread for it and Control.Invoke to notify UI thread...
—SA
Member 10994712 12-Aug-14 3:06am    
for making the communication easy i use 2 forms.i tried...but when i show the second form it doesnot involk the datareceived event..
Sergey Alexandrovich Kryukov 12-Aug-14 3:10am    
Again, this forms make no sense, it sounds like. How they can make anything "easy"?
You don't have to use this event. But you can. You did not answer my question.
—SA

Well no, it won't.

Think about it: a serial port connects two machines by a single piece of wire. So only one thing at a time can talk to the machine at the other end, or the remote machine will just get confused because it can be receiving data from two pieces of software at the same time - and it can't tell where they came from because characters don't have a "tag" which says "I came from software A" or "I came from software B". It's a bit like standing in the kitchen while your wife and child both try to tell you what to do at the same time: you can't listen to both of them at the same time!

Add to that the way you are "opening a new form" and things start to get very confused.

Application.Run is not intended for opening new forms: it opens a new application, separate form the existing one, and the code will not return until the new app is finished.

So what you want to do is have one single SerialPort for your application, and have it route the data to the appropriate form.
So Form1 opens and connects the serial port. It receives the "M", writes the "A" and opens a new Form 2:
C#
if (bytes == 'M')
   {
   serialPort1.Write("A");
   Form2 f2 = new Form2();
   f2.Show();
   do
      {
      bytes = serialPort1.ReadByte();
      f2.ProcessByte(bytes);
      } while (bytes != "X");
   f2.Close();

May not be exactly what you want, but it stands a chance of working!
 
Share this answer
 
Comments
Member 10994712 12-Aug-14 4:41am    
I tried this..but when shows second form it doesnot display textboxes,labels in it..i think is hanged at that time
OriginalGriff 12-Aug-14 4:46am    
That's probably a problem with what you are doing in the second form: So have a look at the code in there and see what you have. If you are trying to open a serial port that is almost certainly the reason because the port is already in use!
You only want one port - and then pass the data around.
Sergey Alexandrovich Kryukov 12-Aug-14 11:28am    
Your advise is to synchronously read from a port in a UI thread, a big no-no. No wonder it can "hang". Not a good advice, I must say.
—SA
Herman<T>.Instance 12-Aug-14 14:36pm    
SerialPort uses always it's own thread and is therefore asynchronous
Sergey Alexandrovich Kryukov 12-Aug-14 14:45pm    
Wait a second: serialPort1.ReadByte() is a blocking call; this is all that matters. Try it yourself and you will see. (You don't have to have anything on the other end of your RS-232 cable.)
Also read: "Synchronously reads one byte from the SerialPort input buffer.". Can you see the problem now?
—SA
The serial port communications should be done asynchronously or in a separate thread; I usually recommend a separate thread, which gives you more uniform approach to using different API: still the same thread synchronization. Speaking of which: your problem will be to care about notifying the UI on your data you get from the port. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Comments
Member 10994712 13-Aug-14 6:37am    
I am new to this type of programming..
in my form1
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string s = serialPort1.ReadExisting();

if (s == "M")
{

serialPort1.Write("A");

// serialPort1.Close();
FLAG = 1;

serialPort1.Close();
this.Close();
Form1 f = new Form1();
f.Show();
}
when i run this i got an error that-Cross-thread operation not valid: Control 'Form2' accessed from a thread other than the thread it was created on.

so..please give me a solution for this...please....
Sergey Alexandrovich Kryukov 13-Aug-14 10:48am    
My whole answer is devoted to this cross-thread problem; read it carefully.
It looks like the code shown above is executed on some thread other than UI. You cannot work with form directly; you should do something like
someControl.Invoke(new Action((f) => { f.Show() }), f);
Besides, I advised to use synchronous port communication in a separate thread.
—SA
Member 10994712 14-Aug-14 0:13am    
when i write the code this.close();,form1 f=new form1(),f.show in button click it works fine..
Member 10994712 14-Aug-14 0:15am    
but when form2 displayed i wrote serialport1.open()in form load.and all other code written on datareceived event..but i didnt got any data from serialport..
Sergey Alexandrovich Kryukov 14-Aug-14 2:35am    
Look, you said before that you got all data from serial port. Maybe you got it but failed to display. Use synchronous I/O in a separate thread. If you have a problem, show this code, for now, without UI. And so on...
—SA

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