Click here to Skip to main content
15,359,191 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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...
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".
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
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...
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..
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.
Member 10994712 12-Aug-14 3:18am
i didnt understand what u mean??
Sergey Alexandrovich Kryukov 12-Aug-14 10:54am
Can you receive the values from serial port, did you try? Without any forms?
Member 10994712 12-Aug-14 23:03pm
yes...i tried that..i received values from serialport.
Sergey Alexandrovich Kryukov 12-Aug-14 23:35pm
Very good; it means that we can be sure that this is not a problem.

The only problem is notification of the UI thread and rendering data in UI, which is simply solved; I explained it in detail in my answer; if you use this approach, you will easily achieve that.

Herman<T>.Instance 12-Aug-14 3:59am
SerialPort already uses it's own thread, so Invoke must be used.
Sergey Alexandrovich Kryukov 12-Aug-14 11:22am
It depends on how you use this. Please see my answer.
CPallini 12-Aug-14 2:52am
What are you trying to do? You know, your PC is connected to just one endpoint of the serial line.
Member 10994712 12-Aug-14 3:14am
i want to display form2 after receiving a character from serialport in form1
CPallini 12-Aug-14 3:22am
Let me rephrase a bit it all:
(1) Form1 SerialPort receives character.
(2) Form2 is shown.

What do you want to do?
Member 10994712 12-Aug-14 3:25am i want to receive value from same serialport in form2 and store these values in a structure.
Member 10994712 12-Aug-14 3:31am
when i load form2 serialport sends data..but form2 didnt get data from serialport.
CPallini 12-Aug-14 3:37am
You shouldn't use two SerialPort instances. You should use just one (please note a SerialPort instance doesn't need to be a member of your form).
Herman<T>.Instance 12-Aug-14 4:00am
true. He should create a container for it
Member 10994712 12-Aug-14 4:08am
this is my code in firstform
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
int bytes = serialPort1.ReadByte();

if (bytes == 'M')


Application.Run(new Form1());

and code in second form is..

char[] c = new char[1000];
char[] c2 = new char[1000];
int O=0;
string[] ss3 = new string[200];
string[] s2 = new string[200];
string[] s3 = new string[200];
string ss = "";
string dfgh = "";
string sd = "";
string dfg = "";
int k = 0;
string[] rr = new string[200];
ss = serialPort1.ReadExisting();
s2 = ss.Split('#');

in second forms code doesnt work

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:
if (bytes == 'M')
   Form2 f2 = new Form2();
      bytes = serialPort1.ReadByte();
      } while (bytes != "X");

May not be exactly what you want, but it stands a chance of working!
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.
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?
Herman<T>.Instance 12-Aug-14 14:54pm
True! use serialPort1.ReadExisting() in stead
Sergey Alexandrovich Kryukov 12-Aug-14 15:05pm
I would strongly prefer my way explained in Solution 3, by a number of reasons...
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[^],
Control events not firing after enable disable + multithreading[^].

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.Close();
FLAG = 1;

Form1 f = new Form1();
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.
Member 10994712 14-Aug-14 0:13am
when i write the code this.close();,form1 f=new form1(), in button click it works fine..
Member 10994712 14-Aug-14 0:15am
but when form2 displayed i wrote form load.and all other code written on datareceived event..but i didnt got any data from serialport..
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...

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