Problem getting info out of serial port

Discussion in 'C++' started by Andres, Feb 20, 2009.

  1. Andres

    Andres New Member

    Joined:
    Jul 22, 2008
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    The code is working properly, but somehow I keep on getting 255
    This code is suppose to get information out of the microcontroller via the serial port. The microcontroller has a program that should be sending different values.

    Example:
    X = 123
    Y = 243
    Z = 124

    but instead it sends
    X = 255
    Y = 255
    Z = 255
    Is there something wrong with the serial port code?
    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include<stdio.h>
    using namespace std;
    
    int main()
    {
        
    FILE * pFileUCS;
    
    int d;
    char str [80];
    int x;
    fstream UCS;
    
    UCS.open("UCS.txt");
    pFileUCS = fopen ("UCS.txt", "w+");
    
    //1 Opening the serial port
    HANDLE hSerial;
    hSerial = CreateFile("COM1",
    GENERIC_READ | GENERIC_WRITE,
    0,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);
    if(hSerial==INVALID_HANDLE_VALUE){
    if(GetLastError()==ERROR_FILE_NOT_FOUND){
    cout<<"Serial port does not exist";
    }
    //some other error occurred. Inform user.
    }
    
    //2 Setting Parameters
    DCB dcbSerial;
    DCB dcbSerialParams = {0};
    dcbSerial.DCBlength=sizeof(dcbSerialParams);
    if (!GetCommState(hSerial, &dcbSerialParams)) {
    //error getting state
    }
    dcbSerialParams.BaudRate=CBR_9600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;
    if(!SetCommState(hSerial, &dcbSerialParams)){
    //error setting serial port state
    }
    
    //3 Setting timeouts
    COMMTIMEOUTS timeouts={0};
    timeouts.ReadIntervalTimeout=1;
    timeouts.ReadTotalTimeoutConstant=5;
    timeouts.ReadTotalTimeoutMultiplier=1;
    timeouts.WriteTotalTimeoutConstant=5;
    timeouts.WriteTotalTimeoutMultiplier=1;
    if(!SetCommTimeouts(hSerial, &timeouts)){
    //error occureed. Inform user
    }
    
    int count=0;
    do{
            count++;
    //4 Reading/Writing data
    char szBuff[10000] = {0};
    DWORD dwBytesRead = 0;
    ReadFile(hSerial,szBuff, 10000, &dwBytesRead, NULL);
    cout<<szBuff<<endl;
    //UCS << szBuff << endl;
    if(!ReadFile(hSerial,szBuff, 10000, &dwBytesRead, NULL)){
    cout<<"There was an error"<<endl;      //error occurred. Report to user.
    }
    
    }while(count!=100);
    UCS.close();
    fclose (pFileUCS);
    
     
    //5 Closing down
    CloseHandle(hSerial);
    return 0;
    }
     
    Last edited by a moderator: Feb 21, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You'll probably get a better response if you reformat the code so that it's readable.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice