serial port still wont work

This is a discussion on serial port still wont work within the C++ Programming forums, part of the General Programming Boards category; Below are first my main file and then the port write function I have got no errors but i dont ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    14

    serial port still wont work

    Below are first my main file and then the port write function I have got no errors but i dont know where I am going wrong as the 3 byte protocol still wont send to the serial port!! can anyone help me achieve the impossible and send the three byte protocol??
    thanks



    /************************************************** *********************

    WINMAIN ( HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine,
    int nCmdShow)

    ************************************************** *********************/

    int WINAPI WinMain( HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine,
    int nCmdShow)



    {


    hPort = INVALID_HANDLE_VALUE; // Serial port handle
    hReadThread = NULL; // Handle to the read thread

    unsigned char Bytes[3] = { 255, 1, 127 } ;

    lpszDevName = TEXT("COM1:");

    // Initialize the port.
    if (!PortInitialize (lpszDevName))
    {
    return 0 ;
    }


    PortWrite(Bytes) ;

    PortClose(hPort) ;


    return 0 ;

    }



    /************************************************** *********************

    PortWrite (BYTE Byte)

    ************************************************** *********************/
    void PortWrite (unsigned char *Bytes)
    {
    DWORD dwError,
    dwNumBytesWritten;


    if (!WriteFile (hPort, // Port handle
    &Bytes, // Pointer to the data to write
    3, // Number of bytes to write
    &dwNumBytesWritten, // Pointer to the number of bytes
    // written
    NULL)) // Must be NULL for Windows CE
    {
    // WriteFile failed. Report error.
    dwError = GetLastError ();
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Its difficult to debug what you've got since it is incomplete.

    I would recommend looking at http://www.programmersheaven.com/zone3/cat411/index.htm

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    14
    could i mail you my code in full so that you can spot why it wont send the bytes to the serial port??

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    No problem. Just push the email button and enter your code. Please use "CODE" (replace " with [ and ] respectively) to start code and "/CODE"to end it. This will keep it much cleaner.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Just a simple question, are you sure you understand the passing of char * to functions?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Doesn't look like it
    > void PortWrite (unsigned char *Bytes)
    ...
    > &Bytes, // Pointer to the data to write
    This isn't true
    It should be Bytes or &Bytes[0]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sending streams over serial port in win32
    By deian in forum C Programming
    Replies: 2
    Last Post: 07-13-2009, 12:01 PM
  2. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  3. Serial port Communication
    By vin_pll in forum C++ Programming
    Replies: 23
    Last Post: 01-07-2009, 08:32 AM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Problem with string and serial port
    By collinm in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21