03-31-2008 08:41 AM
03-31-2008 10:09 AM
To explain: how is your message being created? Only in case you have a string that actually reads "0103040F000F" you will need to read it two characters at a time and convert from
computer science course, lesson 1: A VARIABLE STORES A NUMBER, NOT THE REPRESENTATION OF A NUMBER !
e.g.:
int a = 8;
'a' contains the value which is commonly represented by humans as 8 in decimal. but it is also 1000 in binary or 10 in octal of 8 in hexadecimal. thus, all those tests are true without performing any 'conversion' on the variable:
a == 8 // decimal
a == 0x8 // hexadecimal
a == 010 // octal
(food for thought: decimal is a weird representation used by humans only because they have 10 fingers. imagine if we had 4 fingers on one hand, and 3 on the other)
03-31-2008 11:39 AM
03-31-2008 02:28 PM
You're absolutely right, Olivier: coming from an actual string you must scan 2 characters at a time and retrieve their value. Your solution is correct, but may I suggest a more compact way of solving your problem?
#include <formatio.h>
#include <ansi_c.h>
int i, ch;
char src[20], dst[20];
strcpy (src, "0103040F102030");
for (i = 0; i < strlen (src) / 2; i++) {
Scan (src, "%s[i*w2]>%i[r16]", i * 2, &ch);
sprintf (dst[i], "%c", ch);
}
ComWrt (port, dst, strlen (src) / 2);
A disadvantage of this solution is that it uses CVI Formatting and I/O library function Scan, so if portability to non-CVI compilers is one of your issues you can forgive it.
Remember that you cannot use strlen on destination string "dst" since it may contain non ascii characters and it will return incorrect results.
04-01-2008 08:57 AM
04-01-2008 09:40 AM
08-24-2010 03:59 AM
hi,
in the above thread u have mentioned
08-24-2010 04:33 AM
ComWrt(port, buffer, count) will not stop when it sees a 0x00 character in buffer - it will only stop when count bytes have been sent, whatever they are. But please bear in mind that most C functions (and the debugger) will probably assume the end of a string when they see a 0x00 and this can give misleading results when trying to diagnose problems. Motto: don't use string based functions to work with binary data. ComWrt() works with binary data.
JR
08-24-2010 04:50 AM
JR is absolutely right, as you can prove by checking ComWrt return value: it will be equal to the number of bytes transmitted, including any embedded nul byte, if any.