Received UART characters to strings

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

Post Reply
christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Received UART characters to strings

Post by christoph »

Hi Folks,

I am using the Matrix Multimedia HP488-00-3 Development board with a 16F877A ( just for info. as it nothing to do with query).

My query is just concerning a):-

a) I am receiving a continuous and repeating UART stream of 3 characters (e.g. '1', '2', '3') and want to make them into strings (i.e. STR6, STR7, STR8 to then call them STR5). b) I then compare them with a STR4 (made from STR1, STR2, STR3 which are taken from memory addresses '0', '1', and '2') - with which I have no problem. I know I COULD put them into memory but I dont want them left there or have to add more steps to the flowchart to remove them.

Can someone PLEASE tell me how to do this as I have spent hours getting nowhere!!!

Many thanks in advance,

Chris

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

Re: Received UART characters to strings

Post by Benj »

Hello Chris,

Simply create 2 string variables with 3 bytes or more in length.

string1[10]
string2[10]

Then receive your RS232 characters and populate the string as follows

idx = 0
bVar = RS232 Receive Byte (100)
while bVar < 255
{
string[idx] = bVar
idx = idx + 1
bVar = RS232 Receive Byte (100)
}
string[idx] = 0

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Many thanks Ben - I will follow that up shortly.

Best regards,

Chris

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Hi Ben, or anyone who could help-

Couldn't get anywhere with the suggestion above as I couldn't produce a string. Sorry Ben but I discovered that seperate characters were no good so if I put in '235' it read as 3 individual characters

To clarify my project: I am presenting 5 continually repeating bytes (between 1 and 254) to the RX pin of a PIC16f877A. I want to put the first cycle of 5 bytes into retained memory then 'compare' the very next cycle of 5 bytes with the memory as a check, producing a red or green led light as to matched or not. Ultimately, I aim to finish up with 12 groups of 5 bytes in memory - each group effectively is a different code. All that happens then is that when a particular code is received, it lights the appropriate green "CODE GOOD" LED.

THE PART I AM REALLY STUCK ON is that I can't turn the incoming, second cycle of 5 bytes into a decimal string, to compare them with the first cycle of strings which went into memory.

I feel that the first cycle is a poor way of doing it anyway (puting each character as a string 1, string2 , string3 etc.into address '0', '1', '2', etc. then then saying strings 1 to 5 = string 6, strings 7 to 11 = string 12 etc.) so if anyone has a neater way of doing that, I would be grateful)

Is any of the above a pointer as to why I have run out of ram when I have only 4 (non-fully working cycles of my description above and I actually need 12!!!!.......?).

Hope I can get a bit of help on this,

Chris

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

Re: Received UART characters to strings

Post by Benj »

Hi Chris,

Data is generally type casted in certain ways.

eg.

dvar = 235 puts the decimal value of 253 into variable var
avar = '2' puts the ascii value of 2 into variable var which is 50 in decimal.

When creating a string you need to build up the string using ascii characters.

eg

string[0] = '1'
string[1] = '2'
string[2] = '3'

results in string = "123"

You could then use the StringToInt function in the string manipulation icon to convert back to a decimal value rather then a array of characters.

var = StringToInt ( string )

Hope this helps.

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Thanks Ben,

Will get to work on that right away

Cheers,

Chris :D

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Hi all,

What uses the RAM? So much so that what is a small programme of mine compared to many I look at, I have run out of RAM on the PIC16F877A in only 1/6th of my probable, eventual programme! I haven't included detail cos it just a question of WHAT gobbles it up???

Chris

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

Re: Received UART characters to strings

Post by Benj »

Hi Chris,

Variables are your main source of RAM consumption although the stack and other things also use a little bit.

Do you have any large arrays in your program? Otherwise post up your program and I will have a look at whats using up the resources.

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Thanks Ben - I will do a bit more work first as I haven't managed to fully incorporate your penultimate bit of info. and have several 'possibilities' for the project, so would rather post the one I seriously want to run with if I have a problem with it and/or RAM, but enlightening me on RAM will help!

Cheers,

Chris

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Hi Ben or anyone - can you tell me the way to send Switch No.1 or Switch No.2 or Switch No.3 etc. (up to 16) is 'closed', as an input into RS232. The link and receipt is working fine if I just manually give a character or string to the RS232 input to send. Just cannot work out how to get a switch status and its No. in instead.

Regards,

Chris

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

Re: Received UART characters to strings

Post by Benj »

Hello,

A single RS232 transaction is normally a byte representing an ascii character, but it doesn't have to be.

Each byte has 8 bits which we can tap into to store say 8 digital switch values.

Setting a bit in a byte.

byte = byte | (1 << 0) - Sets bit 0
byte = byte | (1 << 1) - Sets bit 1
byte = byte | (1 << 4) - Sets bit 4
byte = byte | (1 << 7) - Sets bit 7

Clearing a bit in a byte

byte = byte & ~(1 << 0) - Clears bit 0
byte = byte & ~(1 << 1) - Clears bit 1
byte = byte & ~(1 << 4) - Clears bit 4
byte = byte & ~(1 << 7) - Clears bit 7

So now you can set and clear bits in a byte you can read your various switches and update the data in the byte accordingly.

Then transmit the byte via the RS232 serial connection and you can then decode the various bits at the receiver end.

Reading a single bit state

bit = byte & (1 << 0) - Reads bit 0
bit = byte & (1 << 1) - Reads bit 1
bit = byte & (1 << 4) - Reads bit 4
bit = byte & (1 << 7) - Reads bit 7

Repeating this for a second byte will allow for up to the 16 switch values you require to be sent and received.

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Hi Ben,
I understand what you are saying re byte and bits in principle but don't know how to implement it on the flowchart. I have tried to use the input icon but I don't think that is the way you mean is it?

How do I get those ASCII codes into where you have shown them?

Could you possibly show me a flowchart example for what is needed for one bit transmit and one bit receive? I am OK with all the rest of the 'basics' needed for the flowchart .

Would be really grateful.

Many thanks,

Chris

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

Re: Received UART characters to strings

Post by Benj »

Hi Chris,

Here are two examples using an input icon in single pin mode and a switch component.
ss1.JPG
ss1.JPG (13.26 KiB) Viewed 11067 times
ss2.JPG
ss2.JPG (14.71 KiB) Viewed 11067 times
And then recombining at the receiver
ss3.JPG
ss3.JPG (12.14 KiB) Viewed 11066 times

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Hi Ben,

.....then to the RS232 'Component Macro' ?? (as a character or a string??)

Sorry, but now you have shown me for single bit, I'm not sure how it would be for a byte and 2 bytes for my 16 signals?

How do I get the ASCII characters to put in?

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

Re: Received UART characters to strings

Post by Benj »

Hello Christoph,

You would do 8 of the transmit functions (as shown above) to the same out variable using 0 through to 7 for the shift positions (1 << x). This would fill up a byte with 8 bits.

Once you have done this you pass the out byte to the RS232 send byte function.

To receive the byte you would wait for a valid incoming byte to come in (I would recommend non legacy mode for the RS232 component, see component help file for details). Once a byte is received you would use 8 of the receive functions (as shown above) to convert the incoming byte into 8 logic bits. Again using the same received byte for all 8 receive functions and using 0 through 7 for the shift position.

To get 16 signals you repeat the transmit and receive sequences twice. You could synchronise the data by sending 3 bytes of information. The first being a known value and the other two being your data bytes.

eg
send(0x55)
send(data1)
send(data2)

Your receiver then looks for the first byte and when the value matches it knows the next byte it receives will be data1 followed by data2.

ASCII is just a method of assigning graphical characters to numbers. i.e. you dont have to worry about it, decimal, hex, binary, ASCII its all the same thing, a binary number.

'0' = 48 = 0x30 = 0b00110000

Because you don't need to see the data in ASCII format you are simply passing byte values.

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Yep - I get it all now - thanks very much Ben,


Best regards,

Chris :D

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

hi Ben,

Transmitter (just done one byte to try) works fine on flowchart but wont 'compile. I get the following message:-

ailure

........
Return code = 1

Flowcode was unable to compile the flowchart's C code due to the following errors:


If your flowchart contains C code, please review this carefully. If your flowchart contains no C-code or you have thoroughly reviewed the code, contact Technical Support.

FINISHED


Havent had any compiling problems at all before and wondering if it is to do with my ASCII queries which were prompted by the 'PIPE' character ( | ) and ( & which doesn't look like an and! ) and ( ~ ), all of which I didn't realise were on my keyboard!! I had already compiled and programmed PIC 877A successfully (though not getting the reult I was looking for) a number of times and the only differences are the two 'calculation' boxes in the transmitter example (and the '&' for the receiver) containing those characters. Info from web indicates that '|' could mean 'or' and '&' could mean 'and' but I don't know what '~' would be! Can you let me know if it is this and how I modify the calc boxes - or if it is something else.

Regards,

Chris
Last edited by christoph on Sat Jan 21, 2012 2:08 pm, edited 1 time in total.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Received UART characters to strings

Post by medelec35 »

Hi Chris,

~ means Bytewise NOT so its used with Numbers
! means Bitwise NOT so it can only be used with 1 bit

Normally when compiling and you get an error, then error with line number causing the error is displayed e.g

Code: Select all

7segx3 count pulse 16F877a V1 test.c(1028): error: unexpected identifier
7segx3 count pulse 16F877a V1 test.c failure

failure

........
Return code = 1
Have you left the error out because there was no number displayed within brackets?

If that is the case it could be that the place the Flowcode is writing to is no longer accessible.
E.g root of flash drive which is full or too many files.
Folder with no access rights.

Or

Missing semicolon from the supplementary code box.

If you cant find the source of problem, then it is always best to post Flowchart with .C code and .msg.txt. files which are generated in the same directory when you compile to hex or to chip

If you don't want to because its commercially sensitive or you don't want any ideas copied, then its best to PM a person of your choice thike a member of staff from matrix.

I'm afraid

Code: Select all

ailure

........
Return code = 1

Flowcode was unable to compile the flowchart's C code due to the following errors:


If your flowchart contains C code, please review this carefully. If your flowchart contains no C-code or you have thoroughly reviewed the code, contact Technical Support.

FINISHED

Will not help to find source of the problem.

There was an issue with Flowcode 4.3 and 64bit windows 7.
For Return code = 1 and no errors displayed, take a look at:
http://www.matrixmultimedia.com/mmforum ... =30&t=8034
But that was with Flowcode V4.3 and was sorted in V4.5

Martin
Last edited by medelec35 on Sat Jan 21, 2012 4:47 pm, edited 1 time in total.
Martin

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Hi Martin,

I'm OK with discreet electronics but not with microprocessors or ANY kind of programming - hence Flowcode but I am only a beginner with it at the moment so I very much appreciate your posting with regard to this problem. I understand some of what you are saying but didn't get ALL of: "If you cant find the source of problem, then it is always best to post Flowchart with C code and 2. Simple Echo Flowchart_Name.msg.txt.".

Just a point: The 'pipe' character was definitely a '|' and not a '!' .

See attachment for flowchart referred to.
Attachments
TEST UART TX .fcf
(12 KiB) Downloaded 219 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Received UART characters to strings

Post by medelec35 »

christoph wrote: I understand some of what you are saying but didn't get ALL of: "If you cant find the source of problem, then it is always best to post Flowchart with C code and 2. Simple Echo Flowchart_Name.msg.txt.".

Just a point: The 'pipe' character was definitely a '|' and not a '!' .

See attachment for flowchart referred to.
I'm very sorry :oops:
I copied and pasted from one of my other posts, but I copied the wrong bit, and had to rush out on an urgent matter, so did not have time to check and correct mistakes this time.
I have corrected the mistake. it should of said:
If you cant find the source of problem, then it is always best to post Flowchart with .C code and .msg.txt. files which are generated in the same directory when you compile to hex or to chip
So its not surprising you did not understand. If I would have read that bit of the post from someone else I would have not understood it either.

Re ! character.
reason I mentioned that is because both ! and ~ means NOT and I wanted to show what the difference is.

As for your flowchart, it compiled with no problems what so ever.
So the issue is you do not have the latest version which should be V4.5 or there is an issue stopping flowchart from saving the necessary files.
have you tried creating a new directory and trying to compile form the new directory?

What is your Flowcode type and version e.g professional V4.5 etc?

I have attached the generated hex file for you to try on your hardware.

Martin


I have attached the hex file
Attachments
TEST UART TX .hex
(4.3 KiB) Downloaded 190 times
Martin

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Hi Martin,

Yes I have version 4.5 professional and this is the very first hiccup I have had but as you will see below, don't think it anything to do with Flowcode directly.

No worries re me not understanding that bit of information, I am such a novice I didn't realise it had a bit 'extra' attached to it anyway - just happy you were very kindly helping me out. Thanks for your reply - just recently got back to the ubiquitous keyboard and have been checking out what you have posted and it looks like you have hit the nail on the head regarding putting flowchart into another directory! Usually, as soon as I have something working, I do actually do that but hadn't with this one yet!

I tried your attachment and it compiles and programmes the PIC with no problem. So I tried MY attachment which again wouldn't compile and neither did my original. So I thought I would follow your advice and try my 'other' directory - oops realising I hadn't specifically put it in one........ EXCEPT that I HAD (temporarily) put it into a file for 'attachments' in case it didn't e-mail properly or whatever. As it transpires that also compiles and loads up the PIC with no problem at all! However, I did notice that MY file was 12 KiB and the one you posted showed a mere 4.5KiB. I assume that this is significant! I DO have a huge amount of Flowcode stuff in that same folder, though all the ones I have just checked seem to compile and load OK. I will use a new folder to progess this now!

Thanks also for clarifying the symbol characters. Are these ASCII? I have a print out which includes 'extended ascii' but it doesn't give the information regarding what |, !, ¦, ~ etc. mean. Is it to do with 'C' code?

I am going to have a file-sort and do the receiver end loaded with extra confidence thanks to your help - very much appreciated.....

Best regards,

Chris :D

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Received UART characters to strings

Post by medelec35 »

No probs so long as your getting further that's the main thing.
christoph wrote: Thanks also for clarifying the symbol characters. Are these ASCII? I have a print out which includes 'extended ascii' but it doesn't give the information regarding what |, !, ¦, ~ etc. mean. Is it to do with 'C' code?
These are C operations or functions

E.g.

Code: Select all

+	Addition
-	Substraction
/	Division
*	Multiplication
%	Modulus



>	Greater then
>=	Greater then or equal to
==	Equal to
!=	Not equal to
<=	Less then or equal to
<	Less then



&&	AND
||	OR
!	NOT

&	bitwise AND
^	 bitwise exclusive OR (XOR)
|	 bitwise inclusive OR
~	bitwise complement (NOT)
<<           Bitwise left shift
>>           Bitwise right shift
Martin
Martin

christoph
Flowcode V4 User
Posts: 382
Joined: Tue Nov 01, 2011 4:28 pm
Has thanked: 196 times
Been thanked: 26 times
Contact:

Re: Received UART characters to strings

Post by christoph »

Thanks Martin, - I kinda thought they might be.

One final question. When running that flowchart only an asterisk appears for each byte. Is that how it should be? I only ask, because eventually I want to use a smaller, cheaper chip as I wont be needing the LCD and at flowchart building stage it would still be nice to 'see' that the switches were sending the correct information.

Many thanks,

Chris

Post Reply