Go Down

Topic: Serial Input Basics (Read 357271 times) previous topic - next topic

ghlawrence2000

@Robin2 Thanks for a brilliant tutorial! I am not exactly a newbie but I still found it very useful.

Developing the parsedata() function a little further I can now send commands like <getInt32()> <writeImage(0xbb800, 800, 480)> <sendBytes(0x1fe400, 256)> with up to 3 parameters and the parameters can be Hex or decimal.

Code: [Select]
void parseData() {
  char * strtokIndx; // this is used by strtok() as an index
  strtokIndx = strtok(tempChars, "(");     // get the first part - the string
  strcpy(COMMANDFromPC, strtokIndx); // copy it to COMMANDFromPC
  strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
  if (!(strncmp(strtokIndx, "0x", 2))) {
    sscanf(strtokIndx, "%2s%lx", NULL, &VAL1FromPC);
  } else {
    VAL1FromPC =  atoi(strtokIndx);  // convert this part to an integer
  }
  strtokIndx = strtok(NULL, ",");
  if (!(strncmp(strtokIndx, "0x", 2))) {
    sscanf(strtokIndx, "%2s%lx", NULL, &VAL2FromPC);
  } else {
    VAL2FromPC = atoi(strtokIndx);  // convert this part to an integer
  }
  strtokIndx = strtok(NULL, ",");
  if (!(strncmp(strtokIndx, "0x", 2))) {
    sscanf(strtokIndx, "%2s%lx", NULL, &VAL3FromPC);
  } else {
    VAL3FromPC = atoi(strtokIndx);  // convert this part to an integer
  }
}


Continuing with the theme, I then implemented a bool to allow switching between 'command' and 'data' mode.

Partial 'loop' function.
Code: [Select]
void loop() {
  serialEvent();
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    parseData();
    if (!(strcmp(COMMANDFromPC, "ERASE"))) {
      erase();
    }
    if (!(strcmp(COMMANDFromPC, "getInt32"))) {
      readInt32(VAL1FromPC);
    }
    if (!(strcmp(COMMANDFromPC, "getString"))) {
      readString(VAL1FromPC);
    }
    if (!(strcmp(COMMANDFromPC, "getInt16"))) {
      readInt16(VAL1FromPC);
    }


The only changes to serialEvent and recvWithStartEndMarkers are the switch to check which mode we are in.

Code: [Select]
void serialEvent() {
  if (datamode) {


and

Code: [Select]
void recvWithStartEndMarkers() {
  if (!datamode) {


Again, brilliant article, thank you.

Regards,

Graham
UTFT_SdRaw now included in library manager!! ;) High speed image drawing from SD card to UTFT displays for Mega & DUE.
UTFT_GHL - a VASTLY upgraded version of UTFT_CTE. Coming soon to a TFT near you! 8) Shipping April 1 2016!

PaulS

Code: [Select]
  if (!(strncmp(strtokIndx, "0x", 2))) {
This is FAR easier to read if you accept the fact that strcmp() returns -1 or +1 if the strings don't match, depending on the order of the strings in the dictionary, and returns 0 if they do match.

Code: [Select]
  if (strncmp(strtokIndx, "0x", 2) != 0)
  {


Quote
Again, brilliant article, thank you.
Agreed.
The art of getting good answers lies in asking good questions.

LMI1

These functions should be included into the IDE. Transferring larger than byte variables is a common subject in the forum, in my opinion

Coding Badly

#78
Feb 25, 2016, 10:05 pm Last Edit: Feb 25, 2016, 10:06 pm by Coding Badly
These functions should be included into the IDE.
https://github.com/arduino/Arduino/

Create an issue.  Attach a pull request to the issue.


LMI1

https://github.com/arduino/Arduino/

Create an issue.
I can write to that forum.

Attach a pull request to the issue.
What does that mean? How to do it?

Robin2

#80
Mar 02, 2016, 07:14 pm Last Edit: Mar 02, 2016, 07:15 pm by Robin2
I seem to have missed the Posts to this Thread in February 2016 (apologies, if required).

@ghlawrence2000, it seems as if your developments are particular to your needs rather than appropriate for a general tutorial.

You should not call serialEvent() from within loop(). It is called automatically by the hidden main() function. However, as I mentioned in my tutorial I don't think serialEvent() adds anything useful to the Arduino system and I never use it.

Because my code does not use serialEvent() I don't understand the statement
Quote
The only changes to serialEvent and recvWithStartEndMarkers are
@LMI, I don't agree that the functions I have written should be in the Arduino IDE. My functions were written for demonstration purposed and it is unlikely that anyone will want to use them without making some changes. It is easier to see the code and change it when it is not in the IDE.

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

Go Up