Click here to Skip to main content
14,303,069 members
   

C#

 
QuestionMerge Sort linked list Pin
Member 1431822319-Apr-19 12:23
memberMember 1431822319-Apr-19 12:23 
AnswerRe: Merge Sort linked list Pin
#realJSOP19-Apr-19 12:38
mve#realJSOP19-Apr-19 12:38 
GeneralRe: Merge Sort linked list Pin
Member 1431822320-Apr-19 9:12
memberMember 1431822320-Apr-19 9:12 
Questionrecieveing and splitting serial data from Arduino in C# Pin
auting8219-Apr-19 7:07
memberauting8219-Apr-19 7:07 
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 
Hi,

Reading from a SerialPort isn't always easy. Your code may fail to work reliably for several reasons:

1.
The SerialPort.DataReceived event is fired when one or more characters have been received; there is no strict definition of when it fires. And it is not synchronized to anything, it is not aware of what you consider a message (such as a piece of text terminated by a newline).

2.
SerialPort.ReadLine() returns one line of text assuming a newline has been received; if not a timeout exception will occur.

3.
You are using two threads, and communicating between them using a single string (in_data). It is conceivable that two DataReceived events occur before the other thread manages to execute displaydata_event(). In that case, you would loose data.


If you are in charge of software on both sides, the simplest approach would be like this:

1. don't let the peripheral send data at will, make it a slave that doesn't have the initiative, instead it executes commands when it receives them
2. send a command to the peripheral requesting (a measurement and) the return of data
3. wait a short time
4. read the data synchronously

So basically that would be:

SerialPort port;
...
port.Open();
...
port.WriteLine("T");          // the command to measure and send a temperature
Thread.Sleep(100);            // wait for measurement and transmission
string temp=port.ReadLine();  // receive the result
textbox.Text=temp;


The delay should be sufficient for the peripheral to perform the measurement, and the serial port to transmit and receive the data. Since 9600 Baud would correspond to approx 1 msec per character, delaying for 100 msec seems reasonable, and not too harmful for your GUI interface.

Of course, the proper way to do communication is asynchronous, but then you need to take care of proper synchronisation and data transfer; that would include:
1. reading the port with ReadExisting, not ReadLine;
2. collecting the incoming characters;
3. identifying message boundaries;
4. passing results to another thread, using some real data structure (possibly a queue).


Hope this helps.

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


modified 19-Apr-19 17:08pm.

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 
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 

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.