problem sending code via rs232

Discussion in 'C Programming' started by Martin Petzold, Apr 12, 2004.

  1. 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
     
    Martin Petzold, Apr 12, 2004
    #1
    1. Advertising

  2. Martin Petzold <> scribbled the following:
    > 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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Insanity is to be shared."
    - Tailgunner
     
    Joona I Palaste, Apr 12, 2004
    #2
    1. Advertising

  3. Martin Petzold

    Chris Torek Guest

    >Martin Petzold <> scribbled the following:
    >> i always receive an "arrow up" instead an "A" when i do this (snipped!):
    >> BYTE Byte;
    >> Byte = 0x41;
    >> PortWrite((BYTE)(&Byte));


    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?

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


    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.

    >> what is the problem?


    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. :)

    In article <news:c5emt7$hp$>
    Joona I Palaste <> writes:
    >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.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Apr 12, 2004
    #3
  4. "Martin Petzold" <> a écrit dans le message de
    news:c5emor$l7f$...
    > Hi,


    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.
    </win32>

    >
    > 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


    > what is the problem?
    >
    > thx martin
     
    Régis Troadec, Apr 12, 2004
    #4
  5. Martin Petzold

    CBFalconer Guest

    Martin Petzold wrote:
    >
    > 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?

    --
    Some useful references:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://www.eskimo.com/~scs/C-faq/top.html>
    <http://benpfaff.org/writings/clc/off-topic.html>
     
    CBFalconer, Apr 12, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Petzold

    problem sending code via rs232

    Martin Petzold, Apr 12, 2004, in forum: C++
    Replies:
    8
    Views:
    446
    red floyd
    Apr 13, 2004
  2. Replies:
    2
    Views:
    5,537
    Ian Collins
    Jan 19, 2006
  3. Problem with rs232

    , Jul 5, 2007, in forum: C++
    Replies:
    4
    Views:
    361
  4. Marco Savioli
    Replies:
    0
    Views:
    492
    Marco Savioli
    Jul 14, 2007
  5. jasperng
    Replies:
    0
    Views:
    2,053
    jasperng
    Oct 8, 2008
Loading...

Share This Page