#include "StdAfx.h"
#include "Device.h"
#include "Device.h"

std::map<int, CDevice*> g_dmap;
CDevice::CDevice()
{
}

CDevice::~CDevice()
{
	m_obj.CloseSerialPort();
}

bool CDevice::Open()
{
	return m_obj.OpenSerialPort(m_iPort, m_iBaudrate, m_iDatabit, m_iParitybit, m_iStopbit, 0, 0);
}

void CDevice::Close()
{
	m_obj.CloseSerialPort();
}

std::string CDevice::SendCommond(std::string cmd)
{
	if ( !m_obj.IsOpen() )
		return std::string("串口未打开成功");

	DWORD dwRet = m_obj.WriteComm((byte*)cmd.data(), cmd.size());
	if ( dwRet != cmd.size() )
		return std::string("写串口失败");

	byte szData[1024 * 4] = {0};
	dwRet = m_obj.ReadComm(szData, 1024 * 4);
	if (dwRet == 0 )
		return std::string("读串口失败");

	return std::string((char*)szData, dwRet);
}
