SerialList.h
/*
* Copyright (c) 2013-15, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <vector>
#include <cstring>
class SerialDevice
{
public:
SerialDevice()
: m_port(_T("")){};
SerialDevice(LPCTSTR port)
: m_port(port){};
virtual ~SerialDevice(){};
virtual CString GetString() { return (m_port); };
virtual uint32_t GetNum() { return _ttoi(m_port.Mid(3)); };
private:
CString m_port;
};
class SerialList
{
public:
SerialList(){};
virtual ~SerialList(){};
SerialDevice GetDevice(int index) { return m_serialDevices.at(index); };
size_t GetDeviceCount() { return m_serialDevices.size(); };
int FindSerialDevice(SerialDevice device)
{
std::vector<SerialDevice>::iterator it;
for (it = m_serialDevices.begin(); it != m_serialDevices.end(); it++)
{
// Check next index.
if (device.GetNum() > it->GetNum())
{
continue;
}
// The same COM device in the list, return the index
else if (device.GetNum() == it->GetNum())
{
return it - m_serialDevices.begin();
}
// No matched, return negative.
else
{
return -1;
}
}
return -1;
}
void ScanSerialDevices();
void SortSerialDevices();
private:
std::vector<SerialDevice> m_serialDevices;
};
extern SerialList *g_pAllComDevices;
SerialList
SerialList class contains all serial devices on host.
Definition: SerialList.h:63
SerialDevice
SerialDevice class represent a COM port hardware on host.
Definition: SerialList.h:38
SerialList::SerialList
SerialList()
Generated standard constructor.
Definition: SerialList.h:67
SerialList::GetDeviceCount
size_t GetDeviceCount()
Get the number of devices in the list.
Definition: SerialList.h:75
SerialList::~SerialList
virtual ~SerialList()
Generated standard destructor.
Definition: SerialList.h:70
SerialList::GetDevice
SerialDevice GetDevice(int index)
Get SerialDevice variable at specified index.
Definition: SerialList.h:73
SerialList::FindSerialDevice
int FindSerialDevice(SerialDevice device)
Find whether the specified device is in the list.
Definition: SerialList.h:77
SerialDevice::~SerialDevice
virtual ~SerialDevice()
Generated standard destructor.
Definition: SerialList.h:50
SerialDevice::SerialDevice
SerialDevice(LPCTSTR port)
Generated standard constructor with a parameter.
Definition: SerialList.h:46
SerialDevice::SerialDevice
SerialDevice()
Generated standard constructor.
Definition: SerialList.h:42
SerialDevice::GetString
virtual CString GetString()
Get the string containing COM port.(COMxx)
Definition: SerialList.h:53
SerialDevice::GetNum
virtual uint32_t GetNum()
Get the port number only.
Definition: SerialList.h:55