COM

Started by
2 comments, last by Pillejunior 21 years, 10 months ago
Does someone can give me an example how to open your com port with createfile and how to read and write to that??
Advertisement
Anyone???
Just call CreateFile, ReadFile and WriteFile. The docs on CreateFile specify how ports should be opened, and I suppose reading/writing COM ports is the same as reading/writing normal files.
---visit #directxdev on afternet <- not just for directx, despite the name
I think you''ve mistitled your question. It''s too easy to read COM as "Component Object" rather than "Serial Communications Port" - at any rate - here''s some code for you.


  //***************************************************************// From the book "Win32 System Services: The Heart of Windows 98// and Windows 2000"// by Marshall Brain// Published by Prentice Hall//// Copyright 1995, by Prentice Hall.//// This code demonstrates an extremely simple serial connection.//***************************************************************// commtest.cpp#include <windows.h>#include <stdio.h>void ErrorHandler(char *s, DWORD err){	fprintf(stderr,"%s\n", s);	fprintf(stderr,"Error number: %s\n", err);	ExitProcess(1);}void main(){	HANDLE comHandle;	BOOL success;	DCB dcb;	char str[100];	DWORD numWrite, numRead;	COMMTIMEOUTS timeouts;	// Open the comm port. Can open COM, LPT,	// or \\\\.\\TELNET	comHandle = CreateFile("COM2"				, GENERIC_READ|GENERIC_WRITE				, 0, 0				, OPEN_EXISTING				, FILE_ATTRIBUTE_NORMAL				, 0);	if ( comHandle == INVALID_HANDLE_VALUE ) {		ErrorHandler("In CreateFile", GetLastError());	}	// Get the current settings of the COMM port	success = GetCommState(comHandle, &dcb);	if ( !success ) {		ErrorHandler("In GetCommState", GetLastError());	}	// Modify the baud rate, etc.	dcb.BaudRate = 2400;	dcb.ByteSize = 8;	dcb.Parity = NOPARITY;	dcb.StopBits = ONESTOPBIT;	// Apply the new comm port settings	success = SetCommState(comHandle, &dcb);	if ( !success ) {		ErrorHandler("In SetCommState", GetLastError());	}	// Change the ReadIntervalTimeout so that	// ReadFile will return immediately. See	// help file	timeouts.ReadIntervalTimeout = MAXDWORD;	timeouts.ReadTotalTimeoutMultiplier = 0;	timeouts.ReadTotalTimeoutConstant = 0;	timeouts.WriteTotalTimeoutMultiplier = 0;	timeouts.WriteTotalTimeoutConstant = 0;	SetCommTimeouts( comHandle, &timeouts );	// Set the Data Terminal Ready line	EscapeCommFunction(comHandle, SETDTR);	// Send an "at" command to the modem	// Be sure to use \r rather than \n	strcpy(str, "at\r");	success = WriteFile(comHandle					, str					, strlen(str)					, &numWrite					, 0);	if ( !success ) {		ErrorHandler("In WriteFile", GetLastError());	}	// Wait 2 seconds and then retrieve from the	// modem	Sleep(2000);	success = ReadFile(comHandle, str, 100, &numRead, 0);	if ( !success ) {		ErrorHandler("In ReadFile", GetLastError());	}	// Print the string received	fprintf(stdout,"%d\n", numRead);	str[numRead]=''\0'';	fprintf(stdout, "%s\n", str);	// Clear the DTR line	EscapeCommFunction(comHandle, CLRDTR);	CloseHandle(comHandle);}  

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement