Click here to Skip to main content
16,004,653 members
Home / Discussions / C#
   

C#

 
QuestionMerge Sort linked list Pin
Member 1431822319-Apr-19 12:23
Member 1431822319-Apr-19 12:23 
AnswerRe: Merge Sort linked list Pin
#realJSOP19-Apr-19 12:38
professional#realJSOP19-Apr-19 12:38 
GeneralRe: Merge Sort linked list Pin
Member 1431822320-Apr-19 9:12
Member 1431822320-Apr-19 9:12 
Questionrecieveing and splitting serial data from Arduino in C# Pin
auting8219-Apr-19 7:07
auting8219-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
auting8219-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
sitebuilderLuc 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
auting8219-Apr-19 11:36 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
Luc Pattyn19-Apr-19 11:52
sitebuilderLuc Pattyn19-Apr-19 11:52 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
auting8224-Apr-19 7:34
auting8224-Apr-19 7:34 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
Luc Pattyn24-Apr-19 8:37
sitebuilderLuc Pattyn24-Apr-19 8:37 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
auting8224-Apr-19 9:45
auting8224-Apr-19 9:45 
GeneralRe: recieveing and splitting serial data from Arduino in C# Pin
Luc Pattyn24-Apr-19 10:05
sitebuilderLuc Pattyn24-Apr-19 10:05 
Questionrecieveing and splitting serial data from Arduino in C# Pin
auting8219-Apr-19 6:04
auting8219-Apr-19 6:04 
Questionsample application to customize combo box with multi selection an check box in winform Pin
ambili3018-Apr-19 0:18
ambili3018-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
mveRichard 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
professionalEddy 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
mveOriginalGriff18-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
professional#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
mveOriginalGriff18-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
mveOriginalGriff18-Apr-19 6:34 
QuestionHow store all TabPage items in c# Pin
Richi38117-Apr-19 22:58
Richi38117-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.