Thread: Serial Port problem in C

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    Serial Port problem in C

    I wrote a code in C to write and read data in a comport.
    COM3:
    Code:
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <commdlg.h>
    #include <windef.h>
    int nread,nwrite;
    
    void main()
    {
    HANDLE hSerial;
    COMMTIMEOUTS timeouts;
    COMMCONFIG dcbSerialParams;
    char *buffRead;
    char *buffWrite;
    //BYTE data;
    DWORD dwBytesWritten, dwBytesRead;
    int i;
    
    _retry:
    hSerial = CreateFile("COM3",
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_OVERLAPPED, // FILE_ATTRIBUTE_NORMAL
    NULL);
    if ( hSerial == INVALID_HANDLE_VALUE) 
    {
    if (GetLastError() == ERROR_FILE_NOT_FOUND)
    {
    printf(" serial port does not exist \n");
    }
    printf(" some other error occured. Inform user.\n");
    }
    else
    printf("port open successed!\n");
    //DCB dcbSerialParams ;
    //GetCommState( hSerial, &dcbSerialParams.dcb);
    if (!GetCommState(hSerial, &dcbSerialParams.dcb)) 
    {
    printf("error getting state \n");
    }
    dcbSerialParams.dcb.DCBlength = sizeof(dcbSerialParams.dcb);
    
    dcbSerialParams.dcb.BaudRate = 9600;
    dcbSerialParams.dcb.ByteSize = 8;
    dcbSerialParams.dcb.StopBits = TWOSTOPBITS;
    dcbSerialParams.dcb.Parity = NOPARITY;
    dcbSerialParams.dcb.fBinary = TRUE;
    dcbSerialParams.dcb.fDtrControl = DTR_CONTROL_DISABLE;
    dcbSerialParams.dcb.fRtsControl = RTS_CONTROL_DISABLE;
    dcbSerialParams.dcb.fOutxCtsFlow = FALSE;
    dcbSerialParams.dcb.fOutxDsrFlow = FALSE;
    dcbSerialParams.dcb.fDsrSensitivity= FALSE;
    dcbSerialParams.dcb.fAbortOnError = TRUE;
    if (!SetCommState(hSerial, &dcbSerialParams.dcb)) 
    {
    printf(" error setting serial port state \n");
    }
    
    GetCommTimeouts(hSerial,&timeouts);
    //COMMTIMEOUTS timeouts = {0};
    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier= 10;
    if(!SetCommTimeouts(hSerial, &timeouts)) 
    {
    printf("error setting port state \n");
    }
    
    
    //****************Write Operation*********************//
    buffWrite=(char *)malloc(256);
    printf("please input your data:");
    scanf("%s",buffWrite); 
    nwrite=255;
    dwBytesWritten=0;
    //nwrite =1024;
    //dwBytesWritten = 0;
    if (!WriteFile(hSerial, buffWrite, nwrite, &dwBytesWritten, NULL)) 
    { 
    printf("error writing to output buffer \n");
    }
    else
    printf("written data:%s\n",buffWrite);
    system("pause");
    
    //***************Read Operation******************//
    //buffRead=0;
    //buffRead = (char *)malloc(256);
    nread=255;
    //nread = GetFileSize(hSerial,NULL);
    SetFilePointer(hSerial, 0, 0, FILE_BEGIN);
    buffRead = (char *)malloc(256);
    memset(buffRead, 0, 256);
    dwBytesRead=0;
    if (!ReadFile(hSerial,buffRead,nread, &dwBytesRead, NULL)) 
    {
    printf("error reading from input buffer \n");
    }
    else
    printf("Data read from read buffer is:%s\n\n",buffRead);
    CloseHandle(hSerial);
    Sleep(2000);
    goto _retry;
    }
    until Writefile is alright,but in Readfile "Data read from read buffer is:" shows some unrecognizable codes. What's the problem, plz do me a favor.Thx a lot^^

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So what value do you get in dwBytesRead, and is this the expected value?

    > dcbSerialParams.dcb.BaudRate = 9600;
    > dcbSerialParams.dcb.ByteSize = 8;
    > dcbSerialParams.dcb.StopBits = TWOSTOPBITS;
    > dcbSerialParams.dcb.Parity = NOPARITY;
    The default serial connection is 8N1, not 8N2
    If you're pulling an extra bit off each time (compared to a sender assuming 8N1), then you're going to end up with junk.

    Finally - INDENTATION
    SourceForge.net: Indentation - cpwiki
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Serial I/O to RS-232 Port
    By rpinsky in forum C Programming
    Replies: 17
    Last Post: 10-04-2010, 10:20 AM
  2. Serial Port - Problem with binary frames
    By estratos in forum Linux Programming
    Replies: 2
    Last Post: 01-18-2006, 06:22 AM
  3. PC104 Serial Port Programming Problem
    By outerspace in forum C Programming
    Replies: 6
    Last Post: 11-09-2005, 07:07 PM
  4. Problem with string and serial port
    By collinm in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 10:19 AM
  5. Weired serial port problem
    By SuperNewbie in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2003, 05:31 AM