Hi Everyone.
Apologies for the length of my first post but this one has perplexed me an bit in my project.
Im creating an Windows Media Player Visualization in VS2008 and am drawing an blank on the below.
What i need to do with the below code is to create an array of the CreateFile handles which i can then use in the WriteFile function lower down. The problem im having is that i cant seem to get it to follow the usual syntax.
e.g.
HANDLE comports[10];
comports[0] = CreateFile("\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
(this results in VS2008 trying to default comports[0] to an int)
This is the code i have so far (which does work for an single com port, but im working on having it do the same on multiple)
#include "StdAfx.h"
#include "stdio.h"
#include "stdlib.h"
#include "USBGlowLight.h"
#include "CPropertyDialog.h"
#include "math.h"
#include "string.h"
INT iDelayVal = 150;
DWORD bytes_read = 0; // Number of bytes read from port
DWORD bytes_written = 0; // Number of bytes written to the port
// Open COM port(s)
HANDLE comport=CreateFile("\\\.\\COM6", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DCB dcb = {0};
COMMTIMEOUTS CommTimeouts;
STDMETHODIMP CUSBGlowLight::Render(TimedLevel *pLevels, HDC hdc, RECT *prc)
{
COLORREF mycolor;
COLORREF oldmycolor;
//paint old Color on screen
HBRUSH hOldBrush = ::CreateSolidBrush( oldmycolor );
::FillRect( hdc, prc, hOldBrush );
int y = 0;
//Red colour
for (int x = 1; x < ((SA_BUFFER_SIZE)/3); ++x)
{
y = y + static_cast<int>(256.0f * pLevels->frequency[0][x - (prc->left - 1)]);
}
int myRlevel = y / ((SA_BUFFER_SIZE)/3);
y = 0;
//Green colour
for (int x = ((SA_BUFFER_SIZE)/3); x < (((SA_BUFFER_SIZE)/3)*2); ++x)
{
y = y + static_cast<int>(256.0f * pLevels->frequency[0][x - (prc->left - 1)]);
}
int myGlevel = y / ((((SA_BUFFER_SIZE)/3)*2) - ((SA_BUFFER_SIZE)/3));
y = 0;
//Blue colour
for (int x = (((SA_BUFFER_SIZE)/3)*2); x < (SA_BUFFER_SIZE-1); ++x)
{
y = y + static_cast<int>(256.0f * pLevels->frequency[0][x - (prc->left - 1)]);
}
int myBlevel = y / ((SA_BUFFER_SIZE-1) - (((SA_BUFFER_SIZE)/3)*2));
y = 0;
//convert to hex code
BYTE RED = myRlevel;// - (256.0f/2);
BYTE GREEN = myGlevel;// - (256.0f/2);
BYTE BLUE = myBlevel;// - (256.0f/2);
COLORREF c = RGB(RED, GREEN, BLUE);
char str[8];
sprintf(str, "#%06X", SwapBytes(c));
//Choose whether to show the Visual or display Black
switch (m_nPreset)
{
case PRESET_BLANK:
{
mycolor = m_clrForeground;
}
break;
case PRESET_SHOW:
{
mycolor = RGB( myRlevel, myGlevel, myBlevel);;
}
break;
}
//Copy data to the outputbuffer ( for the comm port)
char OUTBUFFER[400];
strcpy(&OUTBUFFER[0], str);
//write data to comm port
WriteFile(comport, &OUTBUFFER, 7, &bytes_written, NULL);
//paint Color on screen
HBRUSH hNewBrush = ::CreateSolidBrush( mycolor );
::FillRect( hdc, prc, hNewBrush );
oldmycolor = mycolor;
//Sleep in between visuals
Sleep(iDelayVal);
if (hNewBrush)
{
::DeleteObject( hNewBrush );
}
return S_OK;
}
Apologies again if the code above looks quite messy, It's an work in progress to clean it up, And again, thanks for any help that anyone can give me :)
Steve