Uart Rx Byte to Variable
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Uart Rx Byte to Variable
Hello
I have a simple question and I kindly ask for help.
In a loop cycle, I receive a sequence of bytes via Uart. 192168, how do I make 192 a variable and 168 another variable?
Surely it is a trivial question but I can not find the optimal solution.
Ferla
I have a simple question and I kindly ask for help.
In a loop cycle, I receive a sequence of bytes via Uart. 192168, how do I make 192 a variable and 168 another variable?
Surely it is a trivial question but I can not find the optimal solution.
Ferla
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Ferla,
What exactly are you attempting here - are you receiving bytes (ie 192 then 168) or a string of characters = "192168"
Could you receive them into an array
values[0] = Uart1::ReceiveChar(timeout) (or x = ReceiveChar, y = ReceiveChar
...
values[n] = Uart1::ReceiveChar(timeout)
or use a circular buffer?
PutByte(Uart1::ReceiveChar(timeout))
If the data received is a string - then you would need to parse them into the variables - in this case each variable value would have to be a fixed length (say 3 bytes) or use a delimiter (eg ',')
Martin
PS - how goes the project with the LEDs?
What exactly are you attempting here - are you receiving bytes (ie 192 then 168) or a string of characters = "192168"
Could you receive them into an array
values[0] = Uart1::ReceiveChar(timeout) (or x = ReceiveChar, y = ReceiveChar
...
values[n] = Uart1::ReceiveChar(timeout)
or use a circular buffer?
PutByte(Uart1::ReceiveChar(timeout))
If the data received is a string - then you would need to parse them into the variables - in this case each variable value would have to be a fixed length (say 3 bytes) or use a delimiter (eg ',')
Martin
PS - how goes the project with the LEDs?
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Martin
Very simple.The EB023 module has the possibility of being able to replace the IP address after being initialized, so I wanted through string UART to associate 4 groups for 3 string characters in 4 variables. I receive the string and with a loop I read every single character as byte as long as the string is long.
Example:
StringRx = "192168123213"
VariablileIP0 = 192
VariabileIP1 = 168
VariabileIP2 = 123
VariabileIP3 = 213
I do not understand how to combine these bytes into one single variable.
Thanks!!
Very simple.The EB023 module has the possibility of being able to replace the IP address after being initialized, so I wanted through string UART to associate 4 groups for 3 string characters in 4 variables. I receive the string and with a loop I read every single character as byte as long as the string is long.
Example:
StringRx = "192168123213"
VariablileIP0 = 192
VariabileIP1 = 168
VariabileIP2 = 123
VariabileIP3 = 213
I do not understand how to combine these bytes into one single variable.
Thanks!!
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Ferla,
Not sure what you mean by that - but here is a simple program that splits the input string into 4 values (I would recommend using an array for these) then rebuilds them into an ip address string (including the '.'s)
Hope this helps....
Martin
Not sure what you mean by that - but here is a simple program that splits the input string into 4 values (I would recommend using an array for these) then rebuilds them into an ip address string (including the '.'s)
Hope this helps....
Martin
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Martin
your example is close to what I wanted, even if it needs changes and adaptations,
because the IP address could also be expressed in decimals like eg. 10.11.12.13 and not always in centesimals 192.168 etc, I know the calculation mul = 100 can not always be good.You will find many variables, do not worry, I did a lot of tests.
If you look at the attachment you will see.
You will also find "[" and "]" which I entered as a check and start and end reception
If you have other suggestions I'm here.
Greetings
Ferla
your example is close to what I wanted, even if it needs changes and adaptations,
because the IP address could also be expressed in decimals like eg. 10.11.12.13 and not always in centesimals 192.168 etc, I know the calculation mul = 100 can not always be good.You will find many variables, do not worry, I did a lot of tests.
If you look at the attachment you will see.
You will also find "[" and "]" which I entered as a check and start and end reception
If you have other suggestions I'm here.
Greetings
Ferla
- Attachments
-
- UDP_RX_TX_OK_16F1937_ipCange4.fcfx
- (33.47 KiB) Downloaded 219 times
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Ferla,
Using mul = 100 will give you leading 0s in the output ip address - 010.011....
If you don't like this it is easy to change:
Needs to be added to check in the conversion back to a string... Note that you need to check that mul is not 1 otherwise an value of 0 won't work....
This doesn't help with the input value - you would need a separator otherwise
19216 could be 19.216 or 192.16 - so passing 192016 removes the ambiguity..
Why not pass in the ip address as "192.168.1.254" ?
Martin
Using mul = 100 will give you leading 0s in the output ip address - 010.011....
If you don't like this it is easy to change:
Code: Select all
if (ip[i] >= mul) & (mul > 1)
This doesn't help with the input value - you would need a separator otherwise
19216 could be 19.216 or 192.16 - so passing 192016 removes the ambiguity..
Why not pass in the ip address as "192.168.1.254" ?
Martin
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Martin
your suggestion to use the separator have already been considered and I will make this change.
I can use all the bytes received to convert them to string., so I can use your example.
Thanks Ferla
your suggestion to use the separator have already been considered and I will make this change.
I can use all the bytes received to convert them to string., so I can use your example.
Thanks Ferla
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Martin rereading my last message maybe I expressed my request badly. I wanted to ask you how do I merge all the read bytes and turn them into a string?
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Ferla,
It's pretty easy to do:
in pseudocode:
Depending on the input technique - it might also be as straightforward to parse the input stream directly (for example from a circular buffer - if we know that it won't be updated whilst we are working) - might also be appropriate if memory is scarce.
Martin
It's pretty easy to do:
in pseudocode:
Code: Select all
data is string[20] // Using a string (it's an array of char!) of suitable size.
index = 0
loop
data[index] = ReadByte() // Get a byte from input source
index = index + 1 // Advance to next position in string
until data buffer full or input ended. // i.e. if Index = 19 (for a string size 20)- leave space for a string terminator
data[index] = 0 // Stick a 0 on end of string so string handling code works ok
Martin
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Martin,
after taking the run-up I stumbled .. I have problems with the separator. If I insert the following string "10.11.12.10" I should check the separator in order to have a correct control as for your advice and if you enter the digits centesimal, decimal or unit es. 10.11.1.1 see the simulation ip variable {10,56,11,56,12,56,10} . it is know correct to change from ip [4] to ip [7] considering the separators "." ?
I always use your example.
after taking the run-up I stumbled .. I have problems with the separator. If I insert the following string "10.11.12.10" I should check the separator in order to have a correct control as for your advice and if you enter the digits centesimal, decimal or unit es. 10.11.1.1 see the simulation ip variable {10,56,11,56,12,56,10} . it is know correct to change from ip [4] to ip [7] considering the separators "." ?
I always use your example.
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Ferla,
You need a slightly different approach if using a separator... I'll post you a sample this evening...
Martin
You need a slightly different approach if using a separator... I'll post you a sample this evening...
Martin
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Thank you much Martin,
you're always a great help ...people like you must be rewarded
you're always a great help ...people like you must be rewarded
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Okay - this parses an input ip address in the format a.b.c.d (ie 112.1.22.122)
- note that the ip address should be in the correct format (ie 4 values 0..255) and NO allowance is made for variations (for example spaces, extra '.'s, missing values - allowing for every possibility makes the code much more complicated !!! ) Note also that your input string must have a terminating nul \0 (actually any non digit character would do) - you did remember to add a \0 to then end of your input string ?
The first 'half' does the hard work - the second half just reflects what we calculated to the UART (to check it works!)
To save the typing. Hope it helps...
Martin
- note that the ip address should be in the correct format (ie 4 values 0..255) and NO allowance is made for variations (for example spaces, extra '.'s, missing values - allowing for every possibility makes the code much more complicated !!! ) Note also that your input string must have a terminating nul \0 (actually any non digit character would do) - you did remember to add a \0 to then end of your input string ?
The first 'half' does the hard work - the second half just reflects what we calculated to the UART (to check it works!)
To save the typing. Hope it helps...
Martin
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Martin
I thank you very much, thanks to your examples I managed to keep going.
Now I have to try everything on the hardware, I hope it works like a simulation.
Greetings see you soon.
Ferla
I thank you very much, thanks to your examples I managed to keep going.
Now I have to try everything on the hardware, I hope it works like a simulation.
Greetings see you soon.
Ferla
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Martin
I just can not find the way to pull out the 4 variables from the string.
The received bytes turn them into a string as you want, then I use your last ip.fcfx project and something happens to them that I can not understand. For test I use an LCD display that allows me to check what happens at the end and I realize that I can not turn the string into 4 variables because they are not shown on the display.
Thanks
Ferla
I just can not find the way to pull out the 4 variables from the string.
The received bytes turn them into a string as you want, then I use your last ip.fcfx project and something happens to them that I can not understand. For test I use an LCD display that allows me to check what happens at the end and I realize that I can not turn the string into 4 variables because they are not shown on the display.
Thanks
Ferla
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Thanks, Martin
the project is composed of the first part where I transform the received bytes into string array,afterwards I visualize if all the string has been correctly received (only for test purpose then inhibited), another step and try to manipulate the string by visualizing the blocks on the display (always for test then inhibited) ,then I get to the point of using your project by entering what has been received,I do not tell you how many proof I did because I lost my bill ... ,in truth and from April 27 (release of your project) I try and try again and again and again......
Before asking for help, I try to try and try it again.
Sometimes I have to admit that some gaps are holding me back
Ferla
the project is composed of the first part where I transform the received bytes into string array,afterwards I visualize if all the string has been correctly received (only for test purpose then inhibited), another step and try to manipulate the string by visualizing the blocks on the display (always for test then inhibited) ,then I get to the point of using your project by entering what has been received,I do not tell you how many proof I did because I lost my bill ... ,in truth and from April 27 (release of your project) I try and try again and again and again......
Before asking for help, I try to try and try it again.
Sometimes I have to admit that some gaps are holding me back
Ferla
- Attachments
-
- UDP_RX_TX_OK_16F1937_ipCange9.fcfx
- (46.66 KiB) Downloaded 124 times
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Ferla,
Took a quick look at your code and (my Italian is no where near as good as your English) think I've spotted a problem:
In Ass_IP Just before you attempt to decode StringIP - you set it to "" (empty string)
This will surely stop things working as you expect!
Another possible problem - is the ip string guaranteed to be all in one block read in (you decrement SIZE to 0 but don't check if a closing ']' has been received - and if it is (assuming the ip is all read) you need to add a terminator (\0) to StringIP.
Martin
Took a quick look at your code and (my Italian is no where near as good as your English) think I've spotted a problem:
In Ass_IP Just before you attempt to decode StringIP - you set it to "" (empty string)
This will surely stop things working as you expect!
Another possible problem - is the ip string guaranteed to be all in one block read in (you decrement SIZE to 0 but don't check if a closing ']' has been received - and if it is (assuming the ip is all read) you need to add a terminator (\0) to StringIP.
Martin
-
- Flowcode v5 User
- Posts: 126
- Joined: Sun Jan 06, 2013 8:58 pm
- Location: Italy
- Been thanked: 19 times
- Contact:
Re: Uart Rx Byte to Variable
Hi Martin,
I'm happy to hear from you.
When you talk about a terminator (\0), do I have to add it in the flowcode or when I send the string via UDP?
I turned it on to try your suggestion
I'm happy to hear from you.
When you talk about a terminator (\0), do I have to add it in the flowcode or when I send the string via UDP?
I turned it on to try your suggestion
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Adding the calculation macro
Should do the trick (but note my previous comment about all the ip address being within SIZE bytes)
Martin
Code: Select all
StringIP[index] = '\0'
Martin
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Another possible problem:
If SIZE is greater than the length of StringIP (20) - and after a '[' character is received there is no terminating ']' (due to a corrupted block or some other problem) then Ass_IP will overwrite memory causing a crash or subtle bugs?
Martin
If SIZE is greater than the length of StringIP (20) - and after a '[' character is received there is no terminating ']' (due to a corrupted block or some other problem) then Ass_IP will overwrite memory causing a crash or subtle bugs?
Martin
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Uart Rx Byte to Variable
Try adding some debug statements
So - for example - send StringIP to UART after receiving it.
What have you tried so far (did you implement the above/get rid of the StringIP ="") - any idea where is it going wrong?
Martin
So - for example - send StringIP to UART after receiving it.
What have you tried so far (did you implement the above/get rid of the StringIP ="") - any idea where is it going wrong?
Martin