Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Unable to open serial port in WinXP using C

Status
Not open for further replies.

Embedded_Geek

Full Member level 6
Full Member level 6
Joined
Jul 5, 2010
Messages
342
Helped
58
Reputation
116
Reaction score
56
Trophy points
1,318
Location
Germany
Activity points
2,974
I am unable to open serial port in WinXP using C. It is returning error even though the correct COM port has been specified. The code is given below:

Code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

void main () 
{
	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){

			printf("FAILED");
		}
       }
}


Waiting for a solution/advice.

Thanks in advance,
 

Is the COM port locked by another application? Is it available at all?

It got resolved. Com port should be specified as "COM1:" and not as "COM1". The colon should be included.
 

If this is a commercial app, you should check on another machine. The colon is not normal (at least, it isn't on my Win7 machine).
It may be specific to the serial card driver perhaps (I'm no windows expert).
If the com port number is greater than 9 (or 10, I can't remember) then the syntax does change, but not with colons.
 
If this is a commercial app, you should check on another machine. The colon is not normal (at least, it isn't on my Win7 machine).
It may be specific to the serial card driver perhaps (I'm no windows expert).
If the com port number is greater than 9 (or 10, I can't remember) then the syntax does change, but not with colons.

I am still getting an error. The error is ERROR_INVALID_NAME.

Any idea what could be the reason????
 

This works for me on Win 7
Code:
	DWORD  dir =GENERIC_READ | GENERIC_WRITE;
	HANDLE comh = CreateFile(port, dir, 0, 0, OPEN_EXISTING, 0, 0);
where port is a string (well, char array) set to "COM1" (with a '\0' string terminator on the end of couse). This works up to COM9.
If it is not working for you, then you need to check your serial port driver, maybe in System->Device Manager,
or try on a different machine. Yours may have a weird serial port hardware or driver. My code works on
a computer with a USB serial port adapter thingy (cp2102 based, driver downloaded from silabs), but the code
would work on virtually any serial port. Maybe XP does things differently. I think you need to check on a recent OS and
maybe a different computer with different serial port hardware.
 

Code:
#include <STDIO.H>
#include <CONIO.H>
#include <WINDOWS.H>
unsigned char szBuff[2];
unsigned char szrBuff[2];
char szRead[20];
DWORD dwNum;
int main() {
	//printf("Hello World");
	//thu mo cong com 1 xem sao
	HANDLE hComm;
	hComm=CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,\
		0,0,OPEN_EXISTING,0,0);
	if(!hComm) {
		printf("Khong mo duoc\n");
		return 0;
	} else {
		printf("mo thanh cong\n");
	}
	DCB dcb;
	memset(&dcb,0,sizeof(dcb));
	if(!GetCommState(hComm,&dcb)) {
		printf("Khong lay duoc thong so cong COM\n");
		CloseHandle(hComm);
		return 0;
	} else {
		printf("Lay thanh cong thong so cong COM\n");
	}
	dcb.BaudRate=CBR_9600;
	//dcb.fParity=0;
	if(!SetCommState(hComm,&dcb)) {
		printf("Khong dat duoc thong so cong\n");
		CloseHandle(hComm);
		return 0;
	} else {
		printf("Dat thong so 9600\n");
	}
	while(1) {
		szBuff[0]='A';
		WriteFile(hComm,szBuff,1,&dwNum,NULL);
		Sleep(500);
		szBuff[0]='B';
		WriteFile(hComm,szBuff,1,&dwNum,NULL);
		Sleep(500);
	}
	CloseHandle(hComm);
	
	getch();
	return 0;
}
above code work fine
 

Yes, that code will work, but Win 7 goes a bit wierd after COM9, so best to use the slash method that srizbf mentions, because that works not only for COM0-9 but for all COMs after that too (My devices have reached COM18 or something.. : )
 

Use \\\\.\\COM9 instead COM9
Work fine, I tested.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top