#define _CRT_SECURE_NO_DEPRECATE
#include <mil.h>
#include <windows.h>
#define IMAGE_FILE M_IMAGE_PATH MIL_TEXT("Bird.mim")
#define CONFIG_SPEED 38400
#define CONFIG_STOP_BITS 1
#define CONFIG_DATA_LENGTH 8
#define CONFIG_PARITY M_DISABLE
#define MAX_PORTS 50
typedef struct
{
MIL_TEXT_CHAR ComPortName[1][MAX_PATH];
MIL_TEXT_CHAR System[1][MAX_PATH];
MIL_UINT Device;
MIL_UINT UartNumber;
}SystemInfo;
typedef struct
{
MIL_ID SystemId;
MIL_ID ReceiveBufferId;
MIL_INT8 * ReceiveBuffer;
MIL_UINT ReadSize;
MIL_INT ReadPosition;
SystemInfo *Matrox;
}UartHook;
MIL_INT MFTYPE ReadHook(MIL_INT , MIL_ID , void*);
MIL_INT InitializeComPort(HANDLE *hCom, MIL_INT InterfaceType, SystemInfo *SysInfo);
void InitializeMatroxUart(MIL_ID *MilSystem, SystemInfo *SysInfo, MIL_INT *UartInterfaceType);
void ReadFromComPort(HANDLE hCom, MIL_INT8 *Buffer, MIL_UINT SizeToRead,
OVERLAPPED *Overlapped);
void WriteToComPort(HANDLE hCom, MIL_INT8 *Buffer, MIL_UINT SizeToWrite,
OVERLAPPED *Overlapped);
bool EnumerateComPorts(SystemInfo *SysInfo);
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplaySource,
MilDisplayReceive,
MilSourceImage,
MilReceivedImage;
MIL_INT64 SourceFormat;
OVERLAPPED Overlapped;
HANDLE hCom;
MIL_INT8 *SourceBuffer,
*ReceiveBuffer;
MIL_INT SourceImageSize,
UartInterfaceType;
MIL_UINT32 SentSize;
MIL_UINT lNumBytesTransferred;
long volatile DoSomething = 0;
volatile UartHook UserStruct;
SystemInfo Matrox;
bool validChoice = EnumerateComPorts(&Matrox);
if(!validChoice)
return 0;
memset(&Overlapped, 0, sizeof(Overlapped));
Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
MappAlloc(M_NULL, M_DEFAULT, &MilApplication );
MsysAlloc(M_DEFAULT, Matrox.System[0], Matrox.Device, M_DEFAULT, &MilSystem);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT,
&MilDisplaySource);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT,
&MilDisplayReceive);
MbufRestore(IMAGE_FILE, MilSystem, &MilSourceImage);
MbufInquire(MilSourceImage, M_EXTENDED_ATTRIBUTE, &SourceFormat);
MbufAllocColor(MilSystem,
(MIL_INT)MbufInquire(MilSourceImage, M_SIZE_BAND, M_NULL),
(MIL_INT)MbufInquire(MilSourceImage, M_SIZE_X, M_NULL),
(MIL_INT)MbufInquire(MilSourceImage, M_SIZE_Y, M_NULL),
(MIL_INT)MbufInquire(MilSourceImage, M_TYPE, M_NULL),
SourceFormat,
&MilReceivedImage);
MbufClear(MilReceivedImage, M_COLOR_BLACK);
MbufInquire(MilSourceImage, M_SIZE_BYTE, &SourceImageSize);
MbufInquire(MilSourceImage, M_HOST_ADDRESS, &SourceBuffer);
MbufInquire(MilReceivedImage, M_HOST_ADDRESS, &ReceiveBuffer);
MdispControl(MilDisplaySource, M_TITLE, M_PTR_TO_DOUBLE(MIL_TEXT("MIL Source Image")));
MdispControl(MilDisplayReceive, M_TITLE,
M_PTR_TO_DOUBLE(MIL_TEXT("MIL Received Image through UART.")));
MdispSelect(MilDisplaySource, MilSourceImage);
MdispSelect(MilDisplayReceive, MilReceivedImage);
InitializeMatroxUart(&MilSystem, &Matrox, &UartInterfaceType);
if(!InitializeComPort(&hCom, UartInterfaceType, &Matrox))
return 0;
MosPrintf(MIL_TEXT("The program will now send data from your Matrox board to your ")
MIL_TEXT("com port.\n"));
MosPrintf(MIL_TEXT("%d bytes will be sent, this may take some time.\n"), SourceImageSize);
MosPrintf(MIL_TEXT("Press <Enter> to send data.\n"));
MosGetch();
MsysControl(MilSystem, M_UART_WRITE_STRING_SIZE + M_UART_NB(Matrox.UartNumber),
SourceImageSize);
MsysControl(MilSystem, M_UART_WRITE_STRING + M_UART_NB(Matrox.UartNumber),
(MIL_INT)SourceBuffer);
ReadFromComPort(hCom, ReceiveBuffer, SourceImageSize, &Overlapped);
lNumBytesTransferred = 0;
MsysInquire(MilSystem, M_UART_BYTES_WRITTEN + M_UART_NB(Matrox.UartNumber),
&lNumBytesTransferred);
MosPrintf(MIL_TEXT("\n\nImage received through COM port\n\n"));
MbufControl(MilReceivedImage, M_MODIFIED, M_DEFAULT);
MosPrintf(MIL_TEXT("The program will now send data from your com port to your ")
MIL_TEXT("Matrox board.\n"));
MosPrintf(MIL_TEXT("%d bytes will be sent, this may take some time.\n"), SourceImageSize);
MosPrintf(MIL_TEXT("Press <Enter> to send data.\n"));
MosGetch();
MbufClear(MilReceivedImage, M_COLOR_BLACK);
MsysControl(MilSystem, M_UART_STRING_DELIMITER + M_UART_NB(Matrox.UartNumber), M_DEFAULT);
MsysControl(MilSystem, M_UART_READ_STRING_MAXIMUM_SIZE + M_UART_NB(Matrox.UartNumber),
SourceImageSize);
MsysControl(MilSystem, M_UART_READ_STRING_SIZE + M_UART_NB(Matrox.UartNumber),
SourceImageSize);
MsysControl(MilSystem, M_UART_READ_STRING + M_UART_NB(Matrox.UartNumber),
M_PTR_TO_DOUBLE(ReceiveBuffer));
WriteToComPort(hCom, SourceBuffer, SourceImageSize, &Overlapped);
lNumBytesTransferred = 0;
MsysInquire(MilSystem, M_UART_BYTES_READ + M_UART_NB(Matrox.UartNumber),
&lNumBytesTransferred);
MosPrintf(MIL_TEXT("\n\nImage received through MIL Uart: \n\n"));
MbufControl(MilReceivedImage, M_MODIFIED, M_DEFAULT);
MosPrintf(MIL_TEXT("The program will now send data from your COM port to your ")
MIL_TEXT("Matrox board\n"));
MosPrintf(MIL_TEXT("and read the data through a MIL hook function.\n"));
MosPrintf(MIL_TEXT("%d bytes will be sent, this may take some time.\n"), SourceImageSize);
MosPrintf(MIL_TEXT("Press <Enter> to send data.\n"));
MosGetch();
MbufClear(MilReceivedImage, M_COLOR_BLACK);
UserStruct.SystemId = MilSystem;
UserStruct.ReceiveBufferId = MilReceivedImage;
UserStruct.ReceiveBuffer = ReceiveBuffer;
UserStruct.ReadSize = SourceImageSize;
UserStruct.ReadPosition = 0;
UserStruct.Matrox = &Matrox;
MsysHookFunction(MilSystem, M_UART_DATA_RECEIVED + M_UART_NB(Matrox.UartNumber), ReadHook,
(void *)(&UserStruct));
ResetEvent(Overlapped.hEvent);
WriteFile(hCom, SourceBuffer, (DWORD)SourceImageSize, &SentSize, &Overlapped);
while(UserStruct.ReadPosition < SourceImageSize)
{
DoSomething = DoSomething;
MosSleep(1);
}
GetOverlappedResult(hCom, &Overlapped, &SentSize, TRUE);
MosPrintf(MIL_TEXT("\n\nFinished reading data from hook function.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
MsysHookFunction(MilSystem, M_UART_DATA_RECEIVED + M_UNHOOK + M_UART_NB(Matrox.UartNumber),
ReadHook, (void *)(&UserStruct));
CloseHandle(hCom);
CloseHandle(Overlapped.hEvent);
MbufFree(MilReceivedImage);
MbufFree(MilSourceImage);
MdispFree(MilDisplaySource);
MdispFree(MilDisplayReceive);
MsysFree(MilSystem);
MappFree(MilApplication);
return 0;
}
void InitializeMatroxUart(MIL_ID *MilSystem, SystemInfo *SysInfo, MIL_INT *UartInterfaceType)
{
SystemInfo *Matrox = (SystemInfo *) SysInfo;
MsysControl(*MilSystem,
M_UART_PARITY + M_UART_NB(Matrox->UartNumber), CONFIG_PARITY );
MsysControl(*MilSystem,
M_UART_SPEED + M_UART_NB(Matrox->UartNumber), CONFIG_SPEED );
MsysControl(*MilSystem,
M_UART_DATA_SIZE + M_UART_NB(Matrox->UartNumber), CONFIG_DATA_LENGTH);
MsysControl(*MilSystem,
M_UART_STOP_BITS + M_UART_NB(Matrox->UartNumber), CONFIG_STOP_BITS );
MsysInquire(*MilSystem,
M_UART_INTERFACE_TYPE + M_UART_NB(Matrox->UartNumber), UartInterfaceType);
}
MIL_INT InitializeComPort(HANDLE *hCom, MIL_INT InterfaceType, SystemInfo *SysInfo)
{
SECURITY_ATTRIBUTES SecAttr;
COMMTIMEOUTS Timeouts;
DCB dcb;
SecAttr.lpSecurityDescriptor = NULL;
SecAttr.bInheritHandle = TRUE;
SecAttr.nLength = sizeof(SecAttr);
*hCom = CreateFile(SysInfo->ComPortName[0],
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
&SecAttr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if(*hCom == INVALID_HANDLE_VALUE)
{
MosPrintf(MIL_TEXT(" Unable to open com port.\n"));
return M_FALSE;
}
if(!GetCommState(*hCom, &dcb))
{
MosPrintf(MIL_TEXT(" Unable to get com port state.\n"));
return M_FALSE;
}
if(!GetCommTimeouts(*hCom, &Timeouts))
{
MosPrintf(MIL_TEXT(" Unable to get com port timeouts.\n"));
return M_FALSE;
}
dcb.BaudRate = CONFIG_SPEED;
dcb.ByteSize = CONFIG_DATA_LENGTH;
if(CONFIG_STOP_BITS == 1)
dcb.StopBits = ONESTOPBIT;
else if(CONFIG_STOP_BITS == 2)
dcb.StopBits = TWOSTOPBITS;
if(CONFIG_PARITY == M_DISABLE)
dcb.Parity = NOPARITY;
else if (CONFIG_PARITY == M_EVEN)
dcb.Parity = EVENPARITY;
else if (CONFIG_PARITY == M_ODD)
dcb.Parity = ODDPARITY;
if(InterfaceType == M_RS232)
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
else if(InterfaceType == M_RS485)
dcb.fRtsControl = RTS_CONTROL_TOGGLE;
if (!SetCommState(*hCom, &dcb))
{
MosPrintf(MIL_TEXT("Unable to set com port to desired configuration.\n"));
return M_FALSE;
}
Timeouts.ReadIntervalTimeout = 50;
Timeouts.ReadTotalTimeoutConstant = 0;
Timeouts.ReadTotalTimeoutMultiplier = 0;
Timeouts.WriteTotalTimeoutConstant = 0;
Timeouts.WriteTotalTimeoutMultiplier = 50;
if(!SetCommTimeouts(*hCom, &Timeouts))
{
MosPrintf(MIL_TEXT("Unable to set com port timeouts.\n"));
return M_FALSE;
}
return M_TRUE;
}
void ReadFromComPort(HANDLE hCom, MIL_INT8 *Buffer, MIL_UINT SizeToRead,
OVERLAPPED *Overlapped)
{
MIL_UINT32 ReadSize = 0;
MIL_UINT32 TransferSize = 0;
ResetEvent(Overlapped->hEvent);
while(TransferSize < SizeToRead)
{
ReadFile(hCom, Buffer+TransferSize, 1024, &ReadSize, Overlapped);
GetOverlappedResult(hCom, Overlapped, &ReadSize, TRUE);
TransferSize += ReadSize;
MosPrintf(MIL_TEXT("%d bytes read. (%.2f%% completed.)\r"),
TransferSize, (float)TransferSize/(float)SizeToRead * 100.0);
}
}
void WriteToComPort(HANDLE hCom, MIL_INT8 *Buffer, MIL_UINT SizeToWrite,
OVERLAPPED *Overlapped)
{
MIL_UINT32 WriteSize = 0;
MIL_UINT32 TransferSize = 0;
MIL_UINT32 TransactionSize = 1024;
ResetEvent(Overlapped->hEvent);
while(TransferSize < SizeToWrite)
{
WriteFile(hCom, Buffer+TransferSize, TransactionSize, &WriteSize, Overlapped);
GetOverlappedResult(hCom, Overlapped, &WriteSize, TRUE);
TransferSize += WriteSize;
MosPrintf(MIL_TEXT("%d bytes written. (%.2f%% completed.)\r"),
TransferSize, (float)TransferSize/(float)SizeToWrite * 100.0);
if(SizeToWrite - TransferSize < TransactionSize)
TransactionSize = (MIL_UINT32)(SizeToWrite - TransferSize);
}
}
MIL_INT MFTYPE ReadHook(MIL_INT HookType, MIL_ID EventId, void* UserStructPtr)
{
UartHook *Params = (UartHook*) UserStructPtr;
MIL_INT Pending = 0;
MIL_UINT lNumBytesTransferred = 0;
MsysInquire(Params->SystemId, M_UART_DATA_PENDING + M_UART_NB(Params->Matrox->UartNumber),
&Pending);
while(Pending)
{
MsysControl(Params->SystemId,
M_UART_READ_STRING_SIZE + M_UART_NB(Params->Matrox->UartNumber), Pending);
MsysControl(Params->SystemId, M_UART_READ_STRING + M_UART_NB(Params->Matrox->UartNumber),
M_PTR_TO_DOUBLE(Params->ReceiveBuffer + Params->ReadPosition));
lNumBytesTransferred = 0;
MsysInquire(Params->SystemId, M_UART_BYTES_READ + M_UART_NB(Params->Matrox->UartNumber),
&lNumBytesTransferred);
MbufControl(Params->ReceiveBufferId, M_MODIFIED, M_DEFAULT);
Params->ReadPosition += lNumBytesTransferred;
MosPrintf(MIL_TEXT("%d bytes read. (%.2f%% completed.)\r"), Params->ReadPosition,
(float)Params->ReadPosition / (float)Params->ReadSize * 100.0);
MsysInquire(Params->SystemId,
M_UART_DATA_PENDING + M_UART_NB(Params->Matrox->UartNumber), &Pending);
}
return 0;
}
bool EnumerateComPorts( SystemInfo *SysInfo )
{
SystemInfo *Matrox = (SystemInfo *) SysInfo;
MIL_ID MilApplication,
MilSystem;
MIL_INT ComPortNumber = 0,
MaxPortNumber = 0,
MinPortNumber = MAX_PORTS,
MatroxDevice[MAX_PORTS],
MatroxUartNumber[MAX_PORTS];
int SystemType,
DeviceNumber,
UartNumber,
NumberOfPortsFound = 0,
PortNumberExists[MAX_PORTS] = {0},
EntryCount = 0,
nIndex[2],
UserChoice = 0,
MatroxPortIndex = -1,
WindowsPortIndex = -1,
SelectedMatroxPort = -1,
SelectedWindowsPort = -1;
const HKEY MAIN_KEY = HKEY_LOCAL_MACHINE;
const MIL_TEXT_CHAR SUB_KEY[30] = MIL_TEXT("HARDWARE\\DEVICEMAP\\SERIALCOMM");
const REGSAM KEY_PERMISSIONS = KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE;
FILETIME DummyFileTime;
DWORD DummyLength = MAX_PATH;
HKEY CurKey;
MIL_TEXT_CHAR EntryName[MAX_PORTS][MAX_PATH] = {MIL_TEXT("")};
MIL_TEXT_CHAR EntryData[MAX_PORTS][MAX_PATH] = {MIL_TEXT("")};
MIL_TEXT_CHAR TempSignalFormat[1][MAX_PATH] = {MIL_TEXT("")};
MIL_TEXT_CHAR MilSystemNames[4][25] = {{M_SYSTEM_MORPHIS},
{M_SYSTEM_SOLIOS},
{M_SYSTEM_RADIENT}};
MIL_TEXT_CHAR SystemNames[4][25] = {{MIL_TEXT("Matrox Morphis")},
{MIL_TEXT("Matrox Solios")},
{MIL_TEXT("Matrox Radient")}};
MIL_TEXT_CHAR MatroxPorts[MAX_PORTS][MAX_PATH];
MIL_TEXT_CHAR WindowsPorts[MAX_PORTS][MAX_PATH] = {{0}};
MIL_TEXT_CHAR MatroxSignalFormat[MAX_PORTS][MAX_PATH];
MIL_TEXT_CHAR MatroxBoardName[MAX_PORTS][MAX_PATH];
MIL_TEXT_CHAR MatroxSystem[MAX_PORTS][MAX_PATH];
MappAlloc(M_NULL, M_DEFAULT, &MilApplication);
MappControl(M_DEFAULT, M_ERROR, M_PRINT_DISABLE);
MosPrintf(MIL_TEXT("Enumerating Matrox COM ports.\n"));
MosPrintf(MIL_TEXT("-----------------------------------------------------\n"));
for(SystemType=0; SystemType<4; SystemType++)
{
for(DeviceNumber=0; DeviceNumber<M_DEV16; DeviceNumber++)
{
MsysAlloc(M_DEFAULT, MilSystemNames[SystemType], M_DEV0+DeviceNumber, M_DEFAULT, &MilSystem);
if(MilSystem)
{
MIL_INT BoardType;
MIL_TEXT_CHAR BoardName[1][50];
MsysInquire(MilSystem, M_BOARD_TYPE, &BoardType);
switch(BoardType&M_BOARD_TYPE_MASK)
{
case M_SOLIOS:
case M_RADIENT:
if ((BoardType & M_XCL) == M_XCL)
{
MosStrcpy(TempSignalFormat[0], MAX_PATH, MIL_TEXT("LVDS"));
MosSprintf(BoardName[0], 50, MIL_TEXT("%s/XCL"), SystemNames[SystemType]);
if ((BoardType & M_SFCL) == M_SFCL)
MosStrcat(BoardName[0], 50, MIL_TEXT(" Full"));
else if ((BoardType & M_DBCL) == M_DBCL)
MosStrcat(BoardName[0], 50, MIL_TEXT(" Dual Base"));
else if ((BoardType & M_SMCL ) == M_SMCL)
MosStrcat(BoardName[0], 50, MIL_TEXT(" Medium"));
}
else if ((MsysInquire(MilSystem, M_BOARD_TYPE, M_NULL) & M_XA) == M_XA)
{
MosStrcpy(TempSignalFormat[0], MAX_PATH, MIL_TEXT("RS232"));
MosSprintf(BoardName[0], 50, MIL_TEXT("%s/XA"), SystemNames[SystemType]);
if ((BoardType & M_QA) == M_QA)
MosStrcat(BoardName[0], 50, MIL_TEXT(" Quad"));
else if ((BoardType & M_DA) == M_DA)
MosStrcat(BoardName[0], 50, MIL_TEXT(" Dual"));
else if ((BoardType & M_SA) == M_SA)
MosStrcat(BoardName[0], 50, MIL_TEXT(" Single"));
}
break;
case M_MORPHIS:
MosStrcpy(TempSignalFormat[0], MAX_PATH, MIL_TEXT("RS485"));
MosSprintf(BoardName[0], 50, MIL_TEXT("%s"), SystemNames[SystemType]);
if ((BoardType & M_2VD) == M_2VD)
MosStrcat(BoardName[0], 50, MIL_TEXT(" 2VD (Dual)"));
else if ((BoardType & M_4VD) == M_4VD)
MosStrcat(BoardName[0], 50, MIL_TEXT(" 4VD (Quad)"));
default:
break;
}
for(UartNumber=0; UartNumber<4; UartNumber++)
{
ComPortNumber = 0;
MsysInquire(MilSystem, M_COM_PORT_NUMBER+M_UART_NB(UartNumber), &ComPortNumber);
if(ComPortNumber)
{
MIL_TEXT_CHAR ComPort[50];
PortNumberExists[NumberOfPortsFound++] = (int)ComPortNumber;
MosPrintf(MIL_TEXT("%2d) "), MatroxPortIndex + 2);
MosSprintf(ComPort, 50, MIL_TEXT("COM%ld"), ComPortNumber);
MatroxPortIndex++;
MatroxDevice[MatroxPortIndex] = DeviceNumber;
MatroxUartNumber[MatroxPortIndex] = UartNumber;
MosStrcpy(MatroxSystem[MatroxPortIndex], MAX_PATH,
MilSystemNames[SystemType]);
MosStrcpy(MatroxSignalFormat[MatroxPortIndex], MAX_PATH,
TempSignalFormat[0]);
MosStrcpy(MatroxPorts[MatroxPortIndex], MAX_PATH, ComPort);
MosStrcpy(MatroxBoardName[MatroxPortIndex], MAX_PATH, BoardName[0]);
MosPrintf(MIL_TEXT("%s\t: DEV%ld, UART%d = %s\n"),
BoardName[0], DeviceNumber, UartNumber, ComPort);
if (ComPortNumber > MaxPortNumber)
MaxPortNumber = ComPortNumber;
if (ComPortNumber < MinPortNumber)
MinPortNumber = ComPortNumber;
}
else
break;
}
MsysFree(MilSystem);
MilSystem = M_NULL;
}
else
break;
}
}
MosPrintf(MIL_TEXT("\n"));
MappControl(M_DEFAULT, M_ERROR, M_PRINT_ENABLE);
MappFree(MilApplication);
MosPrintf(MIL_TEXT("Enumerating Windows COM ports.\n"));
MosPrintf(MIL_TEXT("-------------------------------\n"));
RegOpenKeyEx(MAIN_KEY, SUB_KEY, 0, KEY_PERMISSIONS, &CurKey);
while (RegEnumValue(CurKey,
EntryCount,
EntryName[EntryCount],
&DummyLength,
NULL,
NULL,
NULL,
(MIL_UINT32 *) &DummyFileTime ) != ERROR_NO_MORE_ITEMS)
{
EntryCount++;
DummyLength = MAX_PATH;
}
for (nIndex[0] = 0; nIndex[0] < EntryCount; nIndex[0]++)
{
int nEntryExists = 0;
DummyLength = MAX_PATH * sizeof(MIL_TEXT_CHAR);
RegQueryValueEx(CurKey,
EntryName[nIndex[0]],
NULL,
NULL,
(LPBYTE)EntryData[nIndex[0]],
&DummyLength);
for (nIndex[1] = 0; nIndex[1] <= (int) MatroxPortIndex; nIndex[1]++)
{
if (MosStrcmp(EntryData[nIndex[0]], MatroxPorts[nIndex[1]]) == 0)
nEntryExists = 1;
}
if (!nEntryExists)
{
WindowsPortIndex++;
MosStrcpy(WindowsPorts[WindowsPortIndex], MAX_PATH, EntryData[nIndex[0]]);
MosPrintf(MIL_TEXT("%2d) "), MatroxPortIndex + WindowsPortIndex + 2);
MosPrintf(MIL_TEXT("%s "), EntryName[nIndex[0]]);
MosPrintf(MIL_TEXT(" \t= %s\n"), EntryData[nIndex[0]]);
}
}
MosPrintf(MIL_TEXT("\n"));
RegCloseKey(CurKey);
MosPrintf(MIL_TEXT("Please specify the Matrox COM Port index to use.\n"));
char UserInput[32];
do
{
MosPrintf(MIL_TEXT("Valid entries are from 1 to %d, (Q)uit: "), (MatroxPortIndex + 1));
scanf("%s", UserInput);
sscanf(UserInput, "%d", &UserChoice);
if (UserChoice >= 1 && UserChoice <= (int)MatroxPortIndex + 1)
{
MosPrintf(MIL_TEXT("\n%d) %s, DEV%d, %s selected.\n"),
UserChoice,
MatroxBoardName[UserChoice - 1],
MatroxDevice[UserChoice - 1],
MatroxPorts[UserChoice - 1]);
SelectedMatroxPort = UserChoice - 1;
}
else
{
char inputChar;
sscanf(UserInput, "%c", &inputChar);
if(inputChar == 'q' ||
inputChar == 'Q' )
{
UserChoice = -1;
}
else
{
MosPrintf(MIL_TEXT("Invalid selection. "));
UserChoice = 0;
}
}
}while (UserChoice == 0);
if(UserChoice != -1)
{
MosPrintf(MIL_TEXT("\nPlease specify the other COM Port index to use.\n"));
UserChoice = 0;
char UserInput[32];
do
{
MosPrintf(MIL_TEXT("Valid entries are from 1 to %d, (Q)uit: "),
(MatroxPortIndex + 1) + (WindowsPortIndex + 1));
scanf("%s", UserInput);
sscanf(UserInput, "%d", &UserChoice);
if ((UserChoice >= 1) &&
(UserChoice <= (int)MatroxPortIndex + 1 + (int)WindowsPortIndex + 1))
{
if ((UserChoice - 1) > (int)MatroxPortIndex)
MosPrintf(MIL_TEXT("\n%d) Windows %s selected.\n"),
UserChoice,
WindowsPorts[(UserChoice - 1) - (MatroxPortIndex) - 1]);
else
MosPrintf(MIL_TEXT("\n%d) %s, DEV%d, %s selected.\n"),
UserChoice,
MatroxBoardName[UserChoice - 1],
MatroxDevice[UserChoice - 1],
MatroxPorts[UserChoice - 1]);
if (UserChoice == (int)SelectedMatroxPort + 1)
{
MosPrintf(MIL_TEXT("\nYou can not select the same port twice. Select a different ")
MIL_TEXT("COM port.\n"));
UserChoice = 0;
}
else
{
MIL_TEXT_CHAR TempSignalFormat[1][MAX_PATH];
if ((UserChoice - 1) > (int)MatroxPortIndex)
MosStrcpy(TempSignalFormat[0], MAX_PATH, MIL_TEXT("RS232"));
else
MosStrcpy(TempSignalFormat[0], MAX_PATH, MatroxSignalFormat[UserChoice - 1]);
if (MosStrcmp(TempSignalFormat[0], MatroxSignalFormat[SelectedMatroxPort]) != 0)
{
MosPrintf(MIL_TEXT("\nIncompatible formats. \n"));
if ((UserChoice - 1) > (int)MatroxPortIndex)
MosPrintf(MIL_TEXT("The Windows %s uses the RS232 format.\nThe %s uses the ")
MIL_TEXT("%s format. \n"),
WindowsPorts[(UserChoice - 1) - (MatroxPortIndex) - 1],
MatroxBoardName[SelectedMatroxPort],
MatroxSignalFormat[SelectedMatroxPort]);
else
MosPrintf(MIL_TEXT("The %s uses the %s format.\n")
MIL_TEXT("The %s uses the %s format. \n"),
MatroxBoardName[UserChoice - 1],
MatroxSignalFormat[UserChoice - 1],
MatroxBoardName[SelectedMatroxPort],
MatroxSignalFormat[SelectedMatroxPort]);
MosPrintf(MIL_TEXT("Select a compatible board.\n\n"));
UserChoice = 0;
}
else
{
if ((UserChoice - 1) > (int)MatroxPortIndex)
{
SelectedWindowsPort = (UserChoice - 1) - (MatroxPortIndex) - 1;
MosSprintf(Matrox->ComPortName[0], MAX_PATH, MIL_TEXT("\\\\.\\%s"),
WindowsPorts[SelectedWindowsPort]);
}
else
{
SelectedWindowsPort = (UserChoice - 1) + (MatroxPortIndex);
MosSprintf(Matrox->ComPortName[0], MAX_PATH, MIL_TEXT("\\\\.\\%s"),
MatroxPorts[SelectedWindowsPort - MatroxPortIndex]);
}
}
}
}
else
{
char inputChar;
sscanf(UserInput, "%c", &inputChar);
if(inputChar == 'q' ||
inputChar == 'Q' )
{
UserChoice = -1;
}
else
{
MosPrintf(MIL_TEXT("Invalid selection. "));
UserChoice = 0;
}
}
}while (UserChoice == 0);
if(UserChoice != -1)
{
MosPrintf(MIL_TEXT("\n"));
MosStrcpy(Matrox->System[0], MAX_PATH, MatroxSystem[SelectedMatroxPort]);
Matrox->Device = MatroxDevice[SelectedMatroxPort];
Matrox->UartNumber = MatroxUartNumber[SelectedMatroxPort];
}
}
if(UserChoice == -1)
return false;
else
return true;
}