标签云

微信群

扫码加入我们

WeChat QR Code

I have a sensor that is giving me force readings through a serial port.I type a command to the serial port and it will give me back 1 reading each time.I use a timer to send the command every so often.private void daqTimerElapsed(object sender, ElapsedEventArgs e){giveCommand("W");timeCounter += timeBetweenSamples;}Once the command gets sent, the serial port's dataReceived event fires and that's where the force reading comes in as a string.I convert the string to a double and put it into a List<> of doubles.Basically I am trying to get the table of values set up so I can perform a real time graphing in ZedGraph.Everytime new data has arrived, store it in the array list and then update the graph.//graphing section from DataReceived eventif (graphFlag == 1){dataReading = Convert.ToDouble(data);x.Add(timeCounter / 1000);y.Add(dataReading);//remake the graph each time a new X Y pair arriveszedGraphControl1.GraphPane.CurveList.Clear();spl1 = new PointPairList(x.ToArray(), y.ToArray());GraphPane myPane = zedGraphControl1.GraphPane;myCurve1 = myPane.AddCurve(curveTitle, spl1, Color.Orange, SymbolType.None);zedGraphControl1.AxisChange();zedGraphControl1.Invalidate();zedGraphControl1.Invoke(new EventHandler(delegate { zedGraphControl1.Refresh(); }));numSamples++; }I got it work, except for each time reading, there are many force readings but I only want 1 force reading per time reading.My export to file method shows this (time then force)://export the data to a text fileprivate void exportToolStripMenuItem_Click(object sender, EventArgs e){Stream myStream;SaveFileDialog saveFileDialog1 = new SaveFileDialog();saveFileDialog1.Title = "Save the test results to a text file";saveFileDialog1.ShowDialog();if ((myStream = saveFileDialog1.OpenFile()) != null){StreamWriter st = new StreamWriter(myStream);foreach(double xvalue in x){foreach(double yvalue in y){st.WriteLine(xvalue.ToString() + " " + yvalue.ToString());}}myStream.Close();}}0 -8.111820 -7.9534950 -7.9534950 -7.9395250 -7.9395250 -7.9488380 -7.9814340 -7.9814340 -7.8650190 -7.8650190 -7.8836460 -7.8836460 -8.0419710 -8.0373140 -8.0373140 -7.8417360 -7.8417360.1 -7.8743320.1 -7.9860910.1 -7.9860910.1 -7.9348680.1 -7.8324230.1 -7.8324230.1 -7.8324230.1 -7.9022720.1 -7.9022720.1 -8.0233440.1 -7.8976160.1 -7.8976160.1 -7.8696760.1 -7.869676Can anyone help me on getting 1 force reading for 1 time reading?


Please don't prefix your titles with "C# - " and such. That's what the tags are for.

2019年04月20日15分24秒

M.Babcock: good catch - in time for me to edit.

2019年04月20日15分24秒

Ok thanks I will try without using dataReceived, Yeah ReadTo() is nice I just read up to a newline and then trim

2019年04月20日15分24秒

If you think this helped solved the problem, select it as the answer.

2019年04月20日15分24秒

Well i tried your suggestion and basically got rid of the dataReceived part and moved it into the timer instead.I write the command, wait about 50ms and then read like you said.It works, but it still doesnt solve my table of values problem.I still get tons of force readings for 1 time reading.I just want 1 for 1! :(

2019年04月19日15分24秒