Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interfacing relay output to rs-232 control line and read from PC

Status
Not open for further replies.

iisfaq

New Member
Hi All

I am a newbe here and have a simple question (well I hope it is simple)

I have bought a rain sensor - this basically turns on a relay when it detects rain and turns off the relay when the rain has gone away.

The relay has NO and NC contacts + common.

What I would like to do is to connect the relay to my computer via an RS-232 Interface.

Since I am only interested in the state of the relay ON/OFF I would this would be quite simple to connect to a control line on the RS 232 Interface?

My electronics knowledge is very limited but I can follow instructions and put a simple circuit together but not design one.

Would connecting the output from the relay NO to the DTR (pin 6) or DSR (pin 4) + common to the signal ground (pin 5) be enough for me for me to read the state of the relay from the PC?

The spec on the output of the relay is

Max load 1A 24V

The input voltage to the unit takes 9-30V DC.

At this point I do not know what the output voltage is - maybe 24V - I have not yet recieved the unit.

The unit is available at www rainsensors com and there is a FAQ under the support section.

Any help would be appreciated

Cheers

Chris
 
RS-232 is a bipolar signal: -12v & +12v as spec, -6 & +6 will work. Ground is the "neutral zone", an invalid state.

The easy answer is to have the relay contacts go to request to send (RTS) & clear to send (CTS). RTS is a driven signal from data terminal equipment (DTE:the computer) and CTS is flow control from the data communication equipment (DCE: a modem). It allows the DCE to throttle data and not be overrun.

All that aside: RTS is easily directly switchable on your system & CTS is just as easily read. There are simple calls in visualBASIC for each, with equivalents in other languages.

You can also use data terminal ready (DTR) and data set ready (DSR) in exactly the same way. These are how each side senses the others' existence.

The NO or NC contacts can be used, season to taste... just be sure no voltage is in the contacts: they just switch... G.H. <<<)))
 
Last edited:
I just recently wrote a little program to read / write to the RS 232 port. It can be downloaded here. It is a zip file so just extract it and install it. The program is a real simple affair made to test a RS232 port. Just select the com port you are using from the drop down. If you connect pin 5 (ground) to a 9 volt battery neg. or any low voltage source and battery + to pins 8, 6 or 1 (place a logic high) you will see how it works. Unfortunately I am traveling this week so I don't have the source code with me but making exactly what you want would be pretty simple. The program runs on XP, Vista and Windows 7 both 32 and 64 bit versions. Any questions just ask.

Ron
 
Last edited:
Thanks for the responses - I will try your program and logic tests Ron.

Something like that is pretty much all I am after.

Once I get the device hopefully in the next week I will know a bit more about the relay outputs and see what options I really have.

Chris
 
Hi Chris

The relay will be easy to handle. I should be home around the end of the week with access to my drawing software and other tools. The little program can easily be edited to do whatever you want. For example when raining, it would post it but also use the RTS or DTR outputs to trigger something? Maybe a buzzer or whatever.

Ron
 
Can you post source code?

Hey Brown Out, how goes it? From one of my post in this thread:

Unfortunately I am traveling this week so I don't have the source code with me but making exactly what you want would be pretty simple.

We should be home by Friday / Saturday and I'll be happy to post it. What the hell I should have done (hindsight at work) was included it with the exe file. There wasn't much to it as I recall and it was done in VB 6.0. There was another thread some time back, maybe a few weeks with "comparator" in the title, I think. I posted some code in that thread, not quite what the download is but close.

OK I found the thread part of the code I used looks like this:

Private Sub Command1_Click() 'Start
MSComm1.CommPort = Val(Combo1.Text) 'Select port from combo box
MSComm1.PortOpen = True 'Open the port
Timer1.Enabled = True
End Sub

Private Sub Command2_Click() 'Stop
MSComm1.PortOpen = False 'Close the port
Timer1.Enabled = False
End Sub

Private Sub Form_Load()
Combo1.AddItem "1"
Combo1.AddItem "2"
Combo1.AddItem "3"
Combo1.AddItem "4"
Combo1.AddItem "5"
Combo1.AddItem "13"
End Sub

Private Sub Timer1_Timer() 'Timer set for 100 mS
If MSComm1.CTSHolding = True Then
Shape1.FillColor = &HC000&
Else: Shape1.FillColor = &HFF0000
End If
If MSComm1.DSRHolding = True Then
Shape2.FillColor = &HC000&
Else: Shape2.FillColor = &HFF0000
End If
End Sub

I later expanded that to what the little download is. I have VB here on the laptop but forget exactly how I created that little routine.

Ron
 
For anyone who was interested in the source code I have included it in the downloadable zip folder I linked to earlier and that can be found here. There is now a folder inside the zip folder that includes the VB 6 as well as VB.NET coding. I also caught the fact that on the original download program I had the DTS and RTS outputs reversed. That is fixed. Any questions on the files feel free to ask.

Ron
 
Great thanks for the code - I am sure it will help

Chris

If you want to open the files and don't have VB I suggest you download the Visual Basic 2010 Express software free from Microsoft. You would need VB 6 to open the .vbp files and VB.NET versions like I mentioned to open the newer .sln files. The free software can be had here.

Ron
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top