C++ code

hello
I have a problem oppening a com port with C++,
I am using this code to stablish a serial communication , it works on regular serial port like(COM1), but does not work with my Arduino port (COM6):

typedef void *HANDLE;
HANDLE myHandle;
#define GENERIC_READ (0x80000000L)
#define GENERIC_WRITE (0x40000000L)
#define NULL 0
#define OPEN_EXISTING 3
#define FILE_ATTRIBUTE_NORMAL 0x00000080
myHandle=CreateFileA("COM6", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

any idia what is wrong here or any other way to do it?
Thank you

Why is this a poll?

The CreateFileA function is not meant to be called directly. The CreateFile() method is. It does not return a pointer of type NULL, so it isn't clear why you are defining the return type variable as void *.

C# is so much easier to use for Windows applications that it boggles the mind why anyone still tries using C++ to talk to the serial port or create Windows applications.

babakmanafi:
any idia what is wrong here or any other way to do it?

What is wrong exactly? Compiler error? Runtime crash? It just doesn't do what you expect? What do you expect, and what does it do?

I just need to recieve bytes from serial port comming from my arduino board.
that code works when I use my secound computer as sender using COM1 , but when I connect my arduino board using USB cable(COM6) the code
does not return the right HANDLE to use for sending and recieving datas,
I don't see any error but can't recieve the correct datas (it recieve a constant number)

but when I connect my arduino board using USB cable(COM6) the code
does not return the right HANDLE to use for sending and recieving datas,
I don't see any error but can't recieve the correct datas (it recieve a constant number)

How do you know that the program is not returning the correct HANDLE?

What code is running on the Arduino?

What do you see in your application?

Feel free to volunteer any other data that would help us solve your problem.

here are the codes that I am using:
c++

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
using namespace std;

HANDLE OpenRS232(const char* comName, DWORD BaudRate){
	DCB CommDCB;
	COMMTIMEOUTS CommTimeouts;
	HANDLE myHandle;
	myHandle=CreateFileA(LPCSTR(comName), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	
	if(GetLastError()!=ERROR_SUCCESS)
	{	
        return INVALID_HANDLE_VALUE;
	}
	else
	{
		GetCommState(myHandle, &CommDCB);
		CommDCB.BaudRate=BaudRate;
		CommDCB.Parity=NOPARITY;
		CommDCB.StopBits=ONESTOPBIT;
		CommDCB.ByteSize=8;
		CommDCB.fBinary=1;  
		CommDCB.fParity=0;
		CommDCB.fOutxCtsFlow=0;
		CommDCB.fOutxDsrFlow=0;
		CommDCB.fDtrControl=0;
		CommDCB.fDsrSensitivity=0;
		CommDCB.fTXContinueOnXoff=0;
		CommDCB.fOutX=0;
		CommDCB.fInX=0;
		CommDCB.fErrorChar=0;
		CommDCB.fNull=0;
		CommDCB.fRtsControl=RTS_CONTROL_TOGGLE;
		CommDCB.fAbortOnError=0;
		SetCommState(myHandle, &CommDCB);
		SetupComm(myHandle, 100, 100);
		GetCommTimeouts(myHandle, &CommTimeouts);
		CommTimeouts.ReadIntervalTimeout=MAXDWORD;
		CommTimeouts.ReadTotalTimeoutMultiplier=0;
		CommTimeouts.ReadTotalTimeoutConstant=0;
		SetCommTimeouts(myHandle, &CommTimeouts);
		return myHandle;
  }

}

//Close the serial port
//Usage: CloseRS232(ComHandle);
void CloseRS232(HANDLE Handle)
{
	CloseHandle(Handle);
}

int main(int argc, char* argv[])
{
   const char *ComPort;
   HANDLE RS232Handle;
   RS232Handle=OpenRS232("COM1", 9600);

	if (RS232Handle==INVALID_HANDLE_VALUE) return -1;
	BYTE Byte;          
	DWORD dwBytesTransferred;		
	while(1){
            ReadFile ( RS232Handle, &Byte,1 ,&dwBytesTransferred, NULL);
            cout<<"      Recieved = "<< int(Byte)<<endl;
			}

	CloseRS232( RS232Handle);
	return 0; 
}

and this is Arduino code:

void setup() 
{ 
  Serial.begin(9600); 
  pinMode(13, OUTPUT);     
} 


byte thisByte = 1; 
void loop() 
{ 
  Serial.write(thisByte);    
  if(thisByte == 255)   thisByte=1;
  digitalWrite(13, HIGH);   
  delay(200);              
  digitalWrite(13, LOW);    
  delay(200);              
  thisByte=thisByte+1;  
}

It is look like there is something diferent between regular Computer COM port and Arduino USB (Virtual COM) communication that I dont know about.