Serial Communications

Post Reply
alanwms
Posts: 95
http://meble-kuchenne.info.pl
Joined: Fri Dec 04, 2020 2:29 pm
Has thanked: 23 times
Been thanked: 3 times

Serial Communications

Post by alanwms »

Hello:
I have my serial comms working, but on occasion it seems to not work. My setup is simply to poll the serial port and use the serial string to operate.

In order to attempt to use the serial interrupt (instead), I can't seem to make it work. I have been told that gathering a string in the serial interrupt is not the method, and that I should be using a circular buffer. Studying the buffer for me is like completing a crossword puzzle. I don't have a simple example.

So I attempted a different method -
On serial interrupt signal, I simply set a bit and return. At that point, I return from the interrupt and poll the uart. Does not work, and I suspect that the uart buffer may be empty after the interrupt occurred.

I WOULD like to implement the circular buffer, but can't find guidance that meets my understanding.
I have attached a jpg file of the area I'm working on that does not perform.

Guidance would be appreciated.
Attachments
Serial Flag.jpg
Serial Flag.jpg (84.25 KiB) Viewed 560 times

medelec35
Matrix Staff
Posts: 692
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 242 times
Been thanked: 244 times

Re: Serial Communications

Post by medelec35 »

Hello.
Have you looked at the examples with the Wiki entry for the Circular Buffer?
Martin

chipfryer27
Valued Contributor
Posts: 102
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 84 times
Been thanked: 47 times

Re: Serial Communications

Post by chipfryer27 »

Hi

The CB is certainly the "goto" component for my needs and as Martin suggests the wiki examples are a great place to start.

Briefly, enable an interrupt on RX that points to your ISR (Interrupt Service Routine). In your ISR keep things brief. You can grab a Character etc from the UART and then store it in the CB before returning.

In your Main routine you can then poll the buffer for content. You can look for the presence of a specific value / character / string and if found "do something".

Sorry to be brief due to travel. If you need an example or two I can knock something up for you later in the week.

Regards

alanwms
Posts: 95
Joined: Fri Dec 04, 2020 2:29 pm
Has thanked: 23 times
Been thanked: 3 times

Re: Serial Communications

Post by alanwms »

Martin - Yes looked at the wiki, and don't comprehend it. Really wondering why I can't simply retrieve a string at uart interrupt time.

Chip! Would love to get a simple example if you catch the time

chipfryer27
Valued Contributor
Posts: 102
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 84 times
Been thanked: 47 times

Re: Serial Communications

Post by chipfryer27 »

Hi

Delayed boarding (bored time).

I might have some stuff on this machine (using PIC's, but the principles are the same) and will check later. If so, the examples would be using v7 but they easily translate to v9.

In one application I used LoRa to create a "mesh" network utilising just a single communications channel, to allow multiple remote locations to talk to a central receiver, with those out of direct range being relayed. Many challenges as multiple units would receive the message at the same time, potentially forwarding and creating an endless loop in process. CB was integral and without it the project couldn't have progressed (well not with my limited knowledge it couldn't <s>).

If I don't have anything on this machine I'll try and knock up a basic example for you on a PIC in the next day or two, but I'm not sure if the RxInt simulates or not. Usually when testing / playing with UART I use PuTTY / Terminal etc connected directly to the chip to send / receive data as I find this easier than using the Injectors. However without hardware to test........

Regards

mnfisher
Valued Contributor
Posts: 399
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 49 times
Been thanked: 227 times

Re: Serial Communications

Post by mnfisher »

As an aside - I like to look for the termination character in the interrupt handler, and set a flag if it is found. The main loop can wait until this is set before attempting to interpret the input. The interrupt routine must be kept tight..

In a calculation block.
Flag = (.c == '\n')

Where .c is the character received and '\n' is the string terminator (so 0 or ',' etc). This sets the flag to true if the terminator character is found..

In your program loop:

Loop
If flag then process_input
Do stuff or sleep
End loop

Martin

chipfryer27
Valued Contributor
Posts: 102
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 84 times
Been thanked: 47 times

Re: Serial Communications

Post by chipfryer27 »

Hi

Attached is a very simple v8 example, which should work in v9 (only have v8 on this machine). v9 provides you with many more options re USART and CB. I haven't tested this on hardware but I'm pretty sure it should work.

It has a USART, Circular Buffer and a LED. Using the Rx Interrupt to populate the CB ensures that no incoming data is lost.

After initialising, the RX Interrupt is enabled before entering an endless loop.

All the loop does is check for the presence of a keyword anywhere in the Circular Buffer and if found it will flash the Led for two seconds. Of course your loop could be doing anything and if the keyword is found do whatever you wish.

When data is received, the interrupt branches to the ISR Macro. This Macro receives the incoming character, stores it in the CB and then, if desired, echos the data back out on the USART (this is a way to check what is going into the CB and can be deleted if required).

I've have set the keyword as "Test" but it could be pretty much anything.

If you connect a PC running Terminal or such like you can send in any data you wish either character at a time or as a string of characters. When the combination of T, e, s and t (Test) is received, irrespective of where in the buffer, the program will branch and flash the Led. If you send 123Test the Led will come on. If you send Test1234 the Led will come on. If you send test it will stay off. If you send tesT it will stay off. The keyword has to match exactly and as mentioned earlier it does not matter where in the buffer it is.

If you know exactly the phrase or combination you are after, then using just a keyword can make things very simple. However if you need to look for data that varies, then there are better ways to search/parse the buffer.

As Martin said in the previous reply, you could send your data and terminate with a predefined character or sequence. You could poll the buffer for this termination character and if present parse the CB for your data.

I can give you an example of how you could collect data from multiple devices if you wish, by polling the CB, but as mentioned earlier v9 gives you far better options when working with the USART and CB, so if you want an example I'll do it once back on v9.

Hope this helps,
Regards
Attachments
CB-1.fcfx
(13.54 KiB) Downloaded 26 times

Post Reply