problem sending code via rs232

M

Martin Petzold

Hi,
i always receive an "arrow up" instead an "A" when i do this (snipped!):

BYTE Byte;

Byte = 0x41;
PortWrite((BYTE)(&Byte));

BOOL PortWrite (BYTE Byte)
{
WriteFile (hPort, &Byte, sizeof (&Byte), NULL, NULL);
return TRUE;
}

what is the problem?

thx martin
 
J

Joona I Palaste

Martin Petzold said:
Hi,
i always receive an "arrow up" instead an "A" when i do this (snipped!):
BYTE Byte;
Byte = 0x41;
PortWrite((BYTE)(&Byte));
BOOL PortWrite (BYTE Byte)
{
WriteFile (hPort, &Byte, sizeof (&Byte), NULL, NULL);
return TRUE;
}
what is the problem?

Your question concerns a non-standard extension to C and is thus
off-topic on comp.lang.c. Please ask in a newsgroup dedicated to your
own OS.
 
C

Chris Torek

Presumably "BYTE" is a typedef-name or #define for some integral
type (int, char, unsigned long, whatever).

If so, why is there a cast here?

Presumably BOOL is another typedef-name or #define for some integral
type, again. If PortWrite needs a "BYTE" (int, unsigned char, whatever),
passing the address of an object ("BYTE Byte = 0x41" then "&Byte")
is probably not right. Another clue that there is something wrong
is that the call contains a cast, to change the value produced by
&Byte into a new value of whatever type the name BYTE stands for.

Impossible to say for certain, but if you have a BYTE object and
PortWrite() demands a BYTE value, it is more likely to work right
if you pass the object's value, rather than its address. :)

Your question concerns a non-standard extension to C and is thus
off-topic on comp.lang.c. Please ask in a newsgroup dedicated to your
own OS.

This may also be (part of) the problem, but -- as shown above --
there is a Standard C issue that can be addressed here as well.

As usual, code with casts is suspicious at best. Always take a
second look at casts -- there is a significant chance that, if some
piece of code requires a cast to compile without a warning, that
piece of code is wrong.
 
R

Régis Troadec

Martin Petzold said:

Hi,

Your post is off-topic here as Joona said, but...
i always receive an "arrow up" instead an "A" when i do this (snipped!):

BYTE Byte;

<win32>
BYTE is a 8 bit unsigned integer.
Byte = 0x41;

OK for 'A' in ASCII.
PortWrite((BYTE)(&Byte));

PortWrite(&Byte);

Perhaps you meant passing the adress of Byte to PortWrite, but it's not OK,
since you cast &Byte to a BYTE value (the representation of the adress of
Byte (of a BYTE element) may be different, e.g. the adress of a BYTE could
be a 32 bit value). I guess you did it probably to fit the types (I see
below that PortWrite takes an argument of type BYTE).
BOOL PortWrite (BYTE Byte)
{
WriteFile (hPort, &Byte, sizeof (&Byte), NULL, NULL);
return TRUE;
}

<win32>

The third argument of WriteFile is the number of bytes (of the buffer whose
the adress is passed as second argument) to write in hPort. It's the same
kind of matter as above : sizeof(&Byte) returns the number of bytes which
are needed to represent the adress of a BYTE element in memory, not the
number of bytes to represent a BYTE element itself. Change in sizeof(BYTE)
or sizeof (*Byte).

Finally, WriteFile returns a non-zero value (of type BOOL) if it has
succeeded. You should use it.

BOOL PortWrite(BYTE * Byte)
{
return WriteFile(hPort, Byte, sizeof(BYTE), NULL, NULL);
}

</win32>

HTH
Regis
 
C

CBFalconer

Martin said:
i always receive an "arrow up" instead an "A" when i do this
(snipped!):

BYTE Byte;

Byte = 0x41;
PortWrite((BYTE)(&Byte));

BOOL PortWrite (BYTE Byte)
{
WriteFile (hPort, &Byte, sizeof (&Byte), NULL, NULL);
return TRUE;
}

what is the problem?

As far as I can see the only defined things in your code snippet
are "0x41" and "return". If you had bothered to #include
<stdio.h> "NULL" would also be included. Did your cat walk over
your keyboard? How long have you lurked here? Did you read the
welcome message?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top