Detecting serial comms...

Grogster

Senior Member
I want to detect the presence of serial data, but I don't need to read the data. Once detected, several other things happen, which is why I want to use a PICAXE to control this, but first I need to be able to get the PICAXE to detect serial communications.

I was thinking of using an 08 picaxe for this, and putting an RC filter on one of the input pins. As I say - I don't care what the data is, I just need to detect that it is there.

Data is normal COM1 RS232 signals @ 9600 baud, which I think is about 15v on a good day, so I imagine I would also need a potential divider on the serial input too...

See attached image of what I was thinking.

In the filter idea, the 1k resistor and 10uF cap act as a filter so hopefully the PICAXE would see enough of a constant voltage to allow the IF/THEN routines to treat is as though it was a high on that input. As serial data is positive-going pulses anyway, there should be no negative-going pulses, so perhaps I can omit the diode all together?

Thoughts?
 

Attachments

Last edited:

Ralpht

New Member
From a Pc the serial data is about 12V max if you're lucky and the logic is inverted - a negative voltage for logic hi's (a mark) and logic zero is a positive voltage (a space).

So you are actually looking at negative voltages.

Why not just put a max232 chip in there to get the voltages down to a safe level for the picaxe. Then just use the output serial stream to re-trigger a one shot that will stay at whatever level you need whilst the serial data is there and when the serial stops , the oneshot times out and you then get the level that indicate no serial comms.
 

moxhamj

New Member
Your diode will represent a short when there are negative volts on the input. See attached for one suggestion. When the volts are negative, the cap is discharged. When the volts go positive, the cap charges up quickly and the picaxe responds. When the volts go negative, the cap discharges slowly through the resistor.

You might need a 10k into the picaxe to limit current if the input is true RS232 ie 12-15V. And with a 10k into the picaxe, maybe a 5V1 zener across the picaxe input to ground as well.
 

Attachments

Grogster

Senior Member
From a Pc the serial data is about 12V max if you're lucky and the logic is inverted - a negative voltage for logic hi's (a mark) and logic zero is a positive voltage (a space).

So you are actually looking at negative voltages.

Why not just put a max232 chip in there to get the voltages down to a safe level for the picaxe. Then just use the output serial stream to re-trigger a one shot that will stay at whatever level you need whilst the serial data is there and when the serial stops , the oneshot times out and you then get the level that indicate no serial comms.
No room on the layout for a MAX232, which is mainly why I wanted to do it with discrete components.
Thanks for the suggestion though!
:)
 

Grogster

Senior Member
Your diode will represent a short when there are negative volts on the input. See attached for one suggestion. When the volts are negative, the cap is discharged. When the volts go positive, the cap charges up quickly and the picaxe responds. When the volts go negative, the cap discharges slowly through the resistor.

You might need a 10k into the picaxe to limit current if the input is true RS232 ie 12-15V. And with a 10k into the picaxe, maybe a 5V1 zener across the picaxe input to ground as well.
This sounds like this is a better idea then my first attempt - I will include the zener and 10k too for extra insurance. Thanks.
:)

What value would you reccomend for the cap?
I would image that 1uF would probably be enough at 9600 baud.

What value would you reccomend for the bleed-resistor across the cap?
I would image that about 820R would do.
 
Last edited:

Dippy

Moderator
Dr_Acs circuit looks OK. (Though I'm surprised your first attempt didn't simply RC the PICAXE enhanced circuit. Anyway, DrACs is simpler).

Values - I agree with the Zener, though I would use a lower value, but all sounds good, why not try it?
Maybe a 1k0 or 2k2 would be fine too. Try it.
Got a 'scope to fine tune you design?
No? Get one :)
So useful for 90% of projects. Some projects impossible without one, unless you can persuade others on the Forum....
(I love spending other people's money).
 
Last edited:

hippy

Ex-Staff (retired)
I'd go with Dr Acula's idea, use 22K for current limiting as per the download circuit and not bother with the zener.

Code:
            |\ |                   ____
RX In >-----| >|-----.-------.----|____|-----> PICAXE
            |/ |     |      .|.     22K
                   __|__    | |
                   --.--    |_|
                     |       |
0V >-----------------^-------^---------------> 0V
 

Grogster

Senior Member
Dr_Acs circuit looks OK. (Though I'm surprised your first attempt didn't simply RC the PICAXE enhanced circuit. Anyway, DrACs is simpler).

Values - I agree with the Zener, though I would use a lower value, but all sounds good, why not try it?
Maybe a 1k0 or 2k2 would be fine too. Try it.
Got a 'scope to fine tune you design?
No? Get one :)
So useful for 90% of projects. Some projects impossible without one, unless you can persuade others on the Forum....
(I love spending other people's money).
Actually, I had forgotten the PICAXE enhanced circuit - whoops...
:-[

I do have a scope.
Must get it back from my friend whom i loaned it to about 18 months ago!!!
 

Grogster

Senior Member
I'd go with Dr Acula's idea, use 22K for current limiting as per the download circuit and not bother with the zener.

Code:
            |\ |                   ____
RX In >-----| >|-----.-------.----|____|-----> PICAXE
            |/ |     |      .|.     22K
                   __|__    | |
                   --.--    |_|
                     |       |
0V >-----------------^-------^---------------> 0V
If the RS232 serial input is around 12v(filtered), would I not need the zener to make sure that the maximum applied voltage to the input pin was not more then 5v?

...or are you assuming that the potential divider on the RS232 input is already in place?
 

Grogster

Senior Member
One other question - can I connect this arrangement in parallel to a working serial connection to a computer - I need to have this unit detect the comms, while the laptop actually processes the comms. I assume that I can, but I thought I should mention how I want to wire it up, as assumption is the mother of all...
 

hippy

Ex-Staff (retired)
I don't see any need for a zener, the download circuit works without it. If going to Pin 3 on an 08/08M there's a clamp diode needed to +V ( see PICAXE Manuals ).

There is no, never has been, a "potential divider" on the RS232 input :) It relies on current limiting via the 22K, the 10K just stops the input floating when the cable is disconnected.

You should be able to connect this in parallel to RS232 going to another circuit. The diode stops the original signal being affected. If the C was too high or the R across it too low you might have some problems, but I expect it's unlikely.

As to what C and R should be, that depends on what you're doing in your software, how often you check RS232 is present and how long after it ceases you want to consider it 'not there' any more.
 

Dippy

Moderator
Why not try it? It won't explode.
If there are any problems then reduce the RC values as they may 'un-square' the signal a bit. A lot of it will depend on the neatness of your product.
 

moxhamj

New Member
The 10k/22k works fine for downloads that are at RS232 voltages so will be fine for protecting the picaxe inputs (the circuit also works fine into 74HC chips too). So looking at hippy's circuit on post 7, let's set the resistors as 22k going into the picaxe, and 10k as the discharge resistor for the capacitor.

Now you need to work out the C. Say you want it to discharge in 10 bytes at 9600 baud (it will charge much quicker than that). 9600 baud is roughly 1000 bytes per second. So 10 bytes is 1/100 second. I cheat and rather than get out the CRO, I use the monostable timing nomogram for a 555 timer, which happens to be in the back of most electronics catalogs. 10k and 10ms is 1uF. Now you don't need a zener.
 

leftyretro

New Member
Any thought of using a simple software solution to detecting serial activity. It would seem to me that the count command performed on the serial input pin using a short interval period would work fine. Any result returned other then zero would mean activity has started.

Lefty
 

hippy

Ex-Staff (retired)
Software wise it's a case of simply checking the pin to see if there is any activity, checking fast and often enough and any activity should be seen. The interface would be as described earlier but no C. The C simply allows the input to be checked less frequently, working as a monostable.
 

Grogster

Senior Member
There is no, never has been, a "potential divider" on the RS232 input :) It relies on current limiting via the 22K, the 10K just stops the input floating when the cable is disconnected.
Now why did I not think of that?!??!???!!!!!(rhetorical)

The programming cable is RS232 - WHY DID I NOT THINK OF THAT!!!!

That last "Question" was another rhetorical one, by the way!!!
;)
 

Grogster

Senior Member
retrolefty - yes, I could do it that way, but the possibility presents itself that at the exact instant that the picaxe checks to see if there is any communication on the bus, there would be a "Gap" in the data stream, and so the picaxe would not respond. Using the cap gives me a small "Reservoir" to fill and empty by way of the cap, making sure that the picaxe definetly will respond. That's my reason behind wanting the cap in circuit.

Software wise it's a case of simply checking the pin to see if there is any activity, checking fast and often enough and any activity should be seen. The interface would be as described earlier but no C. The C simply allows the input to be checked less frequently, working as a monostable.
Exactly. I basically want to use the cap, to allow less frequent checks, but still maintain reliable detection.
 

Ralpht

New Member
Depending on your time constant for the cap, you just might run into the same "gap" problem you mentioned. If there is a long enough gap in the data stream relative to the time constant, the cap might discharge enough to make detection a bit flakey. A small cap will discharge very quickly and too large a cap may not charge up enough to give a reliable indication of serial traffic.

Depending on the amount of serial traffic, the voltage on the cap will vary, hopefully not enough to drop below the detection threshold of the Picaxe chip, but that is a possibility you have to consider.

In theory what you are trying to do will work with the circuits mentioned by others, but I would still be inclined to try to fit in a oneshot of some sort. A 555 will do and should not take up too much room along with its associated passive components.

Good luck.
 

Grogster

Senior Member
I hear what you are saying, and I actually like the idea of the 555 1-shot, but there simply isn't room - I have already designed all the rest of the PCB(a mistake, perhaps!), ASSUMING I can use an RC arrangement - me thinks I should have checked here FIRST.

Oh well - live and learn!!!
:p

I'll post back with the results of the testing I plan to do in the next day or so, so the forum knows what happened...
:)
 

moxhamj

New Member
Re "A small cap will discharge very quickly and too large a cap may not charge up enough to give a reliable indication of serial traffic."

That would be true if there was no diode. But with a diode you can charge the cap in a few milliseconds (the time would be dictated by the drive current of the RS232 driver chip), but the discharge time could be seconds. Try 100uF in the circuit below - that will give around a second to detect the signal which should be plenty of time.
 

Attachments

Dippy

Moderator
Dr-Ac's circuit looks good, I can't understand the delay in trying it?
Get that breadboard out and start testing now :)
Also worth trying, if possible, into ST inputs versus TL input.

One question, relating to a comment above;
How long between data detections?
Is it detecting data - big pause whilst doing something - detect data ... etc.
Or is it all very quick with small time gaps between data.
So, will the cap dicharge enough between data detections so that it doesn't detect the same thing twice? i.e. PICAXE think it's new data when it's still the signal from the old data hanging around (As mentioned above).

If it is very quick I'd have a smaller R1 going to a PIC I/O (or a transistor) so that PICAXE can dump the charge on C straight after a detection. You could aso then have a smaller C depending on how often your code samples the pin.
 

Ralpht

New Member
As per Dippy's comment, just build it and try.

Theoretically the cct should work but there are still some issues that may affect reliability.

Re "A small cap will discharge very quickly and too large a cap may not charge up enough to give a reliable indication of serial traffic."

Dr Ac - That would be true if there was no diode. But with a diode you can charge the cap in a few milliseconds (the time would be dictated by the drive current of the RS232 driver chip), but the discharge time could be seconds. Try 100uF in the circuit below - that will give around a second to detect the signal which should be plenty of time.

Only partially true as far as discharge times are concerned.

The diode is effectively isolating the following circuit from the serial stream when the serial data goes negative. When forward biased the diodes resistance is not a factor in this case, therefore the cap will charge very quickly as soon as there is a positive voltage present.
This circuit is a parallel cap // resistor arrangement and that has some quite complex and messy impedence effects that will affect the charge / discharge rate of the cap.

Bear in mind the cap has a 10K resistor in parallel across it and both of them will have approx 10 - 12 Volts across them max, but that will average down to something more in the half the total voltage range - around 4 -6 volts. The 22k resistor only acts as a current limiter for the picaxe to prevent damage from the 12 V onto what is in reality a pin capable of handling 5V max so it can be ignored for now.

The 10K resistor across the cap will cause a rapid discharge of the cap if there is a long enough space between serial data. The charge discharge rates will be well under a second if my rather rough mental calculations are correct - without a calculator it is very hard to figure out exactly what is happening here - but I'd guess at a few hundred milliseconds or much less. The math can go into headache territory very quickly.

It all depends on the data rate of the serial stream.

All this is theory, and the cct will probably work OK, but to me there is a reliability issue and Grogster did mention that he needed reliability. The voltage across the cap will vary and it may actually average at around the level where the Picaxe may not be able to tell if it is seeing a logic hi or lo - ie: there may be false readings that the serial stream has stopped.

To get the 1 second discharge rate that Dr_Ac was mentioning, the cap will really need a series resistor. Initially the 22K resistor might be seen as a suitable candidate for a series discharge resistor but for the 10K in parallel with the cap, which will overpower any slow discharge the 22K resistor will give.

Grogster - It's a pity you designed the board first. Yes you live and learn.
The cct by the good Dr_A most likely will work - build it and see. I tend to get involved with the theory first.

Can you put a 555 on the board - "dead bug" style. IE: glue it on somewhere with the pins up and hand wire the bits to it and the board?

You might also be able to use diode steering to grab the negative going serial data and present it to the cap as well. This will greatly increase the chance of preventing the cap from discharging early. Worth a try?
 
Last edited:

Dippy

Moderator
That was a long one Ralph. I cannot see how that circuit has too many complex paths. It's basically a rectifier with a slightly variable source.

Most of the above is why I suggested using a controllable discharge path like a transistor.
But a lot of the design and component and values depend on the duration of the real data stream and the frequency (i.e. how often).
If you are just detecting a quick blip and there is significant time between blips then the above would be fine.
If detecting the start of a big long stream then perhaps a little more thought is required though I'm sure that could easily be sorted in code. Sorry, I really cannot see the problems that require so much extra thought , albeit a useful cerebral morning workout :)
Suck it and see.
 

hippy

Ex-Staff (retired)
Another solution to discharge the C is ...
Code:
                                              ___
RX >---.---|>|-----------.------.-------.----|___|---> PICAXE
       |          ___    |      |      .|.    22K
       `---|<|---|___|---'    __|__    | |
                              --.--    |_|
                                |       |
0V >----------------------------^-------^------------< 0V
 

boriz

Senior Member
@Dr Acula

With a discharged capacitor and the data line suddenly going from 0v to 12v in very short order, doesn’t that mean an awfully large current briefly being drawn from the COM port, charging the capacitor? What’s the word on COM port current limits? Would a series resistor be prudent?
 

hippy

Ex-Staff (retired)
Providing the C isn't too large it shouldn't be a problem. Adding an R will limit current but will also delay detection of serial present.
 

moxhamj

New Member
It will draw whatever the RS232 driver can provide. RS232 drivers are designed to handle a short circuit - it is part of the "robustness" of the RS232 system. Usually the current is limited to 10mA.

The maximum current supply will dictate the time it takes to charge the capacitor. A bigger capacitor will take longer. (anyone who is keen can work out how long. Rule of thumb - 1 amp charging a 1 farad capacitor will charge at 1 volt per second. We are using *micro* farads and *milli* amps.). So if you use a smaller cap it will charge quicker but also discharge quicker. With 10k and 10mA max inrush and a diode, the ratio of discharge to charge time will still be >100:1. The value of the capacitor ends up being dictated by the loop time the program takes to check the input pin of the picaxe. That could vary as a program grows in complexity. Hopefully not more than 50ms if the code is tight. Ballpark, I'd probably start with 10uF but anything between 1uF and 100uF would do.
 
Last edited:

Dippy

Moderator
Surely, you want the smallest capacitor that you can get away with.
If the RS232 is current limited to 10mA as said by Drac this means that any significant draw may produce a signifcant distortion of the first or first few bits. Not only to the amplitude but to the shape. If the capacitor is too big it could corrupt some of the leading data.
So much of this depends on how much and how often and questions like: does the PICAXE/code need to be able to sense end-of-data and how quickly? (i.e. so it doesn't think it's sensed new stuff when, in fact, its the old stuff still hanging around on a capacitor).
This information hasn't been supplied.
And I assume no-one has tried it? I assume Grogster is trying it right now?
 

moxhamj

New Member
Indeed Dippy, one does hope Grogster is giving his trusty breadboard a thorough workout as we speak! There are so many unknowns here - delay times in particular. But the general principle is that this should be possible with 4 simple components.
 

hippy

Ex-Staff (retired)
This really is one of those cases of try it and see.

I've done serial detection with a PICAXE without any C and that worked for me but it depends on what this software is doing.
 

Dippy

Moderator
I think people have been saying that for a couple of days now :)
Grogster will be wrestling with a red hot breadboard as we speak (type?).
 

Grogster

Senior Member
I know this thread is getting old, but I was checking in with the "User CP", and re-reading some of my old posts, and thought I should report what happened...

Using the 10k/22k circuit for the PICAXE programmer interface worked a charm in combination with the input 914 and a 1uF tantalum cap. This configuration works every time whenever there is serial comms on the line.

What actually happens, is that data is received via a POCSAG paging receiver, and the message transmitted to the pager is output as RS232 data via the receiver. This data is intercepted by the PICAXE circuit, which plays an alert tune when any data is received. The output data is also fed in parallel to the COM1 port of the computer for logging with suitable software. When data is received, the PICAXE plays the sound, and also trips a relay, which is connected to a keyboard-interface, which is then connected to the PS/2 keyboard port on the computer, "Pressing" the [SPACE-BAR] - the relay contacts are across the spacebar contacts inside the keyboard, in other words. When a message is received, this arrangement automatically disables the screensaver(by "Pressing" of the space-bar), shows the message on the computer, sounds an alert sound, and flashes a high-bright 10mm LED mounted on the top of the computer screen for 10 seconds. After a few minutes of no activity, the screensaver kicks back in until the next message.
 

hippy

Ex-Staff (retired)
The output data is also fed in parallel to the COM1 port of the computer for logging with suitable software ... When a message is received, this arrangement automatically disables the screensaver(by "Pressing" of the space-bar)
If you've written your own software you should be able to get rid of the relay and keyboard spacebar interface by sending a system command under Windows which cancels the screen saver.
 

Grogster

Senior Member
Probably, but I could not work out how.
:D
The logging software on the laptop does not have the ability to clear a screensaver when data is received, and when I e-mailed them about it, they said they had no plans to add that as a feature. As I don't know anything at all about programming for Windoze, but I have made several custom keyboards, I just stuck with what I knew would work!!!
:p

If you know of a simple way(perhaps a little application which would disable the screensaver when any data arrives on the COM port) that would be useful for a future update.
(but in that event, I would want the laptop to do the beep sounds too)

I have no doubt this could be done easily in QBASIC or something similar, I have no idea how to make such an application "Share" the same com port with the logging software. Normally, you simply can't do that(share an open com port between two different applications running at the same time) unless it is built into the logging software - the 2nd application will complain that the port is unavailable(as it is being used by the logging software, which opened the port first!).

Anyway...
 
Top