|
-
Mar 4th, 2002, 02:23 PM
#1
Thread Starter
Addicted Member
Strings, integers, and hex oh my!
I have an imbedded controller (Rabbit 2000) that is programmed in a modified form of C, but I think much of the "basics" are the same. I have to send commands to a device in hex. The following...
Code:
char *run;
run = "\x55\x03\x10";
serBputs(run); //sends data out the serial port
...starts my device. But I want to be able to vary the last value. I've tried several things, creating another string without the last value, setting an integer to say "100", converting that to a string and combining them. This works on a fundamental level, but the "100" doesn't get converted to hex, it thinks it's a string.
So what I want is:
Code:
char *run;
int i; // i can vary from 1-255
run = "\x55\x03\x(i)" // this is not legal
How can I accomplish this?
-
Mar 4th, 2002, 03:01 PM
#2
Fanatic Member
"\x55\x03\x10" is a string of 3 chars (and the 0-char at position 3 of course). All chars are hexadecimal.
If you need to modify the last char (in this case the "\x10"), you can change it like this: run[2] = (whatever char you like).
However, the "100" in your example won't work (if hexadecimal), since it's just too large. The range is \x00 to \xFF (which is equal to 0 - 255). But if you mean the decimal 100, you can use it. It's represented as \x64.
-
Mar 4th, 2002, 04:33 PM
#3
Thread Starter
Addicted Member
I haven't tried it yet but it sounds good. One thing though, the "\x" converts the decimal to hex... So are you saying that the "Run[2] = " statement won't convert it?? I need an integer to be what I replace with... In other words, I need to be able to use the full 0-255 range in that last position. I'll play around with it now, maybe I'll be enlightened!
-
Mar 4th, 2002, 05:02 PM
#4
Thread Starter
Addicted Member
No go, this is my code:
Code:
char try[5];
main() {
try[0] = "\x55";
try[1] = "\x03";
try[2] = "\x10";
}
It won't even assign the array. The error I've been seeing alot of is: "Type Mismatch: assigning a value of type 'char' to type 'char[]'"
I've also tried:
Code:
char *try;
char try[];
try = "\x55\x03\x10";
And it won't compile. I also tried putting the char assignment in the "main" and it doesn't like that either.
-
Mar 4th, 2002, 08:25 PM
#5
Use single quotes instead of double...
Code:
char szTry[5] = "";
szTry[0] = '\x55';
szTry[1] = '\x05';
szTry[2] = '\x10';
-
Mar 5th, 2002, 07:50 AM
#6
And to have a varying value:
Code:
char szTry[5] = {'\x55', '\x03', '\x10', 0, 0};
int i = 23; // some value
szTry[4] = (char)i;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 5th, 2002, 09:20 AM
#7
Thread Starter
Addicted Member
It's almost working. But when I try to set one of the characters using szTry[2] = something It gives me "assignment to read-only variable not allowed".
Thanks for all the help thus far!!
-
Mar 5th, 2002, 12:45 PM
#8
what's your exact declaration of szTry?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 5th, 2002, 01:06 PM
#9
Thread Starter
Addicted Member
Well, I just tried this:
Code:
main() {
char szTry[5] = {'\x55', '\x03', '\x10', 0, 0};
int i = 23; // some value
szTry[4] = (char)i;
printf("MSG: \"%s\"\n\n", szTry);
}
Before I had alot of other code in my program, but I trimmed it down. I also tried putting the declaration before main. I also am getting a warning that may be specific to my microprocessor and may be part of that problem?? It says: "Initialized variables are placed in flash as constants. Use keyword 'const' to remove this warning." But it's only a warning, it errors on the assignment to read-only...
-
Mar 6th, 2002, 10:04 AM
#10
That's a problem of flash. (The modified version of C)
Instead of this line:
char szTry[5] = {'\x55', '\x03', '\x10', 0, 0};
do:
Code:
char szTry[5];
szTry = {'\x55', '\x03', '\x10', 0, 0};
This should work (but I'm not sure)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 7th, 2002, 08:32 AM
#11
Thread Starter
Addicted Member
Bad news, the compiler really doesn't like" szTry = {'\x55', '\x03', '\x10', 0, 0}; It gives a slew of errors:
"Invalid expression - need left hand value"
"Missing ";" - there are several of these
"\x10 out of scope/not declared"
It only displays ten errors... I tried changing several things, the "," to ";", tried using just normal characters instead, nothing got me any further.
Any other ideas?
-
Mar 7th, 2002, 12:11 PM
#12
szTry[0] = '\x55';
szTry[1] = '\x03';
...
It seems this thing you have has a really weird behaviour...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 7th, 2002, 12:29 PM
#13
Thread Starter
Addicted Member
Thanks for your persistence CornedBee. Your last suggestion is very close to working, it conpiles and runs, but I get too many characters. I may be able to fix that, I just don't have time right now. So this is what I have:
Code:
main() {
char szTry[3];
int i;
i = 10;
szTry[0] = '\x55';
szTry[1] = '\x03';
szTry[2] = '\x10';
printf("MSG: \"%s\"\n\n", szTry);
szTry[2] = (char)i;
printf("MSG: \"%s\"\n\n", szTry);
}
I think all your suggestions didn't work b/c of the modified C version I have. Are you familiar with the Rabbit processor and Dynamic C? Here's their website: www.zworld.com/products/dc/index.html
They have a support forum too, I posted this question there this morning, but I came here first b/c their forum is slow and the biggest reason, smart ppl hang out here! 
Thanks!
-
Mar 7th, 2002, 12:53 PM
#14
Thread Starter
Addicted Member
Just an update. The extra characters are ignored by my device, so IT WORKS! I think I was close before with amac's idea, but I didn't heed riis' comment about hex values. When I was trying to use 100, that was too big for hex.
Who knows when you're debugging, you try so many things that you forget what you did and didn't do!
Thanks again all!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|