RS232 refresh

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

RS232 refresh

Postby achillis1 » Mon Aug 24, 2015 5:42 pm

Hello,

I have 2 problems. :)

1. I have used the RS232 component and set it to use hardware flow control and while testing in real life ( I connected MCU and wifi module) I left UNCONNECTED the CTS / RTS pins and still the 2 devices were able to communicate? Any thoughts? !

2. I am using the the rs232 component and the routine goes as follows : RS232, receiveString, timeout255, N_bytes 13, printLCD-received string, RS232-send received String end of routine.
While using this routine I successfully receive the incoming data and the incoming data are replied back in one piece as well.

Now, if I use a loop in order to continuously listen the RS232 port, the incoming data sometimes are OK sometimes scrabbled! So as the replied data- even if I use send RS232 string "hello" is there is a loop the wifi module cannot receive it as expected.

Examples of received data:

"hello12345678"
and after some loop cycles:
"78hello123456"
"45678hello123"
"..llo.23.45678"


Same can happen with the send data.

Any thoughts?

Thank you,

Best Regards,

Andreas Achilleos

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Tue Aug 25, 2015 5:29 pm

Hello Andreas,

1. I have used the RS232 component and set it to use hardware flow control and while testing in real life ( I connected MCU and wifi module) I left UNCONNECTED the CTS / RTS pins and still the 2 devices were able to communicate? Any thoughts? !


The flow control pins of the module might have resistors to allow the flow control to be disabled if disconnected. The microcontroller's inputs will also float if disconnected allowing communications to flow.

if I use a loop in order to continuously listen the RS232 port, the incoming data sometimes are OK sometimes scrabbled!


Are you using a USB to serial converter? The USB controller may be collecting bytes together as they arrive to try to make efficient data packets. The circular buffer is a good way of receiving and checking for incoming values if your currently using the string compare function.
These users thanked the author Benj for the post:
achillis1 (Wed Aug 26, 2015 2:45 pm)
Rating: 5.56%
 

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: RS232 refresh

Postby achillis1 » Tue Aug 25, 2015 6:30 pm

Hello Ben,

Thank you for the reply.

The flow control pins of the module might have resistors to allow the flow control to be disabled if disconnected. The microcontroller's inputs will also float if disconnected allowing communications to flow.


I do not quite understand! Never the less, so by floating means that RTS/CTS are considered to be ENABLED so always the MCU will be able to receive and send?


Are you using a USB to serial converter?


I am using plain wire connection, TX mcu > Rx wifi module, Rx MCU > Tx wifi module.
Even if I use the flowcodeV6 example of circular buffer the same happens; when the routine is as is in the example, I mean that it will run once only and stop, then the data exchange is OK.
If I put the routine in a loop so that it will not stop at the first run then I get scrubbled data, as if the data begin to go into the incoming register later or sooner resulting the following format: if we say return value size incoming string set at the macro details = 10
Send data = abcdefghij
received data when routine is run once : abcdefghij
received data when routine is under loop : ..cdefghij or abcdefg... or at the most cases : ..abcdefgh and at the other run ijabcdefgh

what it seems at the second and more frequent case is that the RS232 component got late receiving the incoming string "abcdefghij" and for the 0 and 1 byte of incoming string it got NOTHING and filled the rest of the 8 bytes. The result of that is when the next string of data arrives there are already 2 bytes in some buffer; that 2 bytes are the last 2 = "i" and "j", so at the next print screen of the incoming string the data are as follows i j a b c d e f g h and so on . Sometimes goes as follows i j c d e f g h i j


*Is there a way to clear the incoming buffer or reset the register pointer before the macro receives any data?
Any ideas :)

Thank you ,

Best Regards

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Wed Aug 26, 2015 11:41 am

Hello Achillis,

The hardware UART has a two byte buffer. It will receive up to two bytes and store them. If a third byte is received before you empty the buffer then this will be lost.

The buffer is emptied by reading a byte or a string. If you perform two dummy byte reads then this should clear out the contents of the buffer allowing new data to be received.

When I use the circular buffer component I use it with the UART receive interrupt so that data is immediately transferred from the UART buffer into the circular buffer as it is received this should completely get rid of any UART buffer problems.
These users thanked the author Benj for the post:
achillis1 (Wed Aug 26, 2015 2:45 pm)
Rating: 5.56%
 

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: RS232 refresh

Postby achillis1 » Wed Aug 26, 2015 2:54 pm

Hello Ben ,

Thanks for the reply..again.. :)

Can you please tell me how to perform a dummy byte read?

When you say "UART receive interrupt" you mean the UART RXINT0?

Thank you,

Best Regards

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Wed Aug 26, 2015 4:56 pm

Can you please tell me how to perform a dummy byte read?


Simply call the RS232 receive byte macro twice with a timeout of 0 to ensure the hardware buffer is cleared.

When you say "UART receive interrupt" you mean the UART RXINT0?


Yes that's the one. There is an example here: http://www.matrixtsl.com/wiki/images/6/ ... uffer.fcfx
These users thanked the author Benj for the post:
achillis1 (Wed Aug 26, 2015 7:23 pm)
Rating: 5.56%
 

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: RS232 refresh

Postby achillis1 » Wed Aug 26, 2015 9:31 pm

Hello Ben,

I have tried the example you had the link to. It seems that I got it to work once but after I modified it then I got an error!
Afterwards I download it again to check if the cause was my modification but even still I get the error at compiling.

Can you take a look at the message attached?

Thank you,

Best Regards
Attachments
CommsBuffer.msg.txt
(6.38 KiB) Downloaded 612 times

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Thu Aug 27, 2015 10:59 am

Hello,

Error: No remaining RAM block (on target) big enough for:
'FCV_01211_CircularBuffer1__DATA' size:128 bytes


The circular buffer is not fitting in RAM on your device. Go to the properties of the circular buffer and reduce the amount of memory until the compilation completes correctly.
These users thanked the author Benj for the post:
achillis1 (Thu Aug 27, 2015 3:44 pm)
Rating: 5.56%
 

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: RS232 refresh

Postby achillis1 » Thu Sep 17, 2015 12:52 pm

Thanks Ben! That worked.

However, can the circular buffer example work as is on flowcode V4?

Thank you,

Best Regards

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Thu Sep 17, 2015 12:55 pm

Hello,

The circular buffer component was introduced in Flowcode 6. However yes you should be able to create circular buffers in Flowcode v4 and v5 using variables and macros. I think there should be a topic on the forums on how to do it. If not then let me know and I'll try to dig out an example.
These users thanked the author Benj for the post:
achillis1 (Sun Sep 20, 2015 7:02 pm)
Rating: 5.56%
 

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: RS232 refresh

Postby achillis1 » Mon Feb 20, 2017 5:53 pm

Hello,

1. As mentioned above the RS232 has 2 byte buffer.
Q: How is possible to have a macro to receive RS232 string(max 2 Bytes = 14 characters ) > return value then is assigned to a string of 20 characters long / data sent from peer device are equal to 20 characters
SO data stored to the RetValue = 20 characters long! ...> with only one macro "receiveString" the MCU can accept 20 bytes?

2. Because of the 20 byte long return value, the string cannot show on the 16 space long LCD screen so I have to use the scroll function. To use the scroll function I will have to use a loop that scrolls 4 times -for the extra 4 characters/I do not want to use scroll at once 4 bytes/ - But I do not want to enter the loop if no data are received on the RS232. SO how do I set the IF statement icon to go to the loop branch when it receives data at RS232? Data are received and stored to a string value and not a byte in order just to say on the IF icon " if x = 1 go to "

Thank you

Best Regards
Andreas Achilleos

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Tue Feb 21, 2017 10:47 am

Hello Andreas,

Q: How is possible to have a macro to receive RS232 string(max 2 Bytes = 14 characters ) > return value then is assigned to a string of 20 characters long / data sent from peer device are equal to 20 characters


The receive string macro sits and waits for incoming data, As soon as a byte appears on the UART incoming buffer we collect it and store it in a separate RAM buffer. We continue to do this until we receive the specified number of bytes or we encounter a time out event.

So how do I set the IF statement icon to go to the loop branch when it receives data at RS232?


You should be able to use the Length$() function to work out if there are valid bytes in your string variable.

Here is an example code snippet.

str = UART::StringReceive()
len = Length$(str)
if (len > 0)
Yes: We have data
These users thanked the author Benj for the post:
achillis1 (Tue Feb 21, 2017 4:57 pm)
Rating: 5.56%
 

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: RS232 refresh

Postby achillis1 » Tue Feb 21, 2017 5:11 pm

Thanks Ben!

Here is an example code snippet.

str = UART::StringReceive()
len = Length$(str)
if (len > 0)
Yes: We have data
So will I use this in the "C code icon" ?
Can I also use it in the calculation icon , yes?

Regarding the RS232 buffer, you are mentioning above a HARDWARE buffer that stores 2 byte data, then you say that the receive string macro stores the data to a separate RAM buffer. Can you say that (when using the receive string macro )when we have incoming data FIRST they go to the hardware buffer and then moved to the RAM buffer that can hold more data?

I am asking because I have some issues with the buffers (they do not empty - even if I followed previous instructions to perform a dummy read* -)
* a dummy read clears the 2 byte buffer( hardware buffer )
> what about the RAM buffer? Can I do there a dummy read?


Thanks!!

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Tue Feb 21, 2017 5:33 pm

Hi Achillis,

No I didn't post C code it was just some example psuedo code.

Component macro to the RS232/UART component to read a string into a string variable.

Code: Select all

str = UART::StringReceive()


Calculation icon using the Length$ function. Available from the strings function tab. Returns the number of valid data bytes in the string.

Code: Select all

len = Length$(str)


Decision icon to test if the len variable is greater than 0.

Code: Select all

if (len > 0)
Yes: We have data send the valid data bytes to the LCD


I am asking because I have some issues with the buffers (they do not empty - even if I followed previous instructions to perform a dummy read* -)
* a dummy read clears the 2 byte buffer( hardware buffer )
> what about the RAM buffer? Can I do there a dummy read?


This may be something to do with strings rather than the UART incoming buffer.

A string variable is essentially an array of bytes. When we initialise a string for example.

strvar = ""

then all we are really doing is writing 0 to the first byte in the array, the other bytes remain untouched.

For example we have a string containing data.
str = "Hi".

str[0] = 'H'
str[1] = 'i'
str[2] = 0

then after re-initialising the string we are left with this.
str = "".

str[0] = 0
str[1] = 'i'
str[2] = 0

The Length$() function simply searches through the array looking for the null byte (the byte with the value of 0) which signifies the end of the valid data.

If you are doing a StringReceive and then passing the data to a LCD byte by byte without first checking the length then you will likely be using previous valid data by skipping past the null byte.

Please note that an ASCII 0 or '0' is different to a decimal 0 or null as can be seen here.
http://www.asciitable.com/

I hope this helps.

Here is a v7 example program to show the string data on an LCD and perform auto scrolling.
StrDemo.fcfx
(8.76 KiB) Downloaded 285 times
These users thanked the author Benj for the post:
achillis1 (Tue Feb 21, 2017 7:19 pm)
Rating: 5.56%
 

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: RS232 refresh

Postby achillis1 » Tue Feb 21, 2017 8:02 pm

Hello Ben

Many thanks! It is helpful.. Though the last part on how data gets crumpled after a byte by byte write I will need to get a piece of paper and draw on it the whole sequence to get it right in my head!

> When you say the string gets reinitialized you mean the string I create as variables or the strings of the RAM buffer?
(if I want to delete all data on the string what should I do? )

>When I run the simulation on the PC it does not stop on the RS232 receive string macro when I have time out parameter byte value of 255, THOUGH when I run it on my hardware it stops and wait! Any ideas on that?

>When I run the simulation , if I do not enter data in the RS232 icon for the "incoming bytes or char" then what I get printed on the LCD is a character like "U" with dots over it , like a flipped colon ( as I see in the ascii table maybe the decimal value of 129 or 154) and when I add a watch for string , it shows a value for it which it looks like a full stop but not quite like that! I guess is the decimal value of 254! *I get all these when I have no incoming data. Does it say anything to you?

*edit
when I receive NO data on rs232 the string gets a value of 'null' (see picture) and when I do a length calculation it says my string has size of 1! And I when the loop comes around and I receive only 1 character then is added to the 'null' and the length size becomes 2!

p.s how do I change the length of a string?

Thanks!
Attachments
IMG_20170222_083623.jpg
(232.98 KiB) Downloaded 2572 times
IMG_20170222_083532.jpg
(217.8 KiB) Downloaded 2572 times
IMG_20170222_083504.jpg
(227.71 KiB) Downloaded 2572 times

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Wed Feb 22, 2017 10:44 am

Hello,

(if I want to delete all data on the string what should I do? )


In most cases this shouldn't be necessary but when it is you simply loop through the bytes in the string setting them to 0.

e.g. for a 20 byte string you would do something like this.

idx = 0
while (idx < 20)
{
str[idx] = 0
idx = idx + 1
}

When I run the simulation on the PC it does not stop on the RS232 receive string macro when I have time out parameter byte value of 255, THOUGH when I run it on my hardware it stops and wait! Any ideas on that?


Hmm I would have to look into this one, a timeout of 255 is essentially a wait forever and maybe we decided not to allow this on the PC end to avoid Windows thinking Flowcode has crashed. I'll double check this.

>When I run the simulation , if I do not enter data in the RS232 icon for the "incoming bytes or char" then what I get printed on the LCD is a character like "U" with dots over it , like a flipped colon ( as I see in the ascii table maybe the decimal value of 129 or 154) and when I add a watch for string , it shows a value for it which it looks like a full stop but not quite like that! I guess is the decimal value of 254! *I get all these when I have no incoming data. Does it say anything to you?


Hmm no I'm not aware of this one, could you share your program that is doing this.

when I receive NO data on rs232 the string gets a value of 'null' (see picture) and when I do a length calculation it says my string has size of 1! And I when the loop comes around and I receive only 1 character then is added to the 'null' and the length size becomes 2!


Yes it looks like there is a byte coming in which is strange, it's even there when you have valid data. I would probably have to see your program to know more but will try and replicate it here.

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Wed Feb 22, 2017 11:00 am

I can't seem to replicate the issue.

StrTest.fcfx
(7.99 KiB) Downloaded 290 times

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: RS232 refresh

Postby achillis1 » Sat Feb 25, 2017 10:09 pm

Hello Ben,

I cannot read the fcfx, I have flowcode 6.

Never the less , I scroll through the C code file generated after compiling my flowcode and I see the folloowing:
#define MX_UART_RTS_TRIS_1 trisc
#define MX_UART_BAUD_1 9600
#define MX_UART_DBITS_1 8
#define MX_UART_RETURN_1 0
#define MX_UART_ECHO_1 0
#define MX_UART_INT_1 0

Are the above correct for the following settings : baud rate = 9600, parity = none, stop bit = 1 , flowcontrol = none ? It seems I got 1 too many "0" !!!

And another question :

a. when rs232 sends string value will it send as well carriage return ( 0x0d ) or /r or ENTER at the end of it ?
b. can I manipulate what would be at the end of my string data?

Thank you

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Mon Feb 27, 2017 12:32 pm

Hello,

a. when rs232 sends string value will it send as well carriage return ( 0x0d ) or /r or ENTER at the end of it ?


Flowcode will send the bytes that are in your string, it will not add anything to the string for you.

b. can I manipulate what would be at the end of my string data?


You you can for example do this using a newline escape sequence.

String = "Hello World\n"

See here for a full list of escape sequences you can use in C.
https://en.wikipedia.org/wiki/Escape_se ... _sequences
These users thanked the author Benj for the post:
achillis1 (Tue Feb 28, 2017 2:33 pm)
Rating: 5.56%
 

User avatar
achillis1
Posts: 347
Joined: Thu Oct 09, 2008 9:19 am
Has thanked: 92 times
Been thanked: 8 times
Contact:

Re: RS232 refresh

Postby achillis1 » Tue Feb 28, 2017 3:14 pm

Hello,

Many thanks Ben!

You you can for example do this using a newline escape sequence.

String = "Hello World\n"


When I do that I receive error when I compile to chip which says if I have C code in the program to review it! But OK I will bother with that another time!

*** Concerning the above, the first posts where I mention that when I include RS232 READ STRING with timeout 255, BYTE count 20 >for a string consisting of 20 characters and I get scrambled data, I have figured out what is happening!
So to whom it may concern :) :

If I send data to PIC but less than the string's length then the program takes into measurement (as space?) the number of times it took in order for the received data to fill in the whole string! Now to explain more:

Eg. FOR string of length 12 > received data = "123456" then user sends some more data "789012"
then rs232 takes the 14 bytes and assign them like this in the array: "123456.78901"
then number 2 is left out but only to be assigned in the next array :
received data "helloWorld123" then the array will be as this : "2helloWorld12"
TO FIX this I will have to then send 13 characters instead of 14 and later on I can send 14.
*I think Ben you mentioned something like that previously but I did not quite understood it! ( but that guided my thoughts to in order to figure it out else how! ) :)

I do not know if there is a fix or that is how it is suppose to be!

P.S Is there online a guide with all the explanation examples regarding string manipulation?

Best Regards
Andreas Achilleos

User avatar
Benj
Matrix Staff
Posts: 14142
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4329 times
Been thanked: 4075 times
Contact:

Re: RS232 refresh

Postby Benj » Tue Feb 28, 2017 3:29 pm

Hello Andreas,

I would have it so that the variable is always at least 1 byte bigger then the amount of data you want to receive and this way there is always room for the null byte at the end of the data.

The size of the string variable can be edited by varying the number in the square brackets when editing the variable.

e.g. strvar[20]

There is a wiki page on the string manipulation functions here which might help.
http://www.matrixtsl.com/wikiv7/index.p ... _Functions