<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Serial Stream ... Hexadecimal to Digital</title>
		<link>http://forums.leaflabs.com/topic.php?id=74377</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:13:55 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=74377" rel="self" type="application/rss+xml" />

		<item>
			<title>gbulmer on "Serial Stream ... Hexadecimal to Digital"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74377#post-105536</link>
			<pubDate>Thu, 09 Oct 2014 05:27:55 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">105536@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;There are easier ways.&#60;/p&#62;
&#60;p&#62;The first point is the number can't be completely converted until it is known that all of the number has arrived.&#60;br /&#62;
A common way to do this is to receive a character that isn't part of the number. For the serial monitor that is newline or carriage return. So parsing character by character doesn't gain much.&#60;/p&#62;
&#60;p&#62;Secondly it is straightforward to write code which converts from characters to a binary number without assuming much about the number (other than it will fit into an int).&#60;/p&#62;
&#60;p&#62;Third, it is easier to debug if the whole line of text is collected into an array, and then 'decoded'. It is easy to add a print statement which echos the value read so far, and print statements which show the processing steps.&#60;/p&#62;
&#60;p&#62;FInally, the character codes for '0' to '9', 'A' to 'F', or 'a' to 'f', are not conveniently arranged to be the value 0 to 9, and 10 to 15. They are ASCII (or the first 127 of UTF-8) codes, and so they need to be converted to the correct binary value.&#60;/p&#62;
&#60;p&#62;This hasn't been tested, and isn't exactly the way I would do it, however, it might help you:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;// forum topic 74377
#include &#38;lt;string.h&#38;gt;
#include &#38;lt;ctype.h&#38;gt;

int inByte;
char line[20];
int linePos = 0;

const char prefix[] = &#38;quot;PRS &#38;quot;;
// End of String, i.e. the 0 byte.
const char EOS = 0;
// The forum software consumes &#38;#39;\ followed by 0&#38;#39; which breaks code
// So this kludge reduces that problem.

int hexValue;
int nibble;

void setup() {
    Serial1.begin(57600);
}

void loop() {
    if (Serial1.available()) {
        inByte = Serial1.read();
        SerialUSB.print(inByte, BYTE);
    }
    if (inByte == &#38;#39;\n&#38;#39; &#124;&#124; inByte == &#38;#39;\r&#38;#39;) {  // got a line?
      line[linePos] = EOS;    // reducing  horriblness of forum S/W
      if (strncmp(prefix, line, sizeof(prefix)) == 0) {  // matches prefix?
        linePos = strlen(prefix);    // skip over prefix
        hexValue = 0;
        while (isxdigit(line[linePos])) {  // 0-9, A-Z, a-z?
          if (&#38;#39;A&#38;#39; &#38;lt;= line[linePos] &#38;amp;&#38;amp; line[linePos] &#38;lt;= &#38;#39;F&#38;#39;) {
            nibble = (line[linePos]-&#38;#39;A&#38;#39;)+10;
          } else if (&#38;#39;a&#38;#39; &#38;lt;= line[linePos] &#38;amp;&#38;amp; line[linePos] &#38;lt;= &#38;#39;z&#38;#39;) {
            nibble = (line[linePos]-&#38;#39;a&#38;#39;)+10;
          } else if (&#38;#39;0&#38;#39; &#38;lt;= line[linePos] &#38;amp;&#38;amp; line[linePos] &#38;lt;= &#38;#39;9&#38;#39;) {
            nibble = (line[linePos]-&#38;#39;0&#38;#39;);           // Edit: deleted &#38;quot;+10&#38;quot;
          }
          hexValue = (hexValue &#38;lt;&#38;lt; 4) + nibble;
        }
        SerialUSB.print(&#38;quot;value of line=&#38;#39;&#38;quot;);
        SerialUSB.print(line);
        SerialUSB.print(&#38;quot;&#38;#39; value of number=&#38;quot;);
        SerialUSB.print(hexValue);
        SerialUSB.print(&#38;quot; in hex=&#38;quot;);
        SerialUSB.println(hexValue, HEX);
      }
      linePos = 0;    // get ready for another line
    } else if (linePos &#38;lt; sizeof(line)-1) {   // append to line
      line[linePos] = inByte;
      linePos++;
    }
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;WARNING: This has been compiled but not tested.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>ABSimon on "Serial Stream ... Hexadecimal to Digital"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74377#post-105533</link>
			<pubDate>Wed, 08 Oct 2014 10:28:30 +0000</pubDate>
			<dc:creator>ABSimon</dc:creator>
			<guid isPermaLink="false">105533@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hello,&#60;/p&#62;
&#60;p&#62;thank you very mutch, but with UART I geht the string character by character,&#60;br /&#62;
for examlple &#34;1&#34; and &#34;8&#34; and &#34;B&#34; and &#34;3&#34; and &#34;9&#34;&#60;br /&#62;
and I want to parse it in realtime. I am looking for a function like this&#60;/p&#62;
&#60;p&#62;int inByte;                     // Byte read from Serial1&#60;br /&#62;
int start;&#60;br /&#62;
int pressure;  &#60;/p&#62;
&#60;p&#62;void setup() {&#60;br /&#62;
    // Initialize Serial1&#60;br /&#62;
    Serial1.begin(57600);&#60;br /&#62;
    start = 0;&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void loop() {&#60;/p&#62;
&#60;p&#62;    // Read from Serial1, send over USB:&#60;br /&#62;
    if (Serial1.available()) {&#60;br /&#62;
        inByte = Serial1.read();&#60;/p&#62;
&#60;p&#62;        if (inByte == &#34;P&#34; &#124;&#124; inByte == &#34;R&#34; &#124;&#124; inByte == &#34;S&#34;) {&#60;br /&#62;
        	start = 0;&#60;br /&#62;
        }&#60;/p&#62;
&#60;p&#62;        if (start) {&#60;/p&#62;
&#60;p&#62;        	if (start == 1) { pressure  = inByte * 10000; }&#60;br /&#62;
        	if (start == 2) { pressure += inByte * 1000; }&#60;br /&#62;
        	if (start == 3) { pressure += inByte * 100; }&#60;br /&#62;
        	if (start == 4) { pressure += inByte * 10; }&#60;br /&#62;
        	if (start == 4) { pressure += inByte; SerialUSB.println(pressure, DEC); }&#60;/p&#62;
&#60;p&#62;        	start++;&#60;br /&#62;
        }&#60;/p&#62;
&#60;p&#62;        if (inByte == &#34; &#34;) {&#60;br /&#62;
        	start = 1;&#60;br /&#62;
        }&#60;/p&#62;
&#60;p&#62;    }&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;Or is there an easyer was to do it?&#60;/p&#62;
&#60;p&#62;Thank you.&#60;br /&#62;
Simon
&#60;/p&#62;</description>
		</item>
		<item>
			<title>nmillan1 on "Serial Stream ... Hexadecimal to Digital"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74377#post-105531</link>
			<pubDate>Mon, 06 Oct 2014 12:43:50 +0000</pubDate>
			<dc:creator>nmillan1</dc:creator>
			<guid isPermaLink="false">105531@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;You need to separate the data into two strings before printing it to screen. It seems like each line always starts with a &#34;PRS &#34; (4 characters) so you can experiment with using &#34;string.h&#34; functions such as strcpy() and strtok() to parse it. Save the data before the blank space to one string and the data after to the other.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>ABSimon on "Serial Stream ... Hexadecimal to Digital"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74377#post-105530</link>
			<pubDate>Mon, 06 Oct 2014 12:34:04 +0000</pubDate>
			<dc:creator>ABSimon</dc:creator>
			<guid isPermaLink="false">105530@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hello,&#60;/p&#62;
&#60;p&#62;I have a very simple Script&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;int inByte;                    

void setup() {
    Serial1.begin(57600);
}

void loop() {
    if (Serial1.available()) {
        inByte = Serial1.read();
        SerialUSB.print(inByte, BYTE);
    }
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;an now try to parse this result.&#60;/p&#62;
&#60;p&#62;PRS 18B41&#60;br /&#62;
PRS 18B40&#60;br /&#62;
PRS 18B41&#60;br /&#62;
PRS 18B40&#60;br /&#62;
PRS 18B3F&#60;br /&#62;
PRS 18B42&#60;br /&#62;
PRS 18B40&#60;br /&#62;
PRS 18B43&#60;br /&#62;
PRS 18B3F&#60;br /&#62;
PRS 18B3C&#60;br /&#62;
PRS 18B3F&#60;br /&#62;
PRS 18B3D&#60;br /&#62;
PRS 18B40&#60;br /&#62;
PRS 18B3E&#60;br /&#62;
PRS 18B3F&#60;br /&#62;
PRS 18B3D&#60;br /&#62;
PRS 18B39&#60;br /&#62;
PRS 18B3D&#60;br /&#62;
PRS 18B3E&#60;br /&#62;
PRS 18B3C&#60;br /&#62;
PRS 18B3B&#60;br /&#62;
PRS 18B3C&#60;br /&#62;
PRS 18B3D&#60;br /&#62;
PRS 18B3B&#60;br /&#62;
PRS 18B3A&#60;br /&#62;
PRS 18B3B&#60;br /&#62;
PRS 18B3B&#60;br /&#62;
PRS 18B3D&#60;br /&#62;
PRS 18B3A&#60;br /&#62;
PRS 18B39&#60;/p&#62;
&#60;p&#62;I need the secound hexadecimal number as a digital number.&#60;br /&#62;
I think with &#34;inByte = Serial1.read();&#34; I can check each byte,&#60;br /&#62;
so after &#34;PRS &#34; I have to build a digital number.&#60;br /&#62;
I have realy no idea...&#60;/p&#62;
&#60;p&#62;Thank you very Mutch&#60;br /&#62;
Simon
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
