Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Need little help on C#

views
     
TSlucifer93
post Aug 4 2015, 06:26 AM, updated 9y ago

Getting Started
**
Junior Member
127 posts

Joined: Feb 2012


I'm doing a Windows Application Form for a real time graph plotting that takes data from a serial port.

I am new with C# so please bear with me if I did any silly mistake and please DO feel free to correct me.


I done a DataReceivedHander for the serial port.
CODE

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
           SerialPort sp = (SerialPort)sender;
           string[] indata;
           indata = new string[21];
         
   
           for (int i = 0; i < 20; i++)
           {
              indata[i] = sp.ReadExisting();    
           }
}


and I have my graph plotter function, tested with manually input the data.
but now i wanted to put the array from the DataReceivedHandler into my graph function.
So in my graph function will be plotting each array's index value to its receptive location

Can anyone help me out? Thank you in advance


mcblade
post Aug 4 2015, 06:55 AM

New Member
******
Senior Member
1,116 posts

Joined: Jan 2003
From: Kuching, Sarawak
indata = 21 slot
i = 20 slot
1 input will be omitted
ragk
post Aug 4 2015, 11:21 AM

BooBoo~
*******
Senior Member
2,265 posts

Joined: Apr 2009


hows your plotter function look like, receiving string parameter?

This post has been edited by ragk: Aug 4 2015, 11:21 AM
ChrisJL
post Aug 4 2015, 01:22 PM

New Member
*
Junior Member
23 posts

Joined: Jul 2015
Maybe you can do a foreach loop on the array and perform action on each element.

If your graph function looks like this, as stated by ragk,

CODE

private static void GraphPlotter(string value)
{
//whatever
}


then you can have it hooked to an action delegate like this,

CODE

Action<string> action = new Action<string>(GraphPlotter);

// Then you can do a foreach loop
Array.ForEach(indata, action);

TSlucifer93
post Aug 8 2015, 11:58 PM

Getting Started
**
Junior Member
127 posts

Joined: Feb 2012


Sorry for not being clear.
I'm using zedgraph to help me to plot the graph.
I just edit from it's example to plot the graph.

CODE
private void CreateGraph(ZedGraphControl zgc)


the graph is plotted by the function above. As my serial port data is coming from
CODE
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)



ChrisJL
post Aug 9 2015, 08:51 PM

New Member
*
Junior Member
23 posts

Joined: Jul 2015
In your ZedGraphControl struct type, create a parameterized constructor that takes in the string data from the serial port.

In the DataReceiveHandler method,

CODE
// Uses lambda expression to project the indata array into zeds array
ZedGraphControl[] zeds = indata.Select(x => new ZedGraphControl(x)).ToArray();

//Hooks an action delegate to the CreateGraph method
Action<ZedGraphControl> action = new Action<ZedGraphControl>(CreateGraph);

//Performs action loop
Array.ForEach(zeds, action);

But I'd suggest initializing the indata array of type ZedGraphControl, not as type string so you wouldn't need to perform a second loop with the Select method.
TSlucifer93
post Aug 10 2015, 01:38 AM

Getting Started
**
Junior Member
127 posts

Joined: Feb 2012


QUOTE(ChrisJL @ Aug 9 2015, 08:51 PM)
In your ZedGraphControl struct type, create a parameterized constructor that takes in the string data from the serial port.

In the DataReceiveHandler method,

CODE
// Uses lambda expression to project the indata array into zeds array
ZedGraphControl[] zeds = indata.Select(x => new ZedGraphControl(x)).ToArray();

//Hooks an action delegate to the CreateGraph method
Action<ZedGraphControl> action = new Action<ZedGraphControl>(CreateGraph);

//Performs action loop
Array.ForEach(zeds, action);

But I'd suggest initializing the indata array of type ZedGraphControl, not as type string so you wouldn't need to perform a second loop with the Select method.
*
Thanks you for your reply but I'm still lost in this matter.


ChrisJL
post Aug 10 2015, 12:12 PM

New Member
*
Junior Member
23 posts

Joined: Jul 2015
QUOTE(lucifer93 @ Aug 10 2015, 01:38 AM)
Thanks you for your reply but I'm still lost in this matter.
*
It is just a general idea not a solution. I mistakenly thought ZedGraphControl was just a simple struct, apparently not. So you have to google around for more info.

 

Change to:
| Lo-Fi Version
0.0160sec    0.27    5 queries    GZIP Disabled
Time is now: 29th March 2024 - 09:30 AM