Thread: serial port still wont work

  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,829
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    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, 09: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, 10:19 AM