Click here to Skip to main content
15,895,667 members
Articles / Programming Languages / C#

Improving the Performance of Serial Ports Using C#: Part 2

Rate me:
Please Sign up or sign in to vote.
4.39/5 (9 votes)
13 Oct 2011CPOL9 min read 46.2K   2.5K   38  
Simple test programs designed to demonstrate performance issues with the .NET serial port interface and what might be done to improve things.
// RemoteTimeReaderC.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "conio.h"
#include "windows.h"
#include "winbase.h"

enum
{
    NotConnected,
    ConnectionEstablished,
    ConnectionIdle,
    CheckConnected
}status;


bool SendInfoMessage(HANDLE hSerialPort, char* data)
{
	char message[18];

	message[0] = 0x05;
	memcpy(&message[1], &data[1], 9);

	SYSTEMTIME st;

	union
	{
		FILETIME ft;
		char bytes[8];
	}nowTime;

	GetLocalTime(&st);
	SystemTimeToFileTime(&st, &(nowTime.ft));

	memcpy(&message[10], nowTime.bytes, 8);

	DWORD bytesSent;

	if (WriteFile(hSerialPort, message, 18, &bytesSent, NULL) == 0)
	{
		cprintf("Failed to send message\n");
		return false;
	}

	if (bytesSent != 18)
	{
		cprintf("Failed to send complete message\n");
		return false;
	}

	return true;
}

bool SendOneByteMessage(HANDLE hSerialPort, char dataByte)
{
		DWORD bytesSent;

		if (WriteFile(hSerialPort, &dataByte, 1, &bytesSent, NULL) == 0)
		{
			cprintf("Failed to send message\n");
			return false;
		}

		if (bytesSent != 1)
		{
			cprintf("Failed to send complete message\n");
			return false;
		}

		return true;
}

bool AcknowledgementTimerElapsed(HANDLE hSerialPort)
{
/*	
	if (status == NotConnected)
		cprintf("Ack Time NotConnected\n");
	else if (status == ConnectionEstablished)
		cprintf("Ack Time ConnectionEstablished\n");
	else if (status ==	ConnectionIdle)
		cprintf("Ack Time ConnectionIdle\n");
	else if (status == CheckConnected)
		cprintf("Ack Time CheckConnected\n");
	else
		cprintf("Ack Time Invalid\n");
*/		

    if (status == ConnectionEstablished)
        status = ConnectionIdle;
    else if (status == ConnectionIdle || status == NotConnected)
    {
        if (status == ConnectionIdle)
            status = CheckConnected;

        return SendOneByteMessage(hSerialPort, 0x01);
    }
    else if (status == CheckConnected)
    {
        cprintf("Communication lost - Timeout on acknowledge request\n");
        return false;
    }

    return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
	char receivedMessage[10];
	int messageIndex = 0;
	status = NotConnected;

	HANDLE hSerialPort = CreateFile(_T("COM1"), 
            GENERIC_READ | GENERIC_WRITE, 
            0, 
            NULL, 
            OPEN_EXISTING, 
            0, 
            NULL);

	if (hSerialPort == INVALID_HANDLE_VALUE)
	{
		cprintf("Cannot get serial port handle\n");
		return 0;
	}

	DCB dcbConfig;

	if(GetCommState(hSerialPort, &dcbConfig))
	{
		dcbConfig.BaudRate = 9600;
		dcbConfig.ByteSize = 8;
		dcbConfig.Parity = NOPARITY;
		dcbConfig.StopBits = ONESTOPBIT;
		dcbConfig.fDsrSensitivity = FALSE;
		dcbConfig.fDtrControl = DTR_CONTROL_ENABLE;
		dcbConfig.fBinary = TRUE;
		dcbConfig.fRtsControl = RTS_CONTROL_ENABLE;
		dcbConfig.fOutxCtsFlow = TRUE;
	}
	else
	{
		cprintf("Cannot get comm state\n");
		return 0;
	}

	if(!SetCommState(hSerialPort, &dcbConfig))
	{
		cprintf("Cannot set comm state\n");
		return 0;
	}

	COMMTIMEOUTS commTimeout;

	if(GetCommTimeouts(hSerialPort, &commTimeout)) /* Configuring Read & Write Time Outs */
  	{
  		commTimeout.ReadIntervalTimeout = 1000;
  		commTimeout.ReadTotalTimeoutConstant = 1000;
  		commTimeout.ReadTotalTimeoutMultiplier = 0;
  		commTimeout.WriteTotalTimeoutConstant = 10000;
  		commTimeout.WriteTotalTimeoutMultiplier = 0;
  	} 
  	else
	{
		cprintf("Get timeout fail\n");
		return 0;
	}
  
  	if(!SetCommTimeouts(hSerialPort, &commTimeout))
	{
		cprintf("Set timeout fail\n");
		return 0;
	}
   
	while(true)
	{
		char buffer[1000];
		DWORD readSize;

		if (ReadFile(hSerialPort, buffer, 1000, &readSize, NULL) != 0)
		{
			for (int iii=0; iii<1000 && iii<(int)readSize; iii++)
			{
				if (messageIndex == 0)
				{
					if (buffer[iii] == 0x02)
					{
						status = ConnectionEstablished;
						if (!SendOneByteMessage(hSerialPort, 0x03))
							return 0;
					}

					if (status == NotConnected)
						continue;

					if (buffer[iii] == 0x04)
					{
						status = ConnectionEstablished;
						receivedMessage[0] = buffer[iii];
						messageIndex++;
					}
				}
				else
				{
					status = ConnectionEstablished;
					receivedMessage[messageIndex] = buffer[iii];
					messageIndex++;
					if (messageIndex == 10)
					{
						messageIndex = 0;
						if (!SendInfoMessage(hSerialPort, receivedMessage))
							return 0;
					}
				}
			}

		}

		if ( readSize == 0 && !AcknowledgementTimerElapsed(hSerialPort))
			break;
		 
 	}

	return 0;
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Dotric Pty Ltd
Australia Australia
Doug graduated from Deakin University Geelong, Victoria, Australia with a Bachelor of Engineering.

Early in his career, Doug worked on an automated system for testing telephone lines. This system used a network of DEC PDP11s. The software was written in Oregon Pascal with an event driven structure. This early involvement in event driven structures proved to be an invaluable stepping stone into Windows programming some years latter.

Doug completed a Graduate Diploma in Education at La Trobe University to become a qualified secondary school Mathematics and Physics teacher. Subsequent IT contracts were with universities. One such contract was to add functionality to MSN Messenger.

In recent times Doug has been working on database and Android applications.

Comments and Discussions