RS-232 to RS-485 converter

Discussion specific to projects ideas and support.
Post Reply
nxtreme
Posts: 246
Joined: 29 Sep 2010, 03:53
Location: 192.168.1.2

RS-232 to RS-485 converter

Post by nxtreme »

First, I know that 5V communication levels really don't count as RS-232, sorry :).

Second, a bit of an idea of what I'm trying to do. I'm wanting to create something that is somewhat similar to the Mindsensors PSP-NX but using Picaxe microcontrollers on both ends, on the hacked game controller and one on the robot, acting as a go-between. My idea is to have a Picaxe 20X2 (my favorite Picaxe by the way) reading the joysticks (potentiometers) and buttons in the game controller and then sending them wirelessly over the SparkFun 315 Mhz 4800 baud RX/TX pair (woah, long mouthfull) to a receiving 20X2 (or 18M2) on the robot that will interpret values, toss out bad bytes and then send them on over to the NXT. The second 20X2 would also be able to control servos, motors and maybe do some basic navigation/triangulation using IR and a compass.

Third, my problem. Originally I was planning to communicate between the Picaxe and the NXT using the Picaxe as a I2C slave. The Picaxe would write some values to its scratchpad and then the NXT would read those values as it would read a I2C EEPROM. However, after a bit of research I found out the the scratchpad's memory would wear out after about 100,000+ writes (or about 170+ hours at 10 updates a second). I thought, for a little bit more $$ why not stick a I2C RAM or a serial to RS-485 converter in there and making it something that'd last for a good deal longer? I figured that the 485 converter route would be the simpler way to go, even if I had to learn another language (I'm thinking about NXC) to do so.

Fourth, the question (Finally!!! I hear you say). Now, it comes down to finding the right converter to plop in-between the Picaxe and my precious NXT. I first found the SparkFun SP3485 breakout. However, $10 US is too much to spend, I think. I could get two MAX485s for that price. Which brings me to my next option. After a bit of searching (yes, I did do the mandatory Google search :)) I found this blog post about connecting a NXT and an Arduino over 485 using a MAX485 chip. I'm pretty sure that I could just substitute a Picaxe (or a FTDI chip so I could talk to the computer!) for the Arduino, write some code and begin talking. However, I'm cheap. I'd rather spend $1 dollar than $5 most times, even if it means a bit of extra work.

Ok, another option. I found a link on the SparkFun converter page to a tutorial that pointed out that the SN75176 chip is pretty much the same as the MAX485 but a lot cheaper (~$1 VS ~$4 dollars). Disadvantages are that it has a higher power consumption; advantages are that it that it can handle higher communication speeds. I don't think I'm worried about too much about a extra couple mA of power being used and even though the 20X2 can handle serial speeds up to 115,200 baud (with overclocking) I don't think I need to get up that high. 4800/9600 would work to begin with, might bump it up later if I'm desperate for more speed.

Last option that I've considered. The DS485 was mentioned as NI's substitute for the MAX485. If the SN75176 just doesn't cut it I could get it instead as it runs at about ~$3 dollars. It actually seems to be superior to the MAX485; long distance industrial designs seem to prefer them as they (supposedly) have more built in protection against over-voltage and ESD.

Fifth, software. RS-485 only defines the voltage levels and such, no protocol is defined like when using RS-232 or I2C. According to all the tutorials ect. that I have read, I would need to create my own protocol. Now, I wouldn't mind doing that as it might be rather fun and all but I'm wondering what I have to work with on the NXT end. Is there any kind of protocol defined? If not, how hard would it be to write my own? Something like a 2-10 byte message containing power levels for both drive motors, information coming from the IR beacons so that I could do some triangulation/location calculations, amount of dropped messages coming from the controller (IE, coded safety tether) and maybe some other info (ideas anyone?). Another limitation I have to work with is memory space. The 20X2 has 2048 bytes of program space, along with the ability to load programs from an external I2C EEPROM. I would want to reserve at least half that for general use, the other half (or preferably less) I could use for the protocol. I have been thinking it might be easier to just create a 232 like protocol on the NXT, enabling me to use existing commands on the 20X2, opening up a lot more space for other things. I'm not sure if this would work, although, I don't know anything about existing protocols on the NXT.

To sum it all up. What chip do you recommend I use? What kind of protocol does the NXT use for the 485 port? Would it be possible to work with existing 232 protocols so that I don't have to bother with software quite as much on the Picaxe end? Is there any existing work out there that I might find interesting? Thanks for actually reading all of this very long post, I hope it didn't end up too long. I'm very open to suggestions right now, suggestions about pretty much anything that this project involves. Thanks!
One King to rule them all, One King to find them,
One King to bring them all and in the darkness bind them
On Earth where Shadows lie.
nxtreme
Posts: 246
Joined: 29 Sep 2010, 03:53
Location: 192.168.1.2

Re: RS-232 to RS-485 converter

Post by nxtreme »

Well, I've decided to go with the SN75176 chip. I'll get a couple of them and hope that I chose right :). Does anyone know of any examples/tutorials aside from the ones I've pointed out on how to use the 485 port on the NXT? Or a page or something that explains the protocol the NXT uses? I just want to make sure that this is actually possible with the Picaxe before pulling out to much hair...
One King to rule them all, One King to find them,
One King to bring them all and in the darkness bind them
On Earth where Shadows lie.
cghaning
Posts: 9
Joined: 30 Sep 2010, 03:39

Re: RS-232 to RS-485 converter

Post by cghaning »

nxtreme --
1. If memory severs me correct, most laptops and pc's manufactured recently (past 10 years or so) will accept 5V RS232 signals just fine
2. Your idea seems possible and mostly reasonable. You could probably ditch the second Picaxe controller and do that processing with the NXT....
3. RS485 could potentially be much faster than I2C (although you are limited by the RF transmitter you are planning on using) -- but I think it would be great because not many people have touched the RS485 capability on port 4.
4. I've done some work with the Port 4 RS485 port, but it definitely has been a while (and limited). I ended up using the TI SN65HVD1786 because it can withstand ESD, being wired up incorrectly, or even short circuited. I also purchased a FTDI USB-COM485-Plus1 so I could just dump stuff directly to my PC (using a software called RealTerm). http://www.ftdichip.com/Support/Documen ... -PLUS1.pdf
And as far as the cost of various IC's go i've got one word for you -> S-A-M-P-L-E-S. Most IC vendors have a sample program whereby they will GIVE you chips at no cost. In fact, most of the IC's I have are no cost samples.

5. The NXT follows normal serial 'protocol' i.e. Baud Rate, Data bits, Stop Bits, Parity, which most are configurable because of the wonderful Enhanced firmware by John Hansen. See http://bricxcc.sourceforge.net/nbc/nxcd ... tants.html for a description of the definition of the options that are available. I'll throw in the caveat that some of the data bits / parity settings are not fully tested.

For simple point to point application of port 4 (by this I mean only 1 device connected via port 4) -- the only issue is the possibility for something called bus contention. Since the RS485 on the NXT is half duplex, somehow you have to maintain orderly communication or no one will be able to communicate. This means disable the transmitter portion of the RS485 port whenever you aren't transmitting. The NXT handles this internally, so you just have to make sure whatever device you are developing is playing fair. Search google for RS485 half duplex or bus contention for more details.

If you want to extend to multiple devices on port 4 (which is also possible) -- this is really where a method to the communication becomes very necessary. This is because being half-duplex, if more than one device is driving the bus (i.e. transmitting), the data will be scrambled and useless. I would suggest reading about packet based and token based communication protocols.

OK, before I explain one of the setups I had used the RS485 port for, here are some links I found useful:
http://www.robustdc.com/library/san003.html
http://www.bb-elec.com/tech_articles/rs485_basics.asp
http://www.bb-elec.com/tech_articles/rs ... ntents.asp
http://www.lvr.com/rs-485_circuits.htm
http://realterm.sourceforge.net/

Alright, here was my setup:
Lego NXT Connections were as follows (Port 4 Only) to the SN65HVD1786
Vcc = NXT Wire #4 (Green)
B = NXT Wire #5 (Yellow)
A = NXT Wire #6 (Blue)
Gnd= NXT Wire #2 (Black)

I'm using a SN65HVD1786 from TI, (equivalent to LT1486,MAX487,SN75176) with the following advantages:
-- ESD tolerant (in case of static!)
-- Fail safe receiver (for mis-wiring to NXT)

I had this connected to a Parallax BS2.
NOTE to forum admins: I wanted to attach the program as a file -- but the forum won't accept .nxc or .txt as a file extension!

So here it is: (note that the BS2 code is embedded in the comments)

Code: Select all

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*  Build a string formatted as:
*  "%d,%d;", where values are Motor Degrees A, Motor Degrees B respectively
*  and send thru RS485 port at:
*  9600 baud,8 bit,No polarity,1 stop bit
*  Equivalent to 1066 chars/second
*
*  Testing done wired to Parallax BS2 by using
*  a SN65HVD1786 (RS485 to TTL Serial converter from TI, and many similar parts
*  exist such as LT1486,MAX487,SN75176
*  (Wiring schematic forthcoming)
*
*  Lego NXT Connections were as follows (Port 4 Only):
*  Vcc = NXT Wire #4 (Green)
*    B = NXT Wire #5 (Yellow)
*    A = NXT Wire #6 (Blue)
*  Gnd = NXT Wire #2 (Black)
* Parallax BS2 Code to receive data:
--------------------------------------------------------------------------------
' RecievefromLegoNXT.bs2
' Written by Charlie Haning March 14th, 2009
' Example code recieves data sent by the
'  Hi speed Port 4 on the NXT and outputs to PC serial port at 9600-8-N-1
'  Utilizes RS485 to TTl converter
'
' References (From which the code below relies on heavily):
'
' http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol1/col/nv28.pdf
' http://www.parallax.com/dl/docs/prod/appkit/rs485Communication.pdf

xm_rcv CON 0   ' Pin 0 sets transmit (1) or receive (0).
serIO CON 1    ' Pin 1 is used for serial input/output.
T1200       CON     813
T2400       CON     396
T4800       CON     188
T9600       CON     84         'Equivalent to 9600-8-N-1
T19K2       CON     32
T38K4       CON     6
SevenBit    CON     $2000
Inverted    CON     $4000
Open        CON     $8000

'The constants above are used with simple arithmetic
' i.e. for 9600 baud Seven bits -> T9600 + SevenBit

txt VAR Byte(16)
DIRS = %111111
LOW xm_rcv ' Set 485 transceiver to receive mode, HIGH xm_rcv for transmit mode
again:
SERIN serIO,T9600,[STR txt\16\";"] ' Wait for id, then receive string.
DEBUG CLS, STR txt,CR
GOTO again
--------------------------------------------------------------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
task main()
{
  // Set Port 4 to High Speed Port
  SetSensorType(IN_4, SENSOR_TYPE_HIGHSPEED);
  // Set Communication Parameters & Init High Speed
  RS485Uart(HS_BAUD_9600,HS_MODE_8N1);
  string msg;
  long val;
  while(true) {
    msg = NumToStr(MotorRotationCount(OUT_A));
    msg += ",";
    msg += NumToStr(MotorRotationCount(OUT_B));
    msg += ";";
    SendRS485String(msg);
    Wait(20);


  }
}
nxtreme
Posts: 246
Joined: 29 Sep 2010, 03:53
Location: 192.168.1.2

Re: RS-232 to RS-485 converter

Post by nxtreme »

First, sorry for the slow reply. I was traveling and didn't have access to a computer or internet for a couple days.
cghaning wrote:nxtreme --
1. If memory severs me correct, most laptops and pc's manufactured recently (past 10 years or so) will accept 5V RS232 signals just fine
2. Your idea seems possible and mostly reasonable. You could probably ditch the second Picaxe controller and do that processing with the NXT....
3. RS485 could potentially be much faster than I2C (although you are limited by the RF transmitter you are planning on using) -- but I think it would be great because not many people have touched the RS485 capability on port 4.
4. I've done some work with the Port 4 RS485 port, but it definitely has been a while (and limited). I ended up using the TI SN65HVD1786 because it can withstand ESD, being wired up incorrectly, or even short circuited. I also purchased a FTDI USB-COM485-Plus1 so I could just dump stuff directly to my PC (using a software called RealTerm). http://www.ftdichip.com/Support/Documen ... -PLUS1.pdf
And as far as the cost of various IC's go i've got one word for you -> S-A-M-P-L-E-S. Most IC vendors have a sample program whereby they will GIVE you chips at no cost. In fact, most of the IC's I have are no cost samples.
1. Yes, in fact, I've found that a serial port is more robust than a FTDI chip as it will accept lower voltages as "high" than the FTDI chip will.
2. Yes, I probably could. However, I wanted to have the second Picaxe there so that I could handle "broken" packages and only pass off the good ones to the NXT. Although, the multitasking capabilities of the NXT might come in handy... I'll have to look into that.
3. My RX is limited to 4800 baud. However, I don't have to make the Picaxe talk to the NXT at that speed. I could pass off the data to the NXT in short "bursts" at a higher baudrate so that I could spend less time talking to the NXT, more time listening for instructions over RF.
4. I checked out the SN65HVD1786 but as it is more expensive than any of my other options I think I might stick to DS485 or the SN75176. Like I said before, the DS485 seems to have quite a bit of protection built in. I do have a FTDI breakout so I can dump stuff to my PC.

About samples. I know that you can get them but I never have. I'm going to get some other chips on Digikey anyways so I thought that I would just order everything from them. However, I might just get some samples of the SN65HVD1786, the DS485 and the SN75176. That ways I can experiment to see what works best :).

cghaning wrote:5. The NXT follows normal serial 'protocol' i.e. Baud Rate, Data bits, Stop Bits, Parity, which most are configurable because of the wonderful Enhanced firmware by John Hansen. See http://bricxcc.sourceforge.net/nbc/nxcd ... tants.html for a description of the definition of the options that are available. I'll throw in the caveat that some of the data bits / parity settings are not fully tested.

For simple point to point application... token based communication protocols.
5. Wow! That was pretty much what I was looking for. I will probably need more help later on but that is great! Thanks!

For starters I probably will have this set up as a one-sided conversation. The Picaxe talks to the NXT, the NXT listens but doesn't say anything back. I will make it two way as soon as I get the basics setup though. Thanks for that word of warning, you probably saved me a couple days of scratching my head :). I have been thinking that if I do set up multiple devices on one bus I will set one as master. The master can initiate communication with the slaves but not vice-versa. I'm open to suggestions though. And thanks for the ones you already gave me, I will read up on them.
OK, before I explain one of the setups.... So here it is: (note that the BS2 code is embedded in the comments)
Thanks for the links and the code. It's nice that the Stamp BASIC and Picaxe BASIC are so similar.

I do have one more question. In my browsing around I read somewhere that you don't want to connect any two 485 devices with less than one meter of cable. I think it's because of the fact that most devices are designed to work over distances of at least 500 meters. I want to connect the Picaxe right to the NXT (with all the right circuitry of course). The distance between the two might be 5-25 centimeters at most. Should I be worried at all? Should I maybe connect a low value resistor (10-15 ohms?) in series with the com lines? I haven't thoroughly read any of the links you gave me yet, maybe I'll find the answer in one of them :).

Thank you very, very much for taking the time to explain some stuff to me. I won't be able to experiment for a while yet (chips will probably take at least three weeks to arrive) but if/when I do get something working I will post some pictures ect.

Thanks!
One King to rule them all, One King to find them,
One King to bring them all and in the darkness bind them
On Earth where Shadows lie.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests