|
|
View previous topic :: View next topic |
Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 19215
|
|
Posted: Fri Dec 29, 2017 9:52 am |
|
|
OK. We move slightly forwards.
I will repeat my question though as to the real resolution of the data?.
Just because the number is stored in an int16, doesn't mean you really 'have' 16bits to send. At the simplest (for example), if you are reading the ADC, 'into' an int16, there are only 10 bits of data actually there.
However:
Code: |
int16 throttle=1000;
char dummy[32]; //somewhere to put the output
sprintf(dummy,"T%04LW\n");
|
Would generate a string like "T03E8\n"
03E8 is the hex for 1000.
If the number was smaller, perhaps only int10 we could send just 3 digits.
This is lovely and easy to read at the other end. Search here, for examples of this. |
|
|
Zek_De
Joined: 13 Aug 2016 Posts: 100
|
|
Posted: Sat Dec 30, 2017 7:01 am |
|
|
Friends
I did this algorithm but sometimes I got unexpected results. How can I get better this ?
Code: |
void TR_Receive(char *rData,char *buffer,int8_t dim,char c1, char c2)
{
int8_t i=0,j=0,k=0,u=0,l=0,n,DIM;
n = dim-1;
DIM = n+1;
while(rData[i]!=c1)
{
i++;
}
while(rData[j]!=c2)
{
j++;
}
while(i<DIM)
{
buffer[l] = rData[i];
i++;
l++;
}
while(j>-1)
{
buffer[n] = rData[j];
n--;
j--;
}
} |
|
|
|
Zek_De
Joined: 13 Aug 2016 Posts: 100
|
|
Posted: Sat Dec 30, 2017 7:10 am |
|
|
What I want to do. Sending values are like this.
Code: |
sprintf(buf,"%d",throttle); //throttle 0-65536
buf1[0] = 'T';
buf1[1] = buf[0];
buf1[2] = buf[1];
buf1[3] = buf[2];
buf1[4] = buf[3];
buf1[5] = buf[4];
buf1[6] = '\n';
|
Receive side
Code: |
TR_Receive(pData,buf,sizeof(pData)/sizeof(pData[0]),'T','\n');
data[0] = buf[1];
data[1] = buf[2];
data[2] = buf[3];
data[3] = buf[4];
data[4] = buf[5];
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9097 Location: Greensville,Ontario
|
|
Posted: Sat Dec 30, 2017 7:39 am |
|
|
You NEED to show us COMPLETE code, not just a few lines...
It is impossible to say exactly why it fails, we NEED to know what PIC you're using, RS232 configuration/setup, power supplies, etc.
Have you got 'ERRORS' in the use RS232 (...options...), how big is your receive buffer? What does the ISR code consist of ?
What you've shown is very,very complicated for such a simple task but without seeing ALL the program none of us can help
As a starting point, use Mr. T's program as the 'transmitter', say looping every second. Now code your 'receiver' program to properly receive and decode his data.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19215
|
|
Posted: Sat Dec 30, 2017 9:54 am |
|
|
Code: |
sprintf(buf,"%d",throttle); //throttle 0-65536
buf1[0] = 'T';
buf1[1] = buf[0];
buf1[2] = buf[1];
buf1[3] = buf[2];
buf1[4] = buf[3];
buf1[5] = buf[4];
buf1[6] = '\n';
|
Will result in buf1, having values without the /n, unless 'throttle' is over 9999.
Your numbers vary in width. A number like '1' has just one digit then a null terminator.
Are you using a PIC24/30/33?. If not '%d' will only handle an int8.... |
|
|
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
|
Posted: Sat Dec 30, 2017 3:16 pm |
|
|
Just a suggestion:
Please try searching google for "Byte Stuffing" and "Serial data Framing" |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19215
|
|
Posted: Sun Dec 31, 2017 4:08 am |
|
|
Think about a radio.
You want to send some numbers to a person at the other end.
You start saying numbers. 1,4,5,6,7,0,0,5,6,7,8,9.
How are they ever going to know which is which?.
So you change and instead say 'Throttle124'. Still a problem. Have you finished?. So instead you standardise, and add a terminator:
'Throttle124<over>'
'Speed34560<over>'
Suddenly you have a format that tells the person at the other end, 'what' you are sending, and he knows when you have finished (the 'over').
This is exactly what you have to do with any comms link.
Even better you can also make the numbers the same length (he can then be writing them in the right column as he receives them), and preferably add some checking '124 repeat 124 over' or some improved check.
It's worth realising that catalogue numbers for many stores that take orders over the phone usually have some form of checking built into the numbers, so if a digit is wrong, the person at the other end can know there is a problem. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9097 Location: Greensville,Ontario
|
|
Posted: Sun Dec 31, 2017 6:20 am |
|
|
I'd really like to help and love a 'mind challenge' but can anyone figure out his TR_receive function ?
...
n=dim-1;
dim=n+1;
...
if dim=4 then
n=4-1.....( 3)
dim=3+1..( 4)
or am I missing something from staring at this too long ?
From reading the entire 'thread' it seems the OP will have a PIC as Device #1 AND Device #2, therefore is in control of coding BOTH ends of the communications and all the hardware.
It would be nice to nice more facts of the project though. Code for each 'device' is only 25-30 lines long IF it's what I 'think' it's supposed to be...
Of course the longer this gets..the more I'm thinking is another,ugh, Proteus project.....
Jay |
|
|
Zek_De
Joined: 13 Aug 2016 Posts: 100
|
|
Posted: Tue Jan 02, 2018 2:16 pm |
|
|
Friends sorry to late response
I did this for now,very primitive,no accuracy control,but very enough for now,but I will get better later.Thanks to all information again.
void TR_Transmit_Multi(unsigned int16 *data,char *buffer,unsigned int8 bufferDim,unsigned int8 dataDim)
{
unsigned int8 i=0,j=0;
char temp[5]={0};
buffer[0] = 'T';
for(i=0;i<dataDim;i++)
{
sprintf(temp,"%d",data[i]);
for(j=1;j<=5;j++)
{
buffer[j+5*i] = temp[j-1];
temp[j-1]=0;
}
}
buffer[bufferDim-1] = '\n';
}
unsigned int8 TR_Receive_Multi(char *rData,char *buffer,unsigned int8 rDataDim)
{
unsigned int8 i=0,j=0,k=0,n,numberOfint16;
char data[rDataDim];
n = rDataDim-1;
numberOfint16 = (rDataDim-2)/5;
while(rData[i]!='T')
{
i++;
if(i>n)
return 0;
}
while(rData[j]!='\n')
{
j++;
if(j>n)
return 0;
}
while(i<rDataDim)
{
data[k] = rData[i];
i++;
k++;
}
while(j>-1)
{
data[n] = rData[j];
n--;
j--;
}
for(i=0;i<numberOfint16;i++)
{
for(j=0;j<5;j++)
{
buffer[j+5*i] = data[(j+1)+(5*i)];
}
}
return 1;
}
Last edited by Zek_De on Tue Jan 02, 2018 2:23 pm; edited 1 time in total |
|
|
Zek_De
Joined: 13 Aug 2016 Posts: 100
|
|
Posted: Tue Jan 02, 2018 2:22 pm |
|
|
Jay
n=dim-1;
dim=n+1
I realised it .This is carelessness .Sometimes I am laboring late hours
Also I am not using proteus simulation |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19215
|
|
Posted: Tue Jan 02, 2018 2:58 pm |
|
|
Code: |
void TR_Transmit_Multi(unsigned int16 *data,char *buffer,unsigned int8 bufferDim,unsigned int8 dataDim)
{
unsigned int8 i=0,j=0;
char temp[5]={0};
buffer[0] = 'T';
for(i=0;i<dataDim;i++)
{
sprintf(temp,"%d",data[i]);
for(j=1;j<=5;j++)
{
buffer[j+5*i] = temp[j-1];
temp[j-1]=0;
}
}
buffer[bufferDim-1] = '\n';
}
|
You do realise a single printf statement can do all the formatting for each line. 80% of this is wasted code. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9097 Location: Greensville,Ontario
|
|
Posted: Tue Jan 02, 2018 4:01 pm |
|
|
I can't follow the receive 'parser' routine as it has too many variables in it..
Since you know the datastream as you coded the transmit, it is easy to 'parse' or take apart the. There are several examples of received data programs both in the examples folder and the 'code library ' forum.
As I said before, take Mr. Ts SIMPLE 'transmit data' code, and 'loop it' every second. There the transmit program is 'up and running'.
NOW...
cut a recieve program to capture, parse and display it. Use ex_sisr.c as a base example andyou only need 10-15 lines of additional code to have a working receive program.
DONE.
Save both programs.
Now, copy and edit them. For thetransmitprogram that's simply adding a function to acquire a 'throttle' number(0 to 65535) and then transmit it.
Jay |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|