Click here to Skip to main content
14,333,875 members
   

C#

 
AnswerRe: recieveing and splitting serial data from Arduino in C# Pin
Gerry Schmitz19-Apr-19 9:20
mveGerry Schmitz19-Apr-19 9:20 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
auting8219-Apr-19 10:02
memberauting8219-Apr-19 10:02 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
Gerry Schmitz19-Apr-19 11:32
mveGerry Schmitz19-Apr-19 11:32 
AnswerRe: recieveing and splitting serial data from Arduino in C# Pin
Luc Pattyn19-Apr-19 11:00
professionalLuc Pattyn19-Apr-19 11:00 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
auting8219-Apr-19 11:36
memberauting8219-Apr-19 11:36 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
Luc Pattyn19-Apr-19 11:52
professionalLuc Pattyn19-Apr-19 11:52 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
auting8224-Apr-19 7:34
memberauting8224-Apr-19 7:34 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
Luc Pattyn24-Apr-19 8:37
professionalLuc Pattyn24-Apr-19 8:37 
Hi,

I see no problems; create a Sensor class that has:
- a private SerialPort object;
- three private numeric results (temp, moist, soilMoist); could be int, float, double,...;
- a constructor taking the parameters you may need (e.g. port name) and creating the serial port; this is also where you should wire the DataReceived event, so it executes once, not for every start!
- a public Start() method that does what your start button does now; you should remove the DataReceived wiring here.
- a public Stop() method that does what your stop button does now;
- a DataReceived event handler that does what your current one does, except it interprets the incoming string, turns the fields into numbers using float.Parse() or float.TryParse() or something similar, and stuffs the results in the numeric class fields temp, moist, soilMoist; saving results in simple numeric variables does not violate thread safety, there will be no need to use any Invoke.
- three public properties Temp, Moist, SoilMoist that offer a getter to read those results;
- a public Dispose() method that calls SerialPort.Dispose(), something you have omitted so far.

As a result, a Sensor instance would collect whatever you receive on the serial port from calling Start() till calling Stop() or Dispose(). And reading the result properties would immediately return the most recent value that is available (and zero if no results yet).

In order to see the results in your current Form's TextBoxes, you could use a System.Windows.Forms.Timer that periodically (say once every second) gets the results and moves them into the TextBoxes, using float.ToString(). Of course you then would call Start() and Stop/Dispose() only once.

Possible improvement: you could also add a private DateTime measured and a public property DateTime Measured, which will offer the DateTime when the data was last successfully updated.

PS: you can format the current time simply with DateTime.Now.ToString("HH:mm:ss");

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
auting8224-Apr-19 9:45
memberauting8224-Apr-19 9:45 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
Luc Pattyn24-Apr-19 10:05
professionalLuc Pattyn24-Apr-19 10:05 
Questionrecieveing and splitting serial data from Arduino in C# Pin
auting8219-Apr-19 6:04
memberauting8219-Apr-19 6:04 
Questionsample application to customize combo box with multi selection an check box in winform Pin
ambili3018-Apr-19 0:18
memberambili3018-Apr-19 0:18 
AnswerRe: sample application to customize combo box with multi selection an check box in winform Pin
Richard MacCutchan18-Apr-19 1:09
protectorRichard MacCutchan18-Apr-19 1:09 
AnswerRe: sample application to customize combo box with multi selection an check box in winform Pin
Eddy Vluggen18-Apr-19 1:21
mveEddy Vluggen18-Apr-19 1:21 
AnswerRe: sample application to customize combo box with multi selection an check box in winform Pin
OriginalGriff18-Apr-19 3:43
protectorOriginalGriff18-Apr-19 3:43 
GeneralRe: sample application to customize combo box with multi selection an check box in winform Pin
#realJSOP18-Apr-19 8:00
mve#realJSOP18-Apr-19 8:00 
GeneralRe: sample application to customize combo box with multi selection an check box in winform Pin
OriginalGriff18-Apr-19 8:05
protectorOriginalGriff18-Apr-19 8:05 
GeneralRe: sample application to customize combo box with multi selection an check box in winform Pin
Dave Kreskowiak18-Apr-19 8:55
mveDave Kreskowiak18-Apr-19 8:55 
AnswerRe: sample application to customize combo box with multi selection an check box in winform Pin
Gerry Schmitz18-Apr-19 6:12
mveGerry Schmitz18-Apr-19 6:12 
GeneralRe: sample application to customize combo box with multi selection an check box in winform Pin
OriginalGriff18-Apr-19 6:34
protectorOriginalGriff18-Apr-19 6:34 
QuestionHow store all TabPage items in c# Pin
Richi38117-Apr-19 22:58
memberRichi38117-Apr-19 22:58 
AnswerRe: How store all TabPage items in c# Pin
OriginalGriff17-Apr-19 23:34
protectorOriginalGriff17-Apr-19 23:34 
AnswerRe: How store all TabPage items in c# Pin
#realJSOP18-Apr-19 2:18
mve#realJSOP18-Apr-19 2:18 
GeneralRe: How store all TabPage items in c# Pin
Richi38125-Apr-19 20:29
memberRichi38125-Apr-19 20:29 
AnswerRe: How store all TabPage items in c# Pin
Gerry Schmitz18-Apr-19 6:09
mveGerry Schmitz18-Apr-19 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.