<?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: Simple rs485 test</title>
		<link>http://forums.leaflabs.com/topic.php?id=9744</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:21:37 +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=9744" rel="self" type="application/rss+xml" />

		<item>
			<title>jake1981 on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21732</link>
			<pubDate>Wed, 19 Dec 2012 15:40:33 +0000</pubDate>
			<dc:creator>jake1981</dc:creator>
			<guid isPermaLink="false">21732@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks gbulmer.. My library wasn't much a look at.. And had some bugs as well - I've re-written it.. And now I think it could be usable in other rs485 projects as well-- so I decided to make it publicly available for anyone who might be doing something similar. I also provided a simple master/server software that can be executed on a computer (tested only on mac, but should work on other POSIX standard followers as well..)&#60;/p&#62;
&#60;p&#62;And as I see, a lot talented people hang around here, please check out my library, good ideas are welcome as always :)&#60;/p&#62;
&#60;p&#62;Oh, it's here, in the github: &#60;a href=&#34;https://github.com/oskarirauta/RS485_library&#34; rel=&#34;nofollow&#34;&#62;https://github.com/oskarirauta/RS485_library&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21679</link>
			<pubDate>Sun, 16 Dec 2012 08:20:57 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">21679@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;jake1981 - &#60;em&#62;sprintf does not seem to get included with #include &#38;lt;string.h&#38;gt;&#60;/em&#62;&#60;br /&#62;
What causes you to believe it would? You may have some broken documentation.&#60;/p&#62;
&#60;p&#62;The newlib documentation is pretty clear:&#60;br /&#62;
&#60;a href=&#34;http://sourceware.org/newlib/libc.html#sprintf&#34; rel=&#34;nofollow&#34;&#62;http://sourceware.org/newlib/libc.html#sprintf&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://www.google.com/search?rls=en&#38;amp;q=C+sprintf+include&#38;amp;oq=C+sprintf+include&#38;amp;ie=UTF-8&#38;amp;oe=UTF-8&#34; rel=&#34;nofollow&#34;&#62;http://www.google.com/search?rls=en&#38;amp;q=C+sprintf+include&#38;amp;oq=C+sprintf+include&#38;amp;ie=UTF-8&#38;amp;oe=UTF-8&#60;/a&#62;&#60;br /&#62;
yields, e.g.:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://linux.die.net/man/3/sprintf&#34; rel=&#34;nofollow&#34;&#62;http://linux.die.net/man/3/sprintf&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>jake1981 on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21676</link>
			<pubDate>Sun, 16 Dec 2012 07:23:54 +0000</pubDate>
			<dc:creator>jake1981</dc:creator>
			<guid isPermaLink="false">21676@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;sprintf does not seem to get included with #include &#38;lt;string.h&#38;gt; ..
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21399</link>
			<pubDate>Sun, 02 Dec 2012 19:01:34 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">21399@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;sprintf?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>jake1981 on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21398</link>
			<pubDate>Sun, 02 Dec 2012 15:24:12 +0000</pubDate>
			<dc:creator>jake1981</dc:creator>
			<guid isPermaLink="false">21398@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Yes - but it lacked itoa :)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21396</link>
			<pubDate>Sun, 02 Dec 2012 13:07:58 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">21396@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;jake1981 - you rarely need to import functions, e.g. itoa. &#60;/p&#62;
&#60;p&#62;Maple has newlib &#60;a href=&#34;http://sourceware.org/newlib/&#34; rel=&#34;nofollow&#34;&#62;http://sourceware.org/newlib/&#60;/a&#62; which contains most common C functions
&#60;/p&#62;</description>
		</item>
		<item>
			<title>jake1981 on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21394</link>
			<pubDate>Sun, 02 Dec 2012 10:19:31 +0000</pubDate>
			<dc:creator>jake1981</dc:creator>
			<guid isPermaLink="false">21394@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Oops. I forgot to mention. rs485 sometimes has glitches- data goes missing- or get's corrupted.&#60;/p&#62;
&#60;p&#62;My testing rs485 bus is very small. Cabling isn't even 1 meter long, less than 0.5 meters actually, and glitches are rare.. But yet, I have seen them with distances this short.. So I have added some security features (one of admins on this forum actually gave me the idea..) to prevent this..&#60;/p&#62;
&#60;p&#62;There are some tests that check for the correct syntax-- and everytime we give a value, phone number, anything.. Or get a value.. We get it as double.. For example, 12 is 1122.&#60;/p&#62;
&#60;p&#62;command to call the device 1122 therefore is +++ATD1122#&#60;br /&#62;
set phone number of device we are talking with to 19: +++ATSETPHONE1199#&#60;br /&#62;
and when we ask for number: +++ATGETPHONE# we get VAL:1199#&#60;/p&#62;
&#60;p&#62;When setting phone number, number must be between 1 and 31.&#60;/p&#62;
&#60;p&#62;0 is never used, as devices don't call master - master calls devices - but master's number is 0 (reserved for future purposes)&#60;br /&#62;
32 is reserved for relay board. This pool has 1 too many for theoretical maximum amount of devices in rs485.&#60;/p&#62;
&#60;p&#62;+++AT is syntax for starting any command. It doesn't start reading of command without that. Master actually sends:&#60;br /&#62;
+++++AT when command starts. And every command ends with #. When command ends, master sends: ###&#60;/p&#62;
&#60;p&#62;Device reports usually like this:&#60;br /&#62;
OK# - done&#60;br /&#62;
ERR# - syntax error or mismatch&#60;br /&#62;
UNK# - unknown command (caused by user, syntax error or mismatch)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>jake1981 on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21393</link>
			<pubDate>Sun, 02 Dec 2012 10:08:39 +0000</pubDate>
			<dc:creator>jake1981</dc:creator>
			<guid isPermaLink="false">21393@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Okay. I know we talked earlier about doing a very simple protocol for my project that uses rs485 to communicate.. For easy debugging, after all, I decided to go with the original plan - which was an AT-command set designed for project. This way it is way easier, as I don't have my &#34;master&#34; device or relay board yet done (and actually I am still waiting for some parts).. And I have a usb-serial-&#38;gt;rs-485 adapter in my computer and a serial session through that.. And therefore, I am doing by hand the work of master device.. &#60;/p&#62;
&#60;p&#62;Project has grown - it no longer with to maple mini r2's flash, ofcourse, I maybe could minimize it a bit.. Bit I think tweaking comes when I have the system working, so it's atleast usable. Filetransfer hasn't been implemented yet, code has place for it.. But I think, I'l go with the filetransfer when I have my protocol ready first including everything I am needing..&#60;/p&#62;
&#60;p&#62;This isn't so much a opensource project - it's more like a experiment - but when I was playing with Arduinos, I was happy that there was so much source code available for the project, ofcourse, usually code wasn't usable for me, but it gave me a great starting point.. So I decided to share my work done so far..&#60;/p&#62;
&#60;p&#62;It's available here: &#60;a href=&#34;http://pastebin.com/M0dhRqWt&#34; rel=&#34;nofollow&#34;&#62;http://pastebin.com/M0dhRqWt&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;Oh - the code starts with #include &#38;lt;itoa.h&#38;gt;&#60;/p&#62;
&#60;p&#62;I got itoa.h from Arduino Due's IDE (Arduino Beta), copied itoa.h and itoa.c/cpp (can't remember which they had used) to libraries/itoa and it worked out of the box. &#60;/p&#62;
&#60;p&#62;commandBegins was first done with strncmp, but for unknown reason - it started failing, so I implemented my own.. Maybe the reason that it was failing for was that at that time I was still running from RAM and.. Propably my project eated up all the remaining RAM.&#60;/p&#62;
&#60;p&#62;---&#60;/p&#62;
&#60;p&#62;This can be tested also with serial without rs485, just connect your terminal to serial1. Little help is documented into the source code.&#60;/p&#62;
&#60;p&#62;Here's how I thought this could work; devices are 1xRaspberry PI connected to rs485, 1xMaple with 16(or at max. 32) relays. I call this the relayboard. And other devices: various maple or arduinos or similars connected to rs485 using this protocol, having various sensors connected and sensor data is acquired over this protocol with rs485.&#60;/p&#62;
&#60;p&#62;When system starts, only thing starting is relayboard. Relayboard has a watchdog enabled, so if it hangs - it automaticly reboots. When it starts, it checks that all relays are open - and then it closes relay which powers raspberry pi and gives it enough time to boot. After this it enables internal timer that resets every time data moves on rs485. If there has not been data in certain time, it's propably because master(raspberry pi) has hanged, this is the time when we close all relays and restart. A external hardware watchdog for raspberry.&#60;/p&#62;
&#60;p&#62;Raspberry boots and calls to device 32, which is the only static phone number that we aren't going to change and belongs to relayboard. Telling it to power up relay1, which therefore powers up device1. Initially all devices have phone number 99 and raspberry calls to that number after 2 seconds which is enough for boot. If device does not answer, there is no device 1. Raspberry makes a note of this. If device answers, raspberry asks the device to change it's phone number from 99 to same number as relay is, in this case: 1. This is done to all relays/devices of the system, so we can map amount and locations of the devices. There is also going to be command that asks device to report it's capabilities (sensors) to raspberry so  raspberry knows what it can do with the device.&#60;/p&#62;
&#60;p&#62;On the normal loop, raspberry queries mapped devices one by one asking for values from sensors, and ofcourse, checks that devices answer. If device doesn't answer, it has propably hanged - raspberry then asks relayboard to recycle the power of device and then contact the device again, to give it the phonenumber accordingly to it's relay.&#60;/p&#62;
&#60;p&#62;command +++ATZZ# returns always OK. This can be used to test if we lost connection to device, or if it hanged because of what we asked it to do, or something. Forgot to add it to the help in source.&#60;/p&#62;
&#60;p&#62;If raspberry hangs, everything must be restarted, because raspberry maps devices after boot. I could make it remember the devices, but then the system would need changes if I add a device or remove one. Idea is that I can just throw in more devices if I feel it's needed and this way system becomes modular.&#60;/p&#62;
&#60;p&#62;Yeah, and I know my code ain't perfect. But what a heck- it works. If you can do it better, feel free..
&#60;/p&#62;</description>
		</item>
		<item>
			<title>jake1981 on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21391</link>
			<pubDate>Sun, 02 Dec 2012 06:50:51 +0000</pubDate>
			<dc:creator>jake1981</dc:creator>
			<guid isPermaLink="false">21391@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Got it to work by using a sparkfun ttl level converter.. It seems that device can be run with 3.3v - but logic is still at 5v.. Luckily I used USART1 which is 5v tolerant, so my maple didn't fry..&#60;/p&#62;
&#60;p&#62;Can I use USART1 in 5v logic or is it just 5v tolerant, so it can stand 5v - for example because of testing purposes (does this have a 3.3v (lv)ttl or 5v ttl? Let's try..) or is there a command I can use to set it in 5v mode?&#60;/p&#62;
&#60;p&#62;---&#60;/p&#62;
&#60;p&#62;Okay - it seems that I have asked this question before and yes.. You cannot use 5v ttl without added devices that do convertion. Sparkfun's level converter is one of these, but a simpler resistor could do this as well.. For the moment I'll stick with the level converter, but if someone has idea about proper resistor for this need - point me with correct ohms so I don't need to do the math.. Thanks :)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>jake1981 on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21390</link>
			<pubDate>Sun, 02 Dec 2012 05:20:54 +0000</pubDate>
			<dc:creator>jake1981</dc:creator>
			<guid isPermaLink="false">21390@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I had some troubles with Serial1.print earlier, so I added a routine that prints out a byte at time, that's why I did what I did.. Although, it was with arduino and different rs-485 module.&#60;/p&#62;
&#60;p&#62;My program doesn't work. It outputs to rs-485 bus just fine- but cannot read :/
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21386</link>
			<pubDate>Sat, 01 Dec 2012 11:57:31 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">21386@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;jake1981 - Good to see progress. Great stuff.&#60;/p&#62;
&#60;p&#62;A couple of comments:&#60;/p&#62;
&#60;p&#62;&#60;code&#62;Serial1.write(&#38;#39;/&#38;#39;); Serial1.write(&#38;#39;2&#38;#39;); Serial1.write(&#38;#39;5&#38;#39;); Serial1.write(&#38;#39;]&#38;#39;); Serial1.write(&#38;#39; &#38;#39;);&#60;/code&#62;&#60;br /&#62;
could be replaced by&#60;br /&#62;
&#60;code&#62;Serial1.print(&#38;quot;/25] &#38;quot;);&#60;/code&#62;&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;char c = 0;
  while ( MSG[c] != 0 )
  {
    Serial1.write(MSG[c]);
    c++;
  }&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Can be replaces with &#60;code&#62;Serial1.print(MSG);&#60;/code&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>jake1981 on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21383</link>
			<pubDate>Sat, 01 Dec 2012 04:06:11 +0000</pubDate>
			<dc:creator>jake1981</dc:creator>
			<guid isPermaLink="false">21383@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Oh - what kind of device this works with and how did I wire it.. And to what?&#60;/p&#62;
&#60;p&#62;The rs485 module I used was this:&#60;br /&#62;
&#60;a href=&#34;http://viewitem.eim.ebay.fi/UART-TTL-Serial-Port-TO-RS485-Converter-Adapter-MAX485-SMD-SOP8-33V-254-Pin/290717451716/item&#34; rel=&#34;nofollow&#34;&#62;http://viewitem.eim.ebay.fi/UART-TTL-Serial-Port-TO-RS485-Converter-Adapter-MAX485-SMD-SOP8-33V-254-Pin/290717451716/item&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;But I am pretty sure, this is the same module:&#60;br /&#62;
&#60;a href=&#34;https://www.sparkfun.com/products/10124&#34; rel=&#34;nofollow&#34;&#62;https://www.sparkfun.com/products/10124&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;Mine just has header installed for me + screw terminal. But sparkfun's version is red.. Looks much better ;)&#60;/p&#62;
&#60;p&#62;---&#60;/p&#62;
&#60;p&#62;And I wired it to Maple mini R2..&#60;/p&#62;
&#60;p&#62;--&#60;/p&#62;
&#60;p&#62;I took the power from vcc and gnd, rx and tx are connected USART1 rx and tx, and ofcourse, RTS is connected to pin 27 which can be changed in the source by changing define in the beginning. This should work with other rs485 modules as well, some modules I have tried on Arduino side ( &#60;a href=&#34;http://www.electrodragon.com/?product=max485-rs-485-module-ttl-to-rs-485&#34; rel=&#34;nofollow&#34;&#62;http://www.electrodragon.com/?product=max485-rs-485-module-ttl-to-rs-485&#60;/a&#62; - technically identical to waveshare rs-485 module but has headers instead of rj-45 jack) don't support higher baud rates than 57600.&#60;/p&#62;
&#60;p&#62;Although, I found it pretty hard to find rs 485 modules that are 3.3v. Everything in 3.3v is hard to find. Hopefully now that we have arduino due, this would change..
&#60;/p&#62;</description>
		</item>
		<item>
			<title>jake1981 on "Simple rs485 test"</title>
			<link>http://forums.leaflabs.com/topic.php?id=9744#post-21382</link>
			<pubDate>Sat, 01 Dec 2012 03:47:21 +0000</pubDate>
			<dc:creator>jake1981</dc:creator>
			<guid isPermaLink="false">21382@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Okay. Here comes the source:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;/* * * * * * SOURCE STARTS * * * * * * * * /

#define  pinRTS    27
#define  DEBUGGING 1

#ifdef DEBUGGING
char tmp = 0;
#endif

void setup() {
  Serial1.begin(115200);
  pinMode(pinRTS, OUTPUT);
  digitalWrite(pinRTS, LOW);
  delay(10); // Settle for awhile
#ifndef DEBUGGING
  outputMSG(&#38;quot;UART initialization complete.\r\n&#38;quot;);
#endif
}

void loop() {
  delay(200);
  outputMSG(&#38;quot;Hello World\r\n&#38;quot;);
}

void outputMSG(char *MSG){
  digitalWrite(pinRTS,HIGH); // Set rs485 control pin high to enable writing to rs485

#ifdef DEBUGGING
  tmp++;
  Serial1.write(&#38;#39;[&#38;#39;);
  char tmp2 = tmp;
  if ( tmp2 &#38;gt;= 20 ) {
    Serial1.write(&#38;#39;2&#38;#39;);
    tmp2 -= 20;
  }
  else if ( tmp2 &#38;gt;= 10 )
  {
    Serial1.write(&#38;#39;1&#38;#39;);
    tmp2 -= 10;
  }
  Serial1.write(tmp2 + 48);
  Serial1.write(&#38;#39;/&#38;#39;); Serial1.write(&#38;#39;2&#38;#39;); Serial1.write(&#38;#39;5&#38;#39;); Serial1.write(&#38;#39;]&#38;#39;); Serial1.write(&#38;#39; &#38;#39;);

  if ( tmp &#38;gt;= 25 )
    tmp = 0;
#endif

  char c = 0;
  while ( MSG[c] != 0 )
  {
    Serial1.write(MSG[c]);
    c++;
  }

  while ( (USART1_BASE -&#38;gt; SR &#38;amp; USART_SR_TC) == 0);
  digitalWrite(pinRTS,LOW);
}

/* * * * * * SOURCE ENDS * * * * * * * * /&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;It's very simple port from arduino, I think it was hard to find, how to do:&#60;br /&#62;
while (!(UCSR0A &#38;amp; (1 &#38;lt;&#38;lt; TXC0)));&#60;br /&#62;
with maple so when I got it working - I decided, I'd better share this for others..&#60;/p&#62;
&#60;p&#62;Everything about debugging can be removed, it's just that I wanted to make a simple test that I use to test if data corrupts or maple hangs, so I wanted to add a line numbering.. Maybe my debugging is a bit bloated, but as I noticed Maple lacks sprintf I decided to just do this quickly..&#60;/p&#62;
&#60;p&#62;Sorry - I don't understand how to use markup on this forum, so bear with me :)&#60;br /&#62;
(Edit - I fixed the code markup - gbulmer)
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
