How should it be the best algorithm for data sending

General discussion on mikroC PRO for ARM.
Post Reply
Author
Message
doganmustafa
Posts: 11
Joined: 19 Feb 2020 14:44

How should it be the best algorithm for data sending

#1 Post by doganmustafa » 05 Mar 2020 16:59

Hi everyone. Im working on data sending betwen two devices(Stm-Stm, Stm-Pc, Pc-Stm etc) and
For example

Code: Select all

unsigned int my_value=65535  
char sending_buffer[5];
 
void separate_value(int value)
{	  
        int i=0;  
	do
	{
		sending_buffer[i]=value%10;
	       value=value/10;
                i++;
	}
	while(value >0);
}

void main()
{
   separate_value(my_value);      // so the sending_buffer[5]=5,3,5,5,6
} 

an other alternative with same example;

Code: Select all

unsigned int my_value=65535  
char sending_buffer[2];
 
void separate_value(int value)
{	
       int eight_bit=0;
       eight_bit=value/256;         // result is 255 for 65535
       sending_buffer[0]=eight_bit;     //
       sending_buffer[1]=(eight_bit*256)-value;   // result is 255 for 65535
}

void main()
{
   separate_value(my_value);      // so the sending_buffer[2]=255,255
} 

different algorithms can be produce .But i want to know which algorithms are commonly used for efficiently fast data sending. What are your opinions about my algorithms , and can you please share algorithms you use . I am waiting for any kind of advice. : )))
Last edited by doganmustafa on 05 Mar 2020 17:33, edited 2 times in total.

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: How should it be the best algorithm for data sending

#2 Post by hexreader » 05 Mar 2020 17:13

Code: Select all

void separate_value(int _value)
Why the underscore _value? Underscore usually suggests "system" variable, not user variable.

Code: Select all

   while(_my_value >0);
presumably the first underscore is a typo?
my_value never changes from 65535, so the loop will never exit, and the function will not return (unless reset or other external means)

The function that sends 2 bytes will usually be much faster than the 5 byte version, since communication speed is USUALLY much slower than processor speed.

The 5 byte version might be preferable if you add 0x30 to each byte to turn the 5 bytes into ASCII. ASCII is sometimes easier to debug than raw binary, but slower and less efficient. ASCII can be easily seen on any PC terminal program. Probably best to add a 6th byte (such as maybe 'cr' character or space) so that you can tell where one number ends and the next one starts.

As for what is most common.... depends on transmission method... USB? Serial? I2C? SPI? Parallel? Other?
Start every day with a smile...... (get it over with) :)

doganmustafa
Posts: 11
Joined: 19 Feb 2020 14:44

Re: How should it be the best algorithm for data sending

#3 Post by doganmustafa » 05 Mar 2020 17:32

Hi thank you for your answer. _ overscope was my fault. I fixed it and edit new code lines. And olsa while(_my_value>0), i fixed it. It was missed : )) Please check the new codes .
I m looking the best way for USB and Serial Rs-232 communication for now.

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: How should it be the best algorithm for data sending

#4 Post by hexreader » 05 Mar 2020 17:49

For USB or serial...

2 byte version is fastest.

5 byte (or if I did it, with +0x30 to convert to ASCII and 6th space character byte) is easier for a newbie to work with.

Either is acceptable. Start with 6 byte version, then maybe change to 2 byte version once you gain confidence.

I will probably help when you get stuck (no promises) if you zip up and post full project folder :D
Start every day with a smile...... (get it over with) :)

doganmustafa
Posts: 11
Joined: 19 Feb 2020 14:44

Re: How should it be the best algorithm for data sending

#5 Post by doganmustafa » 05 Mar 2020 18:12

Thanks mate : )) . I will do this interesting stuffs depend on your advices : )) . For now, i dont have a project, i am just working on to improve my substructure 8)

AntiMember
Posts: 137
Joined: 02 Jan 2020 19:00

Re: How should it be the best algorithm for data sending

#6 Post by AntiMember » 05 Mar 2020 22:01

Maybe I don’t understand what ...
But 65535 = 0xFFFF. Send 0xFF, 0xFF the fastest.
void separate_value (int value)
{
sending_buffer [0] = (value >>8) & 0xFF;
sending_buffer [1] = value & 0xFF;
}

doganmustafa
Posts: 11
Joined: 19 Feb 2020 14:44

Re: How should it be the best algorithm for data sending

#7 Post by doganmustafa » 08 Mar 2020 01:06

Hi antimember. Thanks for advice. So can we say that sending in hex form is the best and fastest way?

And actually "65535" was just an example. it could be change, it could be any number .

AntiMember
Posts: 137
Joined: 02 Jan 2020 19:00

Re: How should it be the best algorithm for data sending

#8 Post by AntiMember » 08 Mar 2020 08:00

Hi doganmustafa. So you can send any number.
What I wrote is an analog of your algorithm. Only in logical operations.

doganmustafa
Posts: 11
Joined: 19 Feb 2020 14:44

Re: How should it be the best algorithm for data sending

#9 Post by doganmustafa » 08 Mar 2020 13:28

Hi thank you antimember

Post Reply

Return to “mikroC PRO for ARM General”