0

I want to ideally send a few bytes of serial data from a computer to an Arduino. There has to be a better way than what I have chosen to do but my experience with serial data sent byte by byte and c++ is limited. I want to append each byte to definable variable values that are multiple bytes long. for example if the first 3 bytes of a serial stream are '3', '5', and '7' I want that to read as a single int of value 357. I am using Serial.readBytesUntil.

I have tried using Serial.readStringUntil() but I was having the same problem but converting string type to an integer. I feel like there is an easier way than what I am doing.

size_t input = Serial.readBytesUntil('\r', sentdata, sizeof(sentdata)-1);
dist[0] = sentdata[0];
dist[1] = sentdata[1];
dist[2] = sentdata[2];
dist[3] = sentdata[3];
distance = dist[0]*1000 + dist[1]*100 + dist[2]*10 + dist[3];

distance should equal any number sent from 0000 to 9999. just as an fyi this value is related to steps performed by a stepper motor.

evan
  • 169
  • 3
  • 12
  • 1
    ... so, you are sending ascii? othrwise it wouldn't make sence to use a whole byte to send [0,9] – Stefan Jul 27 '19 at 19:02
  • I believe I am using utf-8 as python .encode() defaults to that. Should I be doing something else? – evan Jul 27 '19 at 19:12
  • Ah, yes. Well, a safe and common way is to send data, also numbers as fixed-width character data. The thing with UTF-8 is, is not fixed width. So you don't know how many bytes you are sending. 8-bit ASCII is a good alternative for simple data. Then, basically what you are receiving can be put in a string, but, your way also works (although ascii values are not converted like that). Note: if you need high performance you should only do binary data. – Stefan Jul 27 '19 at 19:33
  • So if I use 8-bit ASCII how would that work in terms of outputs for sentdata and would that change how I transmit the data as well or would I just say .encode("ascii")? – evan Jul 27 '19 at 19:44
  • First you must agree on a way to know if you have collected all data of a specific value (since it's split up). As for the conversion: here are the numbers: https://en.wikipedia.org/wiki/ASCII – Stefan Jul 27 '19 at 19:49
  • Okay but it seems that even with ascii conversion I would somehow have to append the digits together to form a single integer and I am back at the same starting point. – evan Jul 27 '19 at 20:01
  • 1
    Your main error seems to be that you don't appreicate that the ASCII (or UTF-8) value of a digit is not the same as it's numerical value. The code you've written above should be `distance = (dist[0]-'0')*1000 + (dist[1]-'0')*100 + (dist[2]-'0')*10 + (dist[3]-'0');` By subtracting `'0'` from each digit you convert it's ASCII value to it's numerical value. – john Jul 27 '19 at 20:14
  • You can either do your calculation, which is fine (but do take the ascii `0`value into account). Or convert your input into a string and do [atoi](http://www.cplusplus.com/reference/cstdlib/atoi/) which is similar to your conversion. – Stefan Jul 27 '19 at 20:15
  • Ahh okay thank you. I guess I will make the changes about the ascii 0 value but leave the line otherwise unchanged. – evan Jul 27 '19 at 20:33
  • Why don't you want to use `Serial.parseInt()`? – Igor G Jul 27 '19 at 21:56
  • Because of the encoding in python. – evan Aug 06 '19 at 18:20
  • Does this answer your question? [How to parse a string to an int in C++?](https://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) – wovano Sep 21 '21 at 10:14

0 Answers0