Go Down

Topic: Data Input demo sketch (Read 15649 times) previous topic - next topic

Robin2

Newcomers are sometimes unsure how to receive data into an Arduino sketch from the Serial Monitor or from a program running on a PC. I thought it would be helpful to write a demo sketch that illustrates a few useful techniques.

The sketch attached to this Post includes 5 self contained functions that illustrate different ways to deal with incoming data.

  readSingleChar()      reads and saves a single character and its byte value
  readSingleDigit();    reads and saves as a number any of the digits 0-9
  readSeveralChars();   reads several characters and saves them as a string in a character array
  readOneFloat();       reads several numeric characters and saves them as a floating point value
  readCSV();            reads a string, an integer and a float separated by commas
                          for example testing , 123 , 456.78
                         
Newcomers should not be put off by the length of the total sketch. They should just concentrate on one function at a time. The comments will make more sense if the functions are studied in order.

To try (for example) the readSingleDigit() function it is only necessary to comment out all the other functions in loop() as follows
Code: [Select]
//  readSingleChar();
  readSingleDigit();
//  readSeveralChars();
//  readOneFloat();
//  readCSV();


One thing that these simple examples do NOT have is the ability to identify the correct start and end to a message - it is possible for part of a message to be missed. This is unlikely in this demo because of the 1 second delay between iterations of loop() but it is a real risk in a working program that runs quickly. The demo sketch that I included in the first post of this Thread shows how to deal with this problem. Its technique can be combined with the techniques here.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

PaulS

readOneFloat() has problems, in that:
11)  is assumes that all the data for the float is in the serial buffer
2) that nothing else is in the buffer except that float

readCSV() assumes that the string contained something, so that strtok() returns a pointer. Further, it assumes the the string contains two commas. It would be far better to test that strtok() returned a pointer before dereferencing that pointer. The function, too assumes that the input is in a specific format, AND that the whole packet is ready to be read and the nothing but the packet is ready to be read.

As examples, I would have problems recommending that people adopt these functions.

Robin2

Thanks Paul,

This sort of thing is always a balance between completeness and simplicity. I have opted for simplicity. To the best of my knowledge the examples work no matter what rubbish you throw at them. I have deliberately not tried to make universally applicable functions as all the extra code makes it difficult for a newcomer to see the principal message. In that sense I am not expecting anyone to recommend their adoption without reservations.

At the end of my post I referred to the potential problem that the data may be incomplete and provided a link to an example that deals with that problem. I believe it would be counterproductive to deal with that issue in this demo.

Unfortunately as you add (necessary) bells and whistles the examples become more and more specific to a particular case. All I want to achieve with this demo is to point people in the appropriate direction without having to repeat the same stuff over and over in reply to other Threads.

Feel free to add some examples and explanations of your own. It would be convenient to have them all in the same place.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MachineLearner

Newcomers are sometimes unsure how to receive data into an Arduino sketch from the Serial Monitor or from a program running on a PC. I thought it would be helpful to write a demo sketch that illustrates a few useful techniques.

The sketch attached to this Post includes 5 self contained functions that illustrate different ways to deal with incoming data.

  readSingleChar()      reads and saves a single character and its byte value
  readSingleDigit();    reads and saves as a number any of the digits 0-9
  readSeveralChars();   reads several characters and saves them as a string in a character array
  readOneFloat();       reads several numeric characters and saves them as a floating point value
  readCSV();            reads a string, an integer and a float separated by commas
                          for example testing , 123 , 456.78
                         
Newcomers should not be put off by the length of the total sketch. They should just concentrate on one function at a time. The comments will make more sense if the functions are studied in order.

To try (for example) the readSingleDigit() function it is only necessary to comment out all the other functions in loop() as follows
Code: [Select]
//  readSingleChar();
  readSingleDigit();
//  readSeveralChars();
//  readOneFloat();
//  readCSV();


One thing that these simple examples do NOT have is the ability to identify the correct start and end to a message - it is possible for part of a message to be missed. This is unlikely in this demo because of the 1 second delay between iterations of loop() but it is a real risk in a working program that runs quickly. The demo sketch that I included in the first post of this Thread shows how to deal with this problem. Its technique can be combined with the techniques here.

...R
Hello Robin,
After searching for some relevant threads, I though to ask here as it seems to be relevant to the problem at hand!

Problem statement: I want to read a csv file sent from python and do the operation on the Arduino like blinking LED / LED cube!

So , can we use readCSV function for IRIS dataset as it has integers, floats and species strings in the data..
The only thing I see is the string is printed at last coloum in the csv file...if we change the string colum to first to get a handshake for your function, then I thing this function can be called?Right?

Robin2

Hello Robin,
After searching for some relevant threads, I though to ask here as it seems to be relevant to the problem at hand!
This is so old I had forgotten I had written it.

Just start your own Thread with a clear description of your problem and include the program you are having difficulty with.

This Python - Arduino demo may help. Also see Serial Input Basics - simple reliable ways to receive data..

Please don't reply here - the Thread is too old and is not specific to your problem, Just start your own Topic (thread).

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up