PDA

View Full Version : RS232 to RS485



The Master
- 24th July 2008, 11:58
Hi, I need to connect my PC to some of my circuits that use RS485. I was going to have a converter built into one of the circuits. It looks like it should be as simple as connecting a MAX232 chip to a MAX485 chip. That should be fine for 1way communications but if i want it 2 way then i need the serial port to be able to switch between RS485 driver and receiver. Would i be able to use those extra pins on the serial port for this? Which ones can i actually use? Im thinking about CTS, DSR etc. Im not sure which ones are inputs and which are outputs though. Do i need any other components between these pins and the MAX485's RE and DE pins? Maybe resistors?

dhouston
- 24th July 2008, 12:17
http://www.lvr.com/rs-485_circuits.htm
http://www.circuitcellar.com/library/ccofeature/perrin0799/c79bp4.htm

The Master
- 24th July 2008, 12:38
The first document makes the choice of pin straight forward. Looks like its RTS then. I re-read the datasheet on the MAX485 chip and noticed something that i didnt notice before. "RO is enabled when RE is low". That helps quite a lot. I thought either RE or DE had to be high to enable either RO or DI. The diagram in the first doc was very helpfull. I thought i was going to need 2 pins from each PIC chip and 2 pins from the serial port for this but it turns out i only need 1.

I didnt see anything about the voltage that comes out of RTS but i guess thats easy enough to measure myself with a multimeter and im guessing its about the right voltage anyway.

Thanx for the help, i think ive got everything i need to know now:)

The Master
- 22nd September 2008, 21:08
Hi again, I finally got around to measuring the voltage that comes out of RTS. It is about 6V when on and -6V when off. That seems to work an LED fine but what about the MAX485 chip? Ive looked at the datasheet and it appears that "low" is classed as below 0.8V and "high" is 2V or more. Is 6V too much for the chip? It doesnt seem to give a max value for that. I would assume the max input voltage is about the same as VCC. Do i need a diode or anything to stop the negative voltage going into the DE and RE pins?

Ioannis
- 23rd September 2008, 21:01
You have to clamp the RTS levels to TTL levels. Or else the chip might get fried.

Why don't you use the 555 circuit idea? It is automatic, no drivers required. Is it necessary to transmit and receive exactly at the same time?

Ioannis

The Master
- 23rd September 2008, 22:23
I have been thinking about that for a while. It looked really complicated at first but i think i get the general idea. The description near the bottom was a bit confusing but the diagram makes sense. Since first looking at RS485 i was going to have the PIC chips switch between sender and receiver. I need data to be sent pretty quick but i dont need quick replies from any of the devices so that would have worked pretty well.

Im doing a little redesigning of the main driver circuit at the moment to add some extra features. The way im doing it might mean i have no choice but to use the 555 way. They are extra components but i suppose they are pretty cheap.

Thanks for clearing that up anyway. Saved me from releasing the mysterious blue smoke again

The Master
- 24th September 2008, 09:28
I think ive finally got a good way of doing it. Im going to use the 555 method in the main controller. The main controller is the bit that connects all the devices to the PC. At the moment its RS232 but i plan to make a USB or ethernet version. All the devices need to be addressed seperately but there isnt really a method to set the address on the device itself so it would mean plugging each one in seperately and assigning an address. Im going to add another wire between each device that is either high or low. The master will make the wire high which will tell the first device on the line to reply. The master will assign it an address then the device will make the wire high for the next device and so on.

That might seem a little complicated but it should do exactly what i want and it also has the advantage of allowing 2 devices to be swapped around. The 2 that are swapped will pick up each others addresses and continue asif they were still in their origional positions. It does ofcourse mean that they wont be hot pluggable but thats fine.

dhouston
- 24th September 2008, 12:09
Assume a one-byte address. Assign each device an initial address of 0xFE. Connect one at a time and have the master send a message to 0xFE to reset its address to 0x01, 0x02,...,0xNN, etc.

The Master
- 24th September 2008, 15:35
The origional idea was similar to that. I normally start all messages with [255] then a command. EG, 254 would mean set address. Any device connected would take the 3rd byte and set it as its address.

The way im doing it now is a little more complicated. Each device will know how far away from the master it is. Its complicated a little further because there are 2 sockets on each device for in and out but it doesnt matter which way you connect them.

There are 5 wires (and a screen):-
1) Power +
2) Power -
3) Data A
4) Data B
5) Not thought of a name for this yet

The master will keep #5 high constantly. The first device connected to it will check both of the sockets to see if #5 is high on it. If it is then it knows that socket is the input. The default is for both sockets to be inputs so #5 will be low by default unless the master is present. This means that only the first device will see a high on #5 so only that device will set itself an address.

Since the last post ive changed the way the addressing works so there is no need for a PIC chip in the master. All devices will default to [0]. The first one to get a high on #5 will add 1 to the address and save it (it is now device 1). It will then switch to send mode and send the address out to all the other devices then switch back to receive mode. All the other devices will use the new address as the default.

The first device will now make #5 high on whichever socket is still low which causes the next device to set itself an address etc.

I know it sounds complicated but it means that all the devices will automatically address themselves. If i connect all the devices in a straight line then it doesnt matter which order i put them in along the line, they will still function in the correct order.

Once all the devices have a unique number that they can be accessed by then i can set a second address through serial. The data would only require the second byte changing and they can be accessed by their specific address or by their position in the line. It saves having to connect one up at a time to address them or having a button on the back that you press to accept the address.

The position address will be reset everytime the devices turn on but the address that is set by sending a command will be saved in the EEPROM and only change when it gets a change command.

Im using the 555 method in the master but not in the individual devices. Each device only needs to send a small amount of data when they first turn on and possibly when a certain command is sent. They should never need to send data during normal operation

The Master
- 14th October 2008, 23:37
Hi, Ive been testing this RS485 stuff out tonite. I had bypassed it before to get everything else working. Now ive tried to get the RS485 bit working and ive found a little problem. The first 1 or 2 bytes seem to get missing from the data. I think its because the 555 doesnt react quick enough to the data. Im using 115200 baud. Is there anything i can do about this problem?

rmteo
- 14th October 2008, 23:47
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2908&stc=1&d=1224024365

The Master
- 15th October 2008, 00:27
So what your saying is scrap the 555 idea and go back to using RTS. I guess that is an option. Ill have to look into how that will affect the software. I know i can easily control RTS in VB using the comm control but i do like to use API for speed. Ive not had the need to use handshaking before so i should probs read up on that.

Thanx for the suggestion and the diagram. Now i see the point in the MAX232 having 2 ins and outs

Ioannis
- 15th October 2008, 08:46
Why not sending some control characters then wait for the start character, dump it and get the next.

Like this:

Send "#######$data"

on PIC side:

Hserin [wait("$"),str mydata\data_bytes]

Ioannis

The Master
- 15th October 2008, 09:39
I suppose thats a possibility. Im using the hardware UART so its a little harder but still possible.

Is there anything faster than a 555? What about bypassing it? I noticed the output from the MAX232 chip seems to be inverted but if i could invert it again and connect it and the 555 through resistors to the MAX485 then it should be a lot faster because it doesnt rely on the 555 to turn it on, the 555 will simply keep it turned on for a short while after the data stops.

I guess the only problem now is that the MAX485 chip might be too slow to react too

arniepj
- 15th October 2008, 13:15
I have used the 485SD9TB converter from BB Electronics with no problems.It does exactly what you are trying to do.Its not cheap,but you can check out the schematic and maybe reproduce the circuit.

The Master
- 15th October 2008, 20:46
Ive tried the RTS method. It takes quite a while to turn on using software. Im only talking a few ms but its still enough to cause problems.

I think a circuit based on that scematic would be best. I dont have any logic gates that i can make a NOT gate out of so i cant test it yet.

I will have a go at the control characters method. That one seems like it should definately work aslong as i can program it properly. At the moment it relies on counting the bytes that arrive. With the control characters 1 or more will be missing.

dhouston
- 15th October 2008, 21:28
The first 1 or 2 bytes seem to get missing from the data. I think its because the 555 doesnt react quick enough to the data. Im using 115200 baud. Is there anything i can do about this problem?I doubt very much whether it's the 555. That method is widely and successfully used for automatic control of line direction. The fact that subsequent bytes are OK would also seem to indicate it's not the 555. Are you using termination resistors? They are absolutely necessary at 115200.

The Master
- 15th October 2008, 22:00
I am using a termination resistor at the end with the 555 aswell as 2 biasing resistors exactly as shown in the diagram in your first post. I have the RS232 to RS485 converter plus 2 lights on breadboards at the moment. The MAX485 chips are less than 5cm apart but i can add another terminating resistor at the end just to be sure.

Ive been thinking about this one and i think i got it the wrong way round. The thing that got me thinking is that the offset is constant even if i stop sending data then start again. You would expect it to keep shifting one or 2 bytes further out everytime the 555 gets chance to turn back off. I looked at how the circuits reacted to the data and it seems they are actually getting an extra byte or 2. I think that may be to do with the way ive programmed it but now im looking in the right place i have a better chance of solving the problem.

I do like the idea of using a 555 because it allows me to have a status light that doesnt flash too quick to see and doesnt need any other components

The Master
- 15th October 2008, 22:47
It turns out it was a programming problem. As the chips start up the first one sends out a byte to tell the others how many chanels it has then goes into receive mode. The second one then does the same thing but the byte it sends out gets counted as data by the first light. Im really not sure why it was sometimes 2 extra bytes but ive reprogrammed it and it seems to be working perfect now. Sorry for wasting your time

dhouston
- 16th October 2008, 00:41
You're now an expert who can help the next RS485 noob.

The Master
- 16th October 2008, 15:03
Haha, I wouldnt say 'expert'. Remember im the one who just got it wrong. I havnt tested with long wires yet so no doubt ill have more problems

skimask
- 16th October 2008, 15:11
Haha, I wouldnt say 'expert'. Remember im the one who just got it wrong. I havnt tested with long wires yet so no doubt ill have more problems
If you're using drivers designed for '485 and you design the circuit within the '485 specs, why would you have problems with long wires? Shouldn't... Could happen...but shouldn't...

The Master
- 16th October 2008, 15:37
It "could happen" so it probably will the first time i try it. Im almost out of CAT5 cable so i guess it wouldnt hurt to buy another box and test it over the full length (about 300m). Ive just been reading a page about some of the things that can go wrong but i think ive got most of them covered. There might be a problem if the main box isnt connected because none of the lights bias the line themselves. I think thats a good thing because aparently too many devices biasing the line can stop data from being transmitted. Once the main box is connected they all need turning off and back on again before they can work anyway.

Another idea is to put the biasing resistors in the terminating plug. Its possible because once the lights are turned on there will be both positive and negative voltages coming out of the socket. I doubt ill need to do that though

I would like to connect up way more MAX485's than the specification allows and see how many it takes to stop it working just to make sure everything will be fine. I think the limit was about 30 unless i have those other ones that allow 128. I might need a bigger breadboard for that :P