11-17-2009 10:46 AM
I need a lil assistance , try to store a char array into a char array, then convert to a double. Im trying to maintain the current position.
but I cant get pass this error. Also g_sRcvData is a byte a body { margin: 0 0 0 0; padding:0 0 0 0 } td,div { font-family:Tahoma;font-size:8pt;vertical-align:top } body { margin: 0 0 0 0; padding:0 0 0 0 } .transcript { background-color:#d2d2d2; } .messageBlock { margin-left:4px; margin-bottom:3px } .message { margin-left:100px; word-wrap:break-word; white-space:-moz-pre-wrap; _white-space:pre; } .messageCont { margin-left:100px; word-wrap:break-word; white-space:-moz-pre-wrap; _white-space:pre;} .other { color:#39577a;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .myself { color:#da8103;font-style:normal;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont { font-size:8px;text-align:right; color:#39577a;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .myselfCont { font-size:8px;text-align:right; color:#da8103;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .system { margin-left:4px; word-wrap:break-word;color:#da8103;font-style:normal;font-weight:normal; white-space:-moz-pre-wrap; _white-space:pre; } .showTimestamp { margin-right:3px; float:right; color:#999999;font-style:normal;font-weight:normal; } .other1 { color:#ac2000;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont1 { font-size:8px;text-align:right; color:#ac2000;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other2 { color:#3c9fa8;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont2 { font-size:8px;text-align:right; color:#3c9fa8;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other3 { color:#e25614;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont3 { font-size:8px;text-align:right; color:#e25614;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other4 { color:#0b6ac8;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont4 { font-size:8px;text-align:right; color:#0b6ac8;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other5 { color:#b23290;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont5 { font-size:8px;text-align:right; color:#b23290;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other6 { color:#02e7c7;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont6 { font-size:8px;text-align:right; color:#02e7c7;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .other7 { color:#5b3284;vertical-align:top;font-weight:bold;font-style:normal;float:left; width:95px; } .otherCont7 { font-size:8px;text-align:right; color:#5b3284;font-family:Arial,Lucida Grande;font-style:normal;vertical-align:top;font-weight:bold;float:left; width:95px; } .tsDisplay { display:block }
char sAZXRelativeMove[100]={0};
char sELYRelativeMove[100]={0};
char sCrtCmd[256]={0};
char sCurrentPositionX[100]={0};
char sCurrentPositionY[100]={0};
char g_sRcvData[100]={0};
int i=0;
double dNewPosX=0;
double dNewPosY=0;
strcpy(sCrtCmd,"SERVO, READ, 09901483C, 088000000");
Send_SERVO_Command(sCrtCmd);
sCurrentPositionX[i]=g_sRcvData[i];
atof(sCurrentPositionX);
dNewPosX=sCurrentPositionX+dAZXRelativeMove;/////error 7131, 48 Operands of + have illegal types 'pointer to char' and 'pointer to double'.
strcpy(sCrtCmd,"SERVO, READ, 099014840, 088000000");
Send_SERVO_Command(sCrtCmd);
sCurrentPositionY[i]=g_sRcvData[i];
atof(sCurrentPositionY);
dNewPosY=sCurrentPositionX+dAZXRelativeMove;////error 7131, 48 Operands of + have illegal types 'pointer to char' and 'pointer to double'.
Fmt(sAZXRelativeMove,"%f",*dNewPosX);
sprintf(sCrtCmd,"SERVO, WRITE, 098000034, %s",sAZXRelativeMove);
Send_SERVO_Command(sCrtCmd);
Fmt(sELYRelativeMove,"%f",*dNewPosY);
sprintf(sCrtCmd,"SERVO, WRITE, 098000038, %s",sELYRelativeMove);
Send_SERVO_Command(sCrtCmd);
11-17-2009 12:13 PM - edited 11-17-2009 12:16 PM
Am I the only one having difficulty to read your post? Seems like a lot of html code found it's way into your post..
From what I read, your problem is located here:
dNewPosX=sCurrentPositionX+dAZXRelativeMove;/////error 7131, 48
Operands of + have illegal types 'pointer to char' and 'pointer to
double'.
dNewPosY=sCurrentPositionX+dAZXRelativeMove;////error 7131, 48
Operands of + have illegal types 'pointer to char' and 'pointer to
double'.
Can you explain what you are trying to do? Can you explain what is the value for sCurrentPositionX and why it is a string? Are you trying to cast is as a double and then add dAZXRelativeMove? Or do you need to cast it as a pointer to a double?
Was sCurrentPositionXread from a file or obtained from a serial port and you would like to cast it to a double?
If so, you could use atof. Here are some details..
double atof ( const char * str );
Convert string to double
Parses the C string str interpreting its content as a floating point number and returns its value as a double.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals, and interprets them as a numerical value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this function.
A valid floating point number for atof is formed by a succession of:
* An optional plus or minus sign
* A sequence of digits, optionally containing a decimal-point character
* An optional exponent part, which itself consists on an 'e' or 'E' character followed by an optional sign and a sequence of digits.
You may have to change your declaration, unless you do want to work with pointers.
11-17-2009 02:04 PM
Darnell:
I have a couple of comments in addition to Ray's.
1.) atof()
Ray mentions atof(). You are calling it in the code you posted, but you're ignoring the results.
As Ray posted, the prototype for atof is as follows.
double atof ( const char * str );
The format of the prototype shows that atof takes as string input (const char * str) and returns a double output. The original string is unchanged and is still a string. In order to use the double return value, you need to assign the atof() return to it.
For example:
double myDouble;
char myString[] = "3.14159"
myDouble = atof(myString);
2.) array subscript i
Where is your loop? You initialize
int i=0;
but then you don't have a loop where you increment i.
You set
sCurrentPositionX[i]=g_sRcvData[i];
but then you take
atof(sCurrentPositionX);
not sCurrentPositionX[i]
3.) double as pointer
It looks like dNewPosX is a real position, but then in your Fmt statement, you treat it as a pointer.
Fmt(sAZXRelativeMove,"%f",*dNewPosX);
That syntax says that *dNewPosX is a float, so dNewPosX is a pointer to a float. Is it? If not, delete the * in *dNewPosX.
4.) Fmt vs. sprintf
Fmt (a CVI library function) and sprintf (an ANSI C library function) both provide formatted conversion. Why use them both? I have a personal preference for sticking to the ANSI C functions as much as possible to make my code more portable.
You could combine your two statements into one.
Instead of
Fmt(sAZXRelativeMove,"%f",*dNewPosX);
sprintf(sCrtCmd,"SERVO, WRITE, 098000034, %s",sAZXRelativeMove);
use
sprintf(sCrtCmd,"SERVO, WRITE, 098000034, %f",dNewPosX);
5.) READ command response
You send a READ command using Send_SERVO_Command(). What do you need to do to read the response?
6.) HTML code in your post
Ray mentions seeing the HTML code in your post (for example body { margin: 0 0 0 0; padding:0 0 0 0 }). I see it too, and have seen it in several of your previous posts. Do you see it too? It makes your post pretty hard to read. If you see that junk right after you post a message, click on the Options menu in the bar right above your message, and select Edit. Edit is only available for a short period of time after you post, but it gives you another chance to clean up your posts.
11-17-2009 02:21 PM
Thanks Al,
You hit it 100%. I didn't want to make any assumptions in my reply, so I was asking questions rather than making statements.. 😉
If I review what you said: 🙂
Al S wrote:1.) atof()
Ray mentions atof(). You are calling it in the code you posted, but you're ignoring the results.
As Ray posted, the prototype for atof is as follows.
double atof ( const char * str );
The format of the prototype shows that atof takes as string input (const char * str) and returns a double output. The original string is unchanged and is still a string. In order to use the double return value, you need to assign the atof() return to it.
For example:
double myDouble;
char myString[] = "3.14159"
myDouble = atof(myString);
Exactly. 🙂 Plus I like the example. Using atof() does not convert the variable within the function to a double. It returns a double for the string that was passed to the function. So if it is not used properly, it just used some CPU for nothing 😉
Al S wrote:2.) array subscript i
Where is your loop? You initialize
int i=0;
but then you don't have a loop where you increment i.
You set
sCurrentPositionX[i]=g_sRcvData[i];
but then you take
atof(sCurrentPositionX);
not sCurrentPositionX[i]
I saw that.. 🙂 I wanted to understand what was the intension of the code. You hit it 100%.
Al S wrote:3.) double as pointer
It looks like dNewPosX is a real position, but then in your Fmt statement, you treat it as a pointer.
Fmt(sAZXRelativeMove,"%f",*dNewPosX);
That syntax says that *dNewPosX is a float, so dNewPosX is a pointer to a float. Is it? If not, delete the * in *dNewPosX.
You read my mind on this one.. 😉
Al S wrote:4.) Fmt vs. sprintf
Fmt (a CVI library function) and sprintf (an ANSI C library function) both provide formatted conversion. Why use them both? I have a personal preference for sticking to the ANSI C functions as much as possible to make my code more portable.
I didn't pay much attention to this one. However, I do agree with you. Since I go back & forth between CVI & VxWorks, I prefer sticking with sprintf simply because it is second nature. Convenience more than anything else. Still a very good comment. Thanks.
Al S wrote:
6.) HTML code in your postRay mentions seeing the HTML code in your post (for example body { margin: 0 0 0 0; padding:0 0 0 0 }). I see it too, and have seen it in several of your previous posts. Do you see it too? It makes your post pretty hard to read. If you see that junk right after you post a message, click on the Options menu in the bar right above your message, and select Edit. Edit is only available for a short period of time after you post, but it gives you another chance to clean up your posts.
Glad you saw that too. I thought it was part of the problem description and was initially trying to make sense out of it. I don't know what would be causing that unless there is a switch from the regular editor mode to html and it somehow leaks into the preview editor mode. I have not seen this behavior anywhere else before, so I doubt the forum editor is the cause.
Wish I could give you more than 1 kudos, Al. Excellent post.
Thanks,
R
11-17-2009 06:20 PM
I found even found out a better way. I chose to do a memcpy Instead
int Relative_Move_From_Current_Position(double *dAZXRelativeMove,
double *dAZYRelativeMove)
{
char sAZXRelativeMove[100]={0};
char sELYRelativeMove[100]={0};
char sCrtCmd[256]={0};
char sCurrentPositionX[100]={0};
char sCurrentPositionY[100]={0};
int readByte=0;
double dNewPosX=0;
double dNewPosY=0;
double X=0;
strcpy(sCrtCmd,"SERVO, READ, 09901483C, 088000000");
Send_SERVO_Command(sCrtCmd);
memcpy(sCurrentPositionX,g_sRcvData,100);
dNewPosX=atof(sCurrentPositionX);
dNewPosX=dNewPosX+(*dAZXRelativeMove);
strcpy(sCrtCmd,"SERVO, READ, 099014840, 088000000");
Send_SERVO_Command(sCrtCmd);
memcpy(sCurrentPositionY,g_sRcvData,100);
atof(sCurrentPositionY);
dNewPosY=sCurrentPositionY[i]+(*dAZXRelativeMove);
Fmt(sAZXRelativeMove,"%f",dNewPosX);
sprintf(sCrtCmd,"SERVO, WRITE, 098000034, %s",sAZXRelativeMove);
Send_SERVO_Command(sCrtCmd);
Fmt(sELYRelativeMove,"%f",dNewPosY);
sprintf(sCrtCmd,"SERVO, WRITE, 098000038, %s",sELYRelativeMove);
Send_SERVO_Command(sCrtCmd);
return VI_SUCCESS;
}
11-17-2009 06:30 PM
The send_servo_command function, basically Im to keep track of where I am when I make a certain move.(x,y)
So first I did a read, and in the send_servo_command function it does alot of formatting double to hex,hex to float, blah, blah
using the pcnode communication. etc......................
so there a global g_sRcvData variable that returns everything which is a string array. So I did a memcpy from the global string to a new string array .
I got the string array and perform this dNewPosX=atof(sCurrentPositionX);
Then I make a relative move dNewPosX=dNewPosX+(*dAZXRelativeMove);
11-17-2009 06:49 PM
I'm not sure how using memcpy improved your software. The following memcpy
memcpy(sCurrentPositionX,g_sRcvData,100);
fills the first 100 bytes of sCurrentPositionX with g_sRcvData.
What do you mean by:
darnell wrote:so there a global g_sRcvData variable that returns everything which is a string array.
darnell wrote:I got the string array and perform this dNewPosX=atof(sCurrentPositionX);
Then I make a relative move dNewPosX=dNewPosX+(*dAZXRelativeMove);
Yes, this is what Al proposed 🙂