#include "stdafx.h" #include "serialparams.h" #include "debug.h" #include "StringFunctions.h" int DatabitsSpecToInt(TCHAR spec) { return spec-'0'; } TCHAR DatabitsSpecFromInt(int value) { return value+'0'; } int StopbitsSpecToInt(TCHAR spec) { switch(spec) { case '1': return ONESTOPBIT; case '2': return TWOSTOPBITS; default: debug("invalid stopbits spec %02x\n", spec); return ONESTOPBIT; } } TCHAR StopbitsSpecFromInt(int stopbits) { switch(stopbits) { case ONESTOPBIT: return '1'; case TWOSTOPBITS:return '2'; default: debug("invalid stopbits value %02x\n", stopbits); return '1'; } } int ParitySpecToInt(TCHAR spec) { switch(tolower(spec)) { case 'e': return EVENPARITY; case 'm': return MARKPARITY; case 'n': return NOPARITY; case 'o': return ODDPARITY; case 's': return SPACEPARITY; default: debug("invalid parity spec %02x\n", spec); return NOPARITY; } } TCHAR ParitySpecFromInt(int parity) { switch(parity) { case EVENPARITY: return 'e'; case MARKPARITY: return 'm'; case NOPARITY: return 'n'; case ODDPARITY: return 'o'; case SPACEPARITY: return 's'; default: debug("invalid parity value %02x\n", parity); return 'N'; } } void SerialParams::SetSpeed(DCB& dcb, CString &baud) { dcb.BaudRate= CStringFunctions::parseInteger(baud); } void SerialParams::SetBits(DCB& dcb, CString &bits) { dcb.ByteSize= DatabitsSpecToInt(bits.GetAt(0)); dcb.Parity= ParitySpecToInt(bits.GetAt(1)); dcb.StopBits= StopbitsSpecToInt(bits.GetAt(2)); } void SerialParams::DumpState(DCB& dcb) { debug("Current settings: %d %c%c%c\n", dcb.BaudRate, DatabitsSpecFromInt(dcb.ByteSize), ParitySpecFromInt(dcb.Parity), StopbitsSpecFromInt(dcb.StopBits)); } CStringArray *SerialParams::GetDeviceList() { TCHAR *driversActivePath= L"Drivers\\Active"; HKEY hActiveKey; LONG res= RegOpenKeyEx(HKEY_LOCAL_MACHINE, driversActivePath, 0, 0, &hActiveKey); if (ERROR_SUCCESS != res) { error(res, "failed to open registrykey %ls\n", driversActivePath); return NULL; } CStringArray *list= new CStringArray(); // todo: figure out how to find what devices are bluetooth serial ports // for now just adding the most common ones list->Add(_T("COM5:")); list->Add(_T("COM6:")); list->Add(_T("COM7:")); for (int iKey=0 ; ; iKey++) { TCHAR deviceNumber[256]; DWORD nrlen= 256; FILETIME tLast; res= RegEnumKeyEx(hActiveKey, iKey, deviceNumber, &nrlen, NULL, NULL, NULL, &tLast); if (ERROR_SUCCESS != res) break; HKEY hDeviceKey; res= RegOpenKeyEx(hActiveKey, deviceNumber, 0, 0, &hDeviceKey); if (ERROR_SUCCESS != res) { error(res, "failed to open registry key %ls\\%ls\n", driversActivePath, deviceNumber); return NULL; } TCHAR deviceName[256]; DWORD namelen= 256; res= RegQueryValueEx(hDeviceKey, L"Name", NULL, NULL, (BYTE*)deviceName, &namelen); if (ERROR_SUCCESS != res) { error(res, "failed to read registry key %ls\\%ls value Name\n", driversActivePath, deviceNumber); RegCloseKey(hDeviceKey); continue; } RegCloseKey(hDeviceKey); debug("found device: %ls\n", deviceName); list->Add(deviceName); } RegCloseKey(hActiveKey); CStringFunctions::StringArraySort(list); return list; } CStringArray *SerialParams::GetSpeedList() { CStringArray *list= new CStringArray(); list->Add(L"2400"); list->Add(L"9600"); list->Add(L"19200"); list->Add(L"38400"); list->Add(L"115200"); return list; } CStringArray *SerialParams::GetBitsList() { CStringArray *list= new CStringArray(); list->Add("8n1"); list->Add("7e1"); return list; } CString SerialParams::GetDefaultSpeed() { return L"115200"; } CString SerialParams::GetDefaultBits() { return L"8n1"; } CString SerialParams::GetDefaultPort() { return L"COM1:"; }