Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting these errors

hSerial does not name a type <br />
expected unqualified identifiers before if


C++
#include<windows.h>

HANDLE hSerial;   
// error here// hSerial = createFile ("COM4",
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      FILE_ATTRIBUTE_NORMAL,
                      0);
// error here// if(hSerial==INVALID_HANDLE_VALUE)
{
    if(GetLastError()=ERROR_FILE_NOT_FOUND){
        printf("//SERIAL PORT DOES NOT EXIST// ");
    }
    printf("//SOME OTHER ERROR OCCURED.//");
}



DCB dcbSerialParams = {0};
dcbSerial.DCBlength=Sizeof(dcbSerialParams);

if
    (!GetCommState(hSerial, &dcbSerialParams))
{


    
    Printf("Error in getting state");
}

dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParams)){
//error setting serial port state
printf("Error");
}


COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts)){
//error occureed. Inform user
printf("Error occuring in timeouts");
}

char szBuff[n + 1] = {0};
DWORD dwBytesRead = 0;
if(!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL)){
//error occurred. Report to user.
printf("Error occured in read/write byte");
}
CloseHandle(hSerial);
Posted
Updated 18-Mar-15 5:34am
v2

You have not provided enough code for a firm answer. My best guess is you have a missing or extra curly brace eg. '{' or '}'.

C/C++ is case sensitive. You have the incorrect case for some functions and keywords. Look for "Printf", "createFile", and "Sizeof" for example.

If the source you've provided is in fact the entire source, your code needs to appear inside a main() function.

C++
#include <windows.h>

int main(int argc, char *argv[])
{
    HANDLE hSerial = CreateFile ("COM4",
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0);
    if( hSerial==INVALID_HANDLE_VALUE )
    {
        if(GetLastError()=ERROR_FILE_NOT_FOUND
        {
            printf("//SERIAL PORT DOES NOT EXIST// ");
        }
        printf("//SOME OTHER ERROR OCCURRED.//");
        return 0;
    }
 
    DCB dcbSerialParams = {0};
    dcbSerial.DCBlength = sizeof dcbSerialParams;
 
    if ( !GetCommState(hSerial, &dcbSerialParams) )
    {
        printf("Error in getting state");
    }
 
    dcbSerialParams.BaudRate=CBR_9600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;
    if( !SetCommState(hSerial, &dcbSerialParams) )
    {
        //error setting serial port state
        printf("Error");
    }

    COMMTIMEOUTS timeouts={0};
    timeouts.ReadIntervalTimeout=50;
    timeouts.ReadTotalTimeoutConstant=50;
    timeouts.ReadTotalTimeoutMultiplier=10;
    timeouts.WriteTotalTimeoutConstant=50;
    timeouts.WriteTotalTimeoutMultiplier=10;
    if( !SetCommTimeouts(hSerial, &timeouts) )
    {
        //error occurred. Inform user
        printf("Error occurring in timeouts");
    }
 
    char szBuff[n + 1] = {0};
    DWORD dwBytesRead = 0;
    if( !ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL) )
    {
        //error occurred. Report to user.
        printf("Error occurred in read/write byte");
    }
    CloseHandle(hSerial);
    return 0;
}
</windows.h>
 
Share this answer
 
#include <windows.h>
#include <stdio.h>

int main()
{

// Defining hexadecimal bytes
int bytes_to_send[5];
bytes_to_send[0] = 0x81;
bytes_to_send[1] = 0x11;
bytes_to_send[2] = 0xF1;
bytes_to_send[3] = 0x11;
bytes_to_send[4] = 0x04;

// Declare variables and structures
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};

// Open the highest available serial port number
fprintf(stderr, "Opening serial port...");
hSerial = CreateFile(
"COM3", GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hSerial == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error\n");
return 1;
}
else fprintf(stderr, "OK\n");

// Set device parameters (10400 baud, 1 start bit,
// 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error getting device state\n");
CloseHandle(hSerial);
return 1;
}

dcbSerialParams.BaudRate = 10400;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error setting device parameters\n");
CloseHandle(hSerial);
return 1;
}

// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if(SetCommTimeouts(hSerial, &timeouts) == 0)
{
fprintf(stderr, "Error setting timeouts\n");
CloseHandle(hSerial);
return 1;
}

// Send specified text (remaining command line arguments)
DWORD bytes_written, total_bytes_written = 0;
fprintf(stderr, "sending bytes...");


if(!WriteFile(hSerial, bytes_to_send, 5, &bytes_written, NULL))
{
fprintf(stderr, "Error\n");
CloseHandle(hSerial);
return 1;
}
fprintf(stderr, "%d bytes written\n", bytes_written);



// Close serial port
fprintf(stderr, "Closing serial port...");
if (CloseHandle(hSerial) == 0)
{
fprintf(stderr, "Error\n");
return 1;
}
fprintf(stderr, "OK\n");

// exit normally
return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900