Archived

This topic is now archived and is closed to further replies.

This topic is 5657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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);

}

Share this post


Link to post
Share on other sites