UDP string rx

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

UDP string rx

Post by Ferla »

Hello to all
First of all, I wanted to wish you a Happy New Year.

For second, ask for your opinion.
I was trying to use the EB023 modules or the comp. ENC28J60 to receive a string via UDP protocol.
My example rx string is so "015010112113114" where I should divide it into blocks to make sure that these light up a 60 led strip mod. WS2812 es:
015 = number of LED to be turned on ,turn on only led number 15
010 = switch on led block n.10 then turn on LEDs from 15 to 24
112 = color R (0-255)
113 = color G (0-255)
114 = color B (0-255)

what is my problem, I can not divide the following string into blocks. Now I'm trying with Rx Uart 232 to do some tests and I do not understand if I have to reread the contents of the buffer or do a manipulation of the received string.

I trust in you
Thank you
Ferla

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

Re: UDP string rx

Post by Ferla »

Guys maybe I found the right way
viewtopic.php?t=6650

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Note that there is a mistake in the detail:
'2' = 50
'3' = 51
\n = 0

The string variable contains a reference to the location of the first character 'H'.
The \n character is a control code (NULL) with the numeric value 0. This is used as the terminating character of a string and its location is managed by Flowcode when the string is manipulated by any of the String functions.
\n actually = 10 (as would be expected from C where \n\r for new line (or line feed) & carriage return are often found at the end of strings.

\0 actually gives a 0 character (needed for end of strings)

Try single stepping the following (watching main.x)
character.fcfx
(6.95 KiB) Downloaded 303 times

Martin

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

Re: UDP string rx

Post by Ferla »

Hi Martin
Excuse me if I did not answer you before, I was busy with the job.
Thank you for your answer, I managed to complete what I wanted. Now I'm waiting for the hardware.
We hope not to fight too much with the piloting of the LEDs ws2812. I read on the forum about the IP exchange etc. on EB023-2 you have to manage it via EEprom after powering up the hardware. viewtopic.php?f=2&t=18968&p=82119&hilit=eb023#p82119
You have feedback or some examples, maybe our friend Leoo can help me.
Thanks Good day to you and to the friends of the forum.

Hello,
I'll tell you how my project is going.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Hi,

One idea is to pass the values in hex so that each led value is represented by a two digit hex number.
If you use decimal then you'll either need to pad each value to 3 characters (with leading 0s) or introduce a separator character such as a comma..

Oops, I see you've passed each as 3 digits. Sorry just thinking aloud.

Martin

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

Re: UDP string rx

Post by Ferla »

Hi Martin
I went ahead with the development and in the simulation it worked well, once I received the hardware I noticed that when I had to make a decision this was not taken into consideration.
I have done 1000 tests and several manipulations of strig, I have examined many examples but I do not understand why when I have to decide on the character "A" = ASCII 65 does not work. What am I doing wrong? It must be a banality but I entered a mental loop. :x
I am attaching the various steps.
Thank you
Ferla
Attachments
Decision Manipulation String not Found.doc
(49.5 KiB) Downloaded 298 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Hi Ferla,

Looking at the code snippet:

You have str = A001. Assuming you mean str = "A001" (ComandoRicevuto)

You can just use cmd = str[0] rather than using mid$?
chardemo.fcfx
(9.93 KiB) Downloaded 304 times
Demonstrates a very simple loop through string contents with a decision on the character - I've output the details to a com port here. You could assign the command to a byte variable ( c = t here ) and then use (if) c = 'A' at the decision point.

If you can post your code (or message me with it if you perfer) and I'll take a look.

Martin

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

Re: UDP string rx

Post by Ferla »

Hi Martin
Perhaps in my tests I had done what you say.
I did not use the one in the example.
I have simplified my project so I can take a look.
It's very simple. The part that receives the strings works perfectly. Use a strip of 300 LEDs.
Where the "A" character I use to give the ignition command,
"000" would be the single led lighting along the stripLED,
and over "000" I call it the Led group because it illuminates a group defined starting from the single lit LED.

In the attached project you find the specifications.
Thanks
Ferla
Attachments
Prj1_Sing_Group_Led_40P16.fcfx
(30.58 KiB) Downloaded 291 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Ok,

I think the problem arises because of

Code: Select all

.CarattereComando = StringToInt$ (.Str_Risp_CarComando)
.Tot_SingLed = StringToInt$ (.Str_Risp_SigLed)
.Tot_GroupLed = StringToInt$ (.Str_Risp_GrupLed)
(in macro ContrallaComando)

StringToInt$(str) converts (or attempts to convert) a string to an integer...

So x = StringToInt$("123") -> x = 123

StringToInt$("A") = ??? Certainly not 65 ! (10 if you are very lucky - most likely junk)

Code: Select all

.CarattereComando = ComandoRicevuto[0]; 
Should get the correct value here.

A very simple example that reads a (stream of) commands ('A') and then a 3 digit (unsigned) integer from a circular buffer:
No error checking and I just load the buffer using PutByte() :-)
BufferDemo.fcfx
(12.23 KiB) Downloaded 300 times
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

A useful addition to the circular buffer component would be a Peek() macro that returns the current byte without advancing the current index.

Alternatively a ReplaceByte() function that decrements the current index and 'replaces' the last byte returned by GetByte().

This makes some functions (such as reading a string of commands with variable length integer arguments so in Ferla's example we could have A1A2A30A4 rather than A001A002A030 etc)

This technique is commonly used in compilers - reading the next character is used to 'spot' the end of a symbol, however, the next character might be required as part (or all) of the next symbol to be parsed.

The code for Peek would be very similar to GetByte - just no need to advance DATASTART...

Martin

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: UDP string rx

Post by Benj »

Hi Martin,

I've added the suggested PeekByte function for you. Let me know how you get on.
CircularBuffer.fcpx
(66.9 KiB) Downloaded 372 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Thanks Ben,

A very small demo - changed above to read variable length integers...
BufferDemo.fcfx
(13.54 KiB) Downloaded 240 times
Martin

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

Re: UDP string rx

Post by Ferla »

Thank you Guys
I thank everyone for working for me.
Let's see a little .... simplify for convenience the following my striga "<A1>". So following your reasoning I find outgoing the character 1 as a decimal 49.
The WS2812B component accepts to turn on the led n.1 the command in Hex then 0x01.
Now I am facing the conversion from Dec to Hex so it is correct to follow this path, indicated by the legendary Martin :wink: http://www.matrixtsl.com/article.php?a=416
Ferla

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Hi Ferla,

I'm not sure what you need to convert here. The decimal or hex string is just a representation of the underlying binary that you will send to the LEDs
Hex is neat for programming because each 4 bits can be represented by one character.
Decimal doesn't have quite such an easy conversion but again it is just an easily readable representation of the underlying data..

Because of the ASCII character set used to convert from a decimal digit (0..9) you need to subtract '0' from the digit. Hex also uses A..F where A represents 10 in decimal or 1010 in binary. Because the letters do not follow the digits directly you need to check for them and subtract 55. I would usually use c - 'A' + 10 for clarity as the compiler will sort this into one constant anyway...
You might also want to handle lower case letters. If user input is involved always expect the unexpected...

Bear in mind that the UART1::SendNumber in the above examples converts the binary number back to a human-readable (decimal) form.

Martin
Last edited by mnf on Thu Jan 18, 2018 10:20 pm, edited 1 time in total.

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: UDP string rx

Post by Benj »

Not sure if required but this deals with converting numeric values to ASCII strings in different formats (dec, hex, bin) and back again.

https://www.youtube.com/watch?v=gavpYt- ... s&index=17

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

A simple program that displays numbers (0 to 255 or the equivalent of 1 byte as here) in decimal, binary and hexadecimal.
DisplayBase.fcfx
(14.8 KiB) Downloaded 220 times
Martin

Ben - this loops around - rather than looping once (0..255) and then stopping it repeats. Any ideas ? Not sure where I'm writing past the end of a string???

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

Re: UDP string rx

Post by Ferla »

Hello guys
I followed you behind the lines and I tried some solutions.
Thanks to your help I managed to get a part of what I wanted. The project attached in the laboratory works great is simple but has required some control steps eg. check on read buffer, I did not understand why launching the command es. "<A001>" turned on the led n.1 and after a few seconds, even by clears the buffer, a group of about 40 white LEDs was turned on. It is as if after reading the striga it returns to the buffer to complete the whole reading.

Another clarification, what I wrote above about the command led lighting that must be in Hex is not true.I had misled for an example found on the forum, just enter the simple variable (byte or Int) associated with our string number.

Now I have to implement the color management, but I tried with the same method but I think the string should be handled differently, type array?

Greetings
Ferla
Attachments
WS2812B_40P16_Rev2_OK.fcfx
(34.32 KiB) Downloaded 197 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Hi Ferla,

Could I get you to clarify a bit on what you are trying to achieve:

1) Some sample command strings and expected result.

2) Actual results.

3) I assume that you are trying to send a command from the PC to light/clear LEDs in the light strip - but are you trying to control single or multiple LEDs with a single command?

4) Can you get the light strip to work AOK without the command side of things - perhaps a single lit dot moving from end to end.... The LED strips are fairly exacting in their timing requirements so it might be something messing this up (perhaps an interrupt to read the serial port??)

Martin

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

Re: UDP string rx

Post by Ferla »

Hi Martin
Your question is right.
Unlike the animations that occur with the led strips, I must point out the position of an object on a conveyor belt.
The signaling of this must occur with the lighting of a single LED on the led strip. It is possible that I have to report an object of larger size so I have to be able to turn on a group of LEDs always on the led strip. All this via UDP.
Initially I started using a Uart string between PC and ECIO40P16 in order to start the project, as the example WS2812B_40P16_Rev2_OK.fcfx
Perhaps with this explanation you will understand the purpose of my project.
Now I would like to switch from Uart to UDP, or the possibility of using both ENC28j60 and Wiznet WI810MJ. Do you have any examples I can see by sending my string via UDP to my project ?
Thanks
Ferla

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Sorry - I haven't managed to get my Ethernet board working under Flowcode (a Wiznet Wiz5100) - I'd hoped that the Wiz810Mj components (that use the Wiz5100 chip) would work - but no joy.. (Anyone any sample code here?)
I can get UDP working under the Arduino toolset - using the EthernetUDP library..
If you can get UDP working then it should be possible to use the command string received in the same manner as one received from the UART.

Martin

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: UDP string rx

Post by Benj »

Ben - this loops around - rather than looping once (0..255) and then stopping it repeats. Any ideas ? Not sure where I'm writing past the end of a string???
Hi Martin,

If you look at the C code it makes a bit more sense.

The count variable is created as a byte.

Code: Select all

MX_GLOBAL MX_UINT8 FCV_NUM;
And then the for loop is created like this.

Code: Select all

for (FCV_NUM=0; (FCV_NUM)<(256); (FCV_NUM)++)
As Num is 0-255 it is always less than 256, If Num was a 16-bit variable then I believe it would work as expected.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Thanks - all clear now!

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: UDP string rx

Post by mnf »

Now got the Ethernet port working AOK...

Soldered in the 'int' connection to allow interrupts from the shield and also changed the SPI connection to software rather than hardware (which seems to have supplied the magic needed to get things working) I've queried SPI hardware mode on AVR before - is this a problem?

How do you get the SD card to work as a source for the html files? There doesn't seem to be any way to set the CS pin (or indeed much else) when using this option.

Martin

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

Re: UDP string rx

Post by Ferla »

Hi
I understood with a little patience what I wanted, now I have a problem when I connect the EB023 module.
I can not find the right connections in FC or better when I lock the pins in FC and program ECIO40P16 the UART port (RG6> Tx, RG7> Rx) does not work anymore.
In the project I need to communicate with both UART and EB023.

Can you help me? Thank you
Ferla

Ferla
Flowcode v5 User
Posts: 126
Joined: Sun Jan 06, 2013 8:58 pm
Location: Italy
Been thanked: 19 times
Contact:

Re: UDP string rx

Post by Ferla »

I realized that when I create the UDP socket the Pic stops working or created a simple flashing program in Led 1 second in loop and it works until the socket component macro is created, a point the micro does not work, why? Benj and Martin can you tell me a solution? Thank you

Post Reply