VincentO

Hi all,

I would like to find out why programming the serial port using visual C++ from the Visual studio .NET 2002 failure

In the past I used to programme the serial communication port using visual C++ 6 from the Visual studio (without .NET version) and it happens to be fine. The <Windows.h> file is included for your info.

Regards

Vincent



Re: Visual C++ General Serial Port programming using Visual studio .NET 2002 Enterprise

Bruno van Dooren

You mean a VC6 application worked, but after upgrading to VC2002 it doesn't

In that case you'll have to give us more detail. which function fails, and which error code is returned

Without that I can only tell you to have a look at the serial port samples at www.codeproject.com





Re: Visual C++ General Serial Port programming using Visual studio .NET 2002 Enterprise

Ramkrishna Pawar

Do you by any chance mean that the compiler failed to compile your code in VS.NET 2002






Re: Visual C++ General Serial Port programming using Visual studio .NET 2002 Enterprise

VincentO

Yes, at home i use VC6 (no ,NET). Current at my work place, i use a new installation of visual studio.NET 2002 Enterprise = VC7

problems complain by the complier,

Could not find HANDLE, DWORD, COMMTIMEOUTS, DCB, GetCommState... so on and so for for the various instant needed to initialise the serial port. I'm puzzled by that because I have added the <Windows.h>. By right that should do the trick. At home it can compile and work. At work place it fails.

Below is part of my code sample. should be easy for u guys.

#include <Windows.h>
#define BUFSIZE 128

int main(int argc, char* argv[])
{
DCB myDCB;
COMMTIMEOUTS myTimeout;
DWORD bytesWritten, commStatus;
HANDLE hcom;
/*Specify the Serial Port to be Used "Com#" Where # is the Port Number*/
char myPort[] = "Com4";
char myData[100];
/*Open Comm Port with Read/Write access, Exclusive Access, No Security, Open Existing
No Overlapping IO, NULL Handler*/
hcom = CreateFile(myPort,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (hcom == INVALID_HANDLE_VALUE)
{
printf("Serial Port Open Error: %d\n",GetLastError());
return 0;
}
/*Setup Comm Port Input and Output Buffer Size*/
SetupComm(hcom,BUFSIZE,BUFSIZE);

/*Get Default DCB Values and Overwrite Required Values*/
if(GetCommState(hcom,&myDCB)==0)
{
printf("Serial Port Values Unavailable\n");
}
else
{
myDCB.BaudRate = CBR_9600;
myDCB.ByteSize = 8;
myDCB.Parity = NOPARITY;
myDCB.StopBits = ONESTOPBIT;
myDCB.fAbortOnError = TRUE;
myDCB.EofChar = 10;
if(SetCommState(hcom,&myDCB)==0)
{
printf("Unable to write Serial Port Configuration Values\n");
}
else
{
GetCommTimeouts(hcom,&myTimeout);
myTimeout.ReadIntervalTimeout = 50;
myTimeout.ReadTotalTimeoutConstant = 50;
myTimeout.ReadTotalTimeoutMultiplier = 10;
myTimeout.WriteTotalTimeoutConstant = 50;
myTimeout.WriteTotalTimeoutMultiplier = 10;
SetCommTimeouts(hcom,&myTimeout);
printf("Waiting for Data\n");
SetCommMask(hcom,EV_RXCHAR|EV_ERR);
WaitCommEvent(hcom,&commStatus,0);

if (commStatus & EV_RXCHAR)
{
ReadFile(hcom,&myData,20,&bytesWritten,0);
printf("Data Recieved: %s\n", myData);
}
else if(commStatus & EV_ERR)
{
printf("Error Reading From %s", myPort);
}
}
}
CloseHandle(hcom);
getch();
return 0;
}





Re: Visual C++ General Serial Port programming using Visual studio .NET 2002 Enterprise

Bite Qiu - MSFT

The only reason I can think of is that windows.h is not properly included, could you show us the exact error messages you have got Do you use precompile header, if so, make sure #include "stdafx.h" is the first line in your cpp file.






Re: Visual C++ General Serial Port programming using Visual studio .NET 2002 Enterprise

Simple Samples

I compiled your code using both VC 6 and VC 2005. The only errors I got from both are that printf and getch are undeclared or not found.






Re: Visual C++ General Serial Port programming using Visual studio .NET 2002 Enterprise

VincentO

1) For your information I uses the VC++.NET 2002. Its not VC++ 2005.

2) I have not remove any #include "stdafx.h".

3) I have added <Windows.h>

The error i receive was:

C2065: 'Closehandle' undeclared identifier

C2065: 'DWORD' undeclared identifier

C2065: 'COMMTIMEOUTS' undeclared identifier

C2065: 'DCB' undeclared identifier

C2065: 'HANDLE' undeclared identifier

I know this knows strange but it just happen this way... If possible can anyone with Visual studio .NET 2002 Enterprise

try compiling it

Thanx

Vincent





Re: Visual C++ General Serial Port programming using Visual studio .NET 2002 Enterprise

Simple Samples

There is something you can do very quickly. You can right-click on Windows.h within the #include <Windows.h> and then in the context menu select the command to open the #include file. It is helpful to open the #include that way because then you know you are getting the same copy that the compiler is getting. Then look at the file to see what is in it. Are the missing items in there If not then you know it is not the compiler and you need to determine why the Windows.h file is incorrect. If the missing items are there then you need to determine why they are not being found by the compiler. I doubt that the problem is the (version of the) compiler.






Re: Visual C++ General Serial Port programming using Visual studio .NET 2002 Enterprise

Bite Qiu - MSFT

Both 'DCB' and 'CreateFile' are declared in winbase.h, it is very strange that compiler found 'CreateFile' but not 'DCB'.