Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C/C++ Serial Port

Options
  • 06-02-2004 12:52pm
    #1
    Registered Users Posts: 7,860 ✭✭✭


    im about to start a program to communicate with a 3330 through a nokia cable attached to COM1 and cant figure out how to send the AT/FBUS commands down the line. can anyone help me with this? i heard its meant to be the same as writing to a file except using a pointer to the com port somehow?


Comments

  • Registered Users Posts: 79 ✭✭tendofan


    I'm assuming that your targetting Windows; if you're not I can't help I'm afraid.

    Basically you use the CreateFile API call to open the file, then WriteFile/ReadFile to read and write to the port.
    You need to set the port appropriately using the DCB structure and SetCommState API call.

    For a full discussion on how to do Serial port comms in Win32 look at the following:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp

    It's a pretty decent article with example code etc.

    Tendofan.


  • Registered Users Posts: 7,860 ✭✭✭The_B_Man


    ye windows.
    im still havin trouble.
    i can set up the port usin the DCB etc but i cant actually send the stream of text down the line. can you post an example of the readfile or writefile functions with wat params i need to put in it ie whether its int, char, char *? eg readfile(int, char, char *)


  • Registered Users Posts: 79 ✭✭tendofan


    Emm, the above link has pretty clear code samples in it, but make sure that you open the port first, then set the DCB up correctly by reading the current one first, changing any required parameters and then setting the DCB.

    Can you post some of your code so I can see where you might be going wrong?

    What's the error you're getting?

    Anyway, assuming you've opened the port for non-overlapped IO, and set the DCB correctly, you do the following:

    char* pszMsg = "This is a test\0";
    DWORD dwLen = strlen(pszMsg);
    DWORD dwWritten = 0L;

    //hPort is the handle to the open com port

    if (FALSE == WriteFile(hPort, pszMsg, dwLen, &dwWritten, NULL))
    {
    //Writing failed...
    }

    To read you'd do the following:

    DWORD dwToRead = 10;

    char* pszBuff = new char[dwToRead];

    DWORD dwRead = 0L;

    if (FALSE == ReadFile(hPort, pszBuff, dwToRead, &dwRead, NULL) || 0 == dwRead)
    {
    //the read didn't work, or we didn't get anything.
    }

    // there's data in pszBuff
    ...
    delete pszBuff;
    pszBuff = NULL;

    Tendofan


  • Registered Users Posts: 7,860 ✭✭✭The_B_Man


    im having another problem. i had my code sending and receiving a single character up until the other night when i was tired and changed something. now it doesnt send!! also, is there a way for me to send a string down the line?



    #include <stdio.h> /* for printf(), scanf() etc. */
    #include <conio.h> /* for clrscr() */
    #include <math.h> /* for sin(), cos(), sqrt etc.. */
    #include <stdlib.h> /* for min(), max() */
    #include <string.h> /* for god knows what! */
    #include <time.h> /* for randomize() function */
    #include <dos.h> /* for delay() function */
    #include <windows.h> /* for ComPort functionality */


    #define NUMBER_OF_BYTES_TO_READ 1
    #define MSG_LENGTH_MAX 1
    #define READ_BUF_SIZE 1

    int main()
    {
    char *lpBuf = 0 ;
    DWORD dwToWrite = 1 ;

    OVERLAPPED osWrite = {0};
    DWORD dwWritten;
    BOOL fRes;


    BOOL fWaitingOnRead = FALSE;
    OVERLAPPED osReader = {0};
    DCB dcb;
    BOOL fSuccess;
    HANDLE hCom;
    char *Data = "AT" ;
    char *InData = "g" ;
    char *hport = "COM1" ;
    char *Byte = "AT" ;
    unsigned long int *dwCommModemStatus = 0 ;
    unsigned long int *dwNumBytesWritten = 0 ;
    unsigned long int dwBytesTransferred = 0 ;




    //////////////////////////////////////////////////////////////////////////////////////
    ///////////////// SETUP /////////////////
    //////////////////////////////////////////////////////////////////////////////////////

    // Create handle for serial port
    hCom = CreateFile( hport,
    GENERIC_READ | GENERIC_WRITE,
    0, // must be opened with exclusive-access
    NULL, // no security attributes
    OPEN_EXISTING, // must use OPEN_EXISTING
    0, // not overlapped I/O
    NULL // hTemplate must be NULL for comm devices
    );

    // If no such com port
    if (hCom == INVALID_HANDLE_VALUE)
    {
    // Handle the error.
    printf ("CreateFile failed with error %d.\n", GetLastError());
    return (1);
    }

    // Build on the current configuration, and skip setting the size
    // of the input and output buffers with SetupComm.

    fSuccess = GetCommState(hCom, &dcb);

    if (!fSuccess)
    {
    // Handle the error.
    printf ("GetCommState failed with error %d.\n", GetLastError());
    return (2);
    }

    // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.

    dcb.BaudRate = CBR_57600; // set the baud rate
    dcb.ByteSize = 8; // data size, xmit, and rcv
    dcb.Parity = NOPARITY; // no parity bit
    dcb.StopBits = ONESTOPBIT; // one stop bit

    fSuccess = SetCommState(hCom, &dcb);

    if (!fSuccess)
    {
    // Handle the error.
    printf ("SetCommState failed with error %d.\n", GetLastError());
    return (3);
    }

    printf ("Serial port %s successfully reconfigured.\n", hport);

    //////////////////////////////////////////////////////////////////////////////////////
    ///////////////// FINISHED SETUP /////////////////
    //////////////////////////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////////////////////////
    ///////////////// write /////////////////
    //////////////////////////////////////////////////////////////////////////////////////


    // Create this writes OVERLAPPED structure hEvent.
    osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (osWrite.hEvent == NULL)
    return FALSE; // Error creating overlapped event handle.

    // Issue write.
    if (!WriteFile(hCom, lpBuf, dwToWrite, &dwWritten, &osWrite))
    {
    if (GetLastError() != ERROR_IO_PENDING)
    {
    printf("WriteFile failed, but it isn't delayed") ;
    fRes = FALSE; // WriteFile failed, but it isn't delayed. Report error and abort.
    }
    else
    { // Write is pending.
    if (!GetOverlappedResult(hCom, &osWrite, &dwWritten, TRUE))
    {
    printf("write pending") ;
    fRes = FALSE;
    }
    else
    {
    printf("Write success") ;
    fRes = TRUE; // Write operation completed successfully.
    }
    }
    }
    else
    {
    printf("\nWriteFile completed immediately\n") ;
    fRes = TRUE; // WriteFile completed immediately.
    }

    CloseHandle(osWrite.hEvent);
    printf("\nend write\n") ;

    //////////////////////////////////////////////////////////////////////////////////////
    ///////////////// end write /////////////////
    //////////////////////////////////////////////////////////////////////////////////////


    //////////////////////////////////////////////////////////////////////////////////////
    ///////////////// read /////////////////
    //////////////////////////////////////////////////////////////////////////////////////
    // Create the overlapped event. Must be closed before exiting
    // to avoid a handle leak.
    printf("\nosreader event thing\n") ;
    osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

    if (osReader.hEvent == NULL)
    printf("\nError creating overlapped event\n") ; // Error creating overlapped event; abort.

    if (!fWaitingOnRead)
    {
    printf("\nIssue read operation\n") ;
    // Issue read operation.
    if (!ReadFile(hCom, &lpBuf, NUMBER_OF_BYTES_TO_READ, &dwBytesTransferred, &osReader))
    {
    printf("\nif (!ReadFile(hCom, &lpBuf, NUMBER_OF_BYTES_TO_READ, &dwBytesTransferred, &osReader))\n") ;
    if (GetLastError() != ERROR_IO_PENDING) // read not delayed?
    {
    printf("\nError in comms, delayed?\n") ; // Error in communications; report it.
    }
    else
    {
    fWaitingOnRead = TRUE;
    printf("\nfWaitingOnRead = TRUE;\n") ;
    }
    }
    else
    {
    printf("\nread completed immediately\n") ;

    printf("Name\tC\tS\ti\tX") ;
    printf("\nlpbuf\t%c\t%s\t%i\t%x\n", &lpBuf,&lpBuf,&lpBuf, &lpBuf ) ;
    printf("\ndw\t%c\t%s\t%i\t%x\n", &dwBytesTransferred, &dwBytesTransferred, &dwBytesTransferred, &dwBytesTransferred) ;
    printf("\nData\t%c\t%s\t%i\t%x\n", &Data,&Data,&Data,&Data) ;
    printf("\nInData\t%c\t%s\t%i\t%x\n", &InData,&InData,&InData,&InData) ;
    printf("\nByte\t%c\t%s\t%i\t%x\n", &Byte,&Byte,&Byte,&Byte) ;

    // read completed immediately
    // HandleASuccessfulRead(lpBuf, dwRead);
    }
    printf("\nEnd read\n") ;
    }
    //////////////////////////////////////////////////////////////////////////////////////
    ///////////////// end read /////////////////
    //////////////////////////////////////////////////////////////////////////////////////
    printf("\nEnd\n") ;
    return 0;
    }



    ps sorry my formatting didnt come through above, its not usually that messy.


Advertisement