# Switching serial DATA between two sources

#### Pommie

##### Well-Known Member
It was never a good idea in the first place, I just tried to come up with a hardware solution that "might" work.

Mike.

#### dknguyen

##### Well-Known Member
Hi D,
When you say receiver I do you mean the PIC RX PIN and program?

If the RX has received a mid sentence transmission, and is 'in error' when the next sentence arrives from, are you saying it won't 'see' it? There is quite a gap between sentences.

C.
Yes on the PIC RX Pin.

I don't see a gap in sentences so you are going to have to point it out so I can clarify. Do you know how UART works with start/stop bits and the timing? Because if you don't then my explanation won't make sense. There is no magical way for a UART receiver to distinguish between a data bit and the start/stop bits in a transmission that it has jumped into the middle of. It relies on timing to do this which it doesn't have until there is a sufficiently long lull in transmitted messages where nothing is happening.

Last edited:

#### camerart

##### Active Member
Yes on the PIC RX Pin.

I don't see a gap in sentences so you are going to have to point it out so I can clarify. Do you know how UART works with start/stop bits and the timing? Because if you don't then my explanation won't make sense. There is no magical way for a UART receiver to distinguish between a data bit and the start/stop bits in a transmission that it has jumped into the middle of. It relies on timing to do this which it doesn't have until there is a sufficiently long lull in transmitted messages where nothing is happening.
Hi D,
First I'm making sure the program etc work, using manual switch over, while receiving the two channels via two separate HC-12 radio modules. I can send DATA from a second computer.

To clarify how I think the sentences will arrive, LIVE:
The GPS transmits 1/sec and sends about 7x different NMEA sentences.
The single sentences sent from BASE will be as fast as possible, I'm not sure, but I imagine many times/sec.

I think I understand how the UART bits work, but if I'm not mistaken, the program rejects false sentences, then waits for the next '$' which is the start of both types of sentence. (I didn't write the clever sections of the program) Once I've proved with manual switching, I'll try some tests and give some feedback, and Oscilloscope photos. (I've only had it a week, but I may be able to show a long string) C. #### dknguyen ##### Well-Known Member Most Helpful Member I think I understand how the UART bits work, but if I'm not mistaken, the program rejects false sentences, then waits for the next '$' which is the start of both types of sentence.
This, here. is the problem. If the GPS fires off 7 NMEA sentences or BASE continually fires sentences with no delay in between the messages, the MCU will never be able to pick up the $symbol, or the start bits, or the stop bits,since it will be jumbled in with everything else. Imagine this: Imagine that I am standing in a room continually spewing out the digits to a series of 3-digit numbers. You know the numbers are 3-digits long but imagine I never pause in between the digits. So I just say 345123789... if I want to say 345, 123, 789... The only way you would be able to interpret what I'm saying is if you knew when the first digit of any particular number was. If you walked in partway through you would have no idea how to group the digits together into numbers since I do not pause in between the digits. Even if we agreed beforehand that the special number 314 signifies the start of a string of 3-digits numbers, this still would not help since if you walked halfway through and heard "...314123456..." you would have no idea if the 314 was grouped together to be the special starting number since it could just be that the 314 is split between two consecutive 3-digit numbers. I could have been trying to say something like "731, 412, 345, 6..." but you missed the first digit. If you interpreted the 314 that appears there as the an entire 3-digit number and interpreted everything else I said afterwards based on that, everything will be wrong. In that example, your sentences are like the 3-digit numbers, and your$ is like 314. Without a delay longer than a serial-word (8-bits) your MCU RX will never be able to know when a symbol begins. The delay gives the MCU a unique scenario that it can pick up on because it can say to itself "okay, the line has been quiet for long enough that I know that a bit of some kind (either start, stop or data) should have already arrived. Since it didn't arrive then the serial messaging timing doesn't match up for valid data so I know I am in an truly in idle state so the next thing that comes will be a start bit".

A similar example is if you started reading an english paragraph in the middle that had no capitals, periods, or spaces. Except it's worse than that since in English you have the benefit of rather unique combinations of letters making up valid words so you can kind of pick out when a word should start and end. With a UART it's just a string of ones and zeroes and almost any combination could be valid data.

Picking out a random section in the middle of that previous paragraph with those rules looks like this:

Last edited:

#### jjw

##### Member
Would it be better to use a PIC with two hw UARTs, one for GPS, one for BASE.
Maybe even one sw, one hw UART.

#### Pommie

##### Well-Known Member
Would it be better to use a PIC with two hw UARTs, one for GPS, one for BASE.
Maybe even one sw, one hw UART.
That would be far too sensible. Much better to add lots of additional hardware.

Mike.

#### JonSea

##### Well-Known Member
I don't know what the serial stream sources are, but if it's possible to control when each device transmits, this problem goes away.

For example, if each device can send data in response to a serial command rather than sending data continuously, a NAND gate switch like Mike showed would work well.

#### camerart

##### Active Member
Would it be better to use a PIC with two hw UARTs, one for GPS, one for BASE.
Maybe even one sw, one hw UART.
Hi J,
Much has changed since I started this project. I didn't know how the PWM section was going to be, and how much memory I would need or how many PINs I would need and much more.

As you can see by this thread, I did ask, with what I knew then

I've got shelves of PCBs and PICs that have been superseded, but if you can call this fun, then I've had lots of it, and 'met' some helpful people along the way, and I've still not finished.

MERRY CHRISTMAS to all of my helpers and a HAPPY NEW YEAR

Camerart

#### camerart

##### Active Member
This, here. is the problem. If the GPS fires off 7 NMEA sentences or BASE continually fires sentences with no delay in between the messages, the MCU will never be able to pick up the $symbol, or the start bits, or the stop bits,since it will be jumbled in with everything else. Imagine this: Picking out a random section in the middle of that previous paragraph with those rules looks like this: tedreadinganenglishparagraphinthemiddlethathadnocapitalsperiodsorspacesexceptitsworsethanthatsinceinenglishyouhavethebenefitofratheruniquecombinationsoflettersmakingupvalidwordssoyo Hi D, Thanks for your detailed answer. I understand what you mean. The program I use has a section where the two INPUTs are PARSED. I didn't write it, but I'm pretty sure that if it receives a line as yours, above, it ERRORS and flushes everything out and starts again. I've been trying different tests: I had one computer transmitting$BASE sentences while a second computer connected serially to where the removed GPS was, sending three different $NMEA sentences, one after the other. Obviously the two computers aren't in sync, although they both are sending at 1/sec, so possibly could not coincide. This worked and the program PARSED the sentences alternatively, and replied with the correct result shwon on both terminal RX's. Next I'll try similar, but with multiple$NMEA sentences joined together.
C.

Last edited:

#### camerart

##### Active Member
Hi,
I've now got the SWITCH to switch, I can give a fuller explanation if asked.
Thanks to all,
C.

#### dknguyen

##### Well-Known Member
What was the problem?

#### NsrMagazin

##### Member
The image you gave is a PCB. It will be much easier for us if you give a circuit.

For RS232 or variations communication, I recommend this circuit. It can only connect an MCU with 2 secondary(slave) MCUs. For the use of 4 secondary(slave) MCUs it needs to be edited.

#### Attachments

• 80.1 KB Views: 3

#### camerart

##### Active Member
The image you gave is a PCB. It will be much easier for us if you give a circuit.

For RS232 or variations communication, I recommend this circuit. It can only connect an MCU with 2 secondary(slave) MCUs. For the use of 4 secondary(slave) MCUs it needs to be edited.
Hi N,
I've added a SCH to #5
C

#### camerart

##### Active Member
What was the problem?
Hi D,
Each BASE or NMEA sentence has a $at the beginning and a LF at the end. My program looked for a$ then either the 'E' of the BASE or the 3rd 'G' of the GPGGA NMEA sentence. Then it PARSES out the information, and switches to the other source.

As there are multiple NMEA sentences, there are multiple \$...LF so the switch would have been toggled, and I think never got back in time for the GPGGA sentence which is down the list.

The GPGGA sentence was chosen because it has TIME,LONG,LAT and ALT. Remembering that I now have a barometer/altimeter on the PCB, and the first sentence has TIME,LONG and LAT, I changed to that one, and it now works.

So we have established that the SWITCH works ok.

C.

#### unclejed613

##### Well-Known Member
you could also do this with two AND gates, an inverter, and an OR gate. CS2 goes to the inverter and one of the AND gates. The inverter output goes to the other AND gate. the two other inputs on the AND gates are your two serial inputs. the outputs of the AND gates go into the OR gate. the OR gate output is the switch common.

#### camerart

##### Active Member
you could also do this with two AND gates, an inverter, and an OR gate. CS2 goes to the inverter and one of the AND gates. The inverter output goes to the other AND gate. the two other inputs on the AND gates are your two serial inputs. the outputs of the AND gates go into the OR gate. the OR gate output is the switch common.
Hi U,
My switch is working, but thanks.
C.

#### jjw

##### Member
Why did you make this thread here?
Doesn't it belong to Microcontrollers/Oshonsoft?

At least the compass raw values are again in wrong order.
Why are you using different loop counters, when one is enough, for example i
The same with buffer array, maybe there are more.

Last edited:

#### camerart

##### Active Member
Why did you make this thread here?
Doesn't it belong to Microcontrollers/Oshonsoft?
Hi J,
True, I've moved it, ta.
C

Last edited: