ODU.h
#pragma once
#include <string>
using namespace std;
#define ODU_TYPE_331_FLAG "331"
#define ODU_TYPE_335_FLAG "335"
enum class ODU_TYPE {
ODU_TYPE_331,
ODU_TYPE_335,
ODU_TYPE_UNKNOWN
};
class ODU
{
public:
ODU();
virtual int getTxFre();//获取发射频率
virtual bool setTxFre(int);//设置发射频率
virtual int getRxFre();//获取接收频率
virtual bool setRxFre(int);//设置接受频率
virtual float getTxPower();//获取发射功率
virtual bool setTxPower(float);//设置发射功率
virtual float getRxL();//获取接收电平
virtual bool hearBeat();//心跳包
virtual string name();//获取该设备的名字
ODU_TYPE getODUType();//获取当前ODU的类型
protected:
int txFre;//发射频率
int rxFre;//接收频率
float txPower;//发射功率
float rxL;//接收电平
ODU_TYPE type;
};
ODU335.h
#pragma once
#include "ODU.h"
class ODU335:public ODU
{
public:
ODU335();
bool hearBeat();//心跳包
};
ODU.cpp
#include "ODU.h"
#include <iostream>
#include <string>
using namespace std;
//ODU类,用于处理老型号ODU331设备
ODU::ODU()
{
txFre = 34400;
rxFre = 31100;
txPower = 20;
rxL = 0;
type = ODU_TYPE::ODU_TYPE_331;
cout << "调用ODU()" << endl;
}
int ODU::getTxFre()
{
return txFre;
}
bool ODU::setTxFre(int frequence)
{
txFre = frequence;
cout << name() << "发射频率已经设置为" << txFre << "Hz" << endl;
return true;
}
int ODU::getRxFre()
{
return rxFre;
}
bool ODU::setRxFre(int frequence)
{
rxFre = frequence;
cout << name()<< "接收频率已经设置为" << txFre << "Hz" << endl;
return true;
}
float ODU::getTxPower()
{
return txPower;
}
bool ODU::setTxPower(float power)
{
rxFre = power;
cout << name() << "发射功率已经设置为" << txPower << "dBm" << endl;
return true;
}
float ODU::getRxL()
{
return rxL;
}
bool ODU::hearBeat()
{
cout << name()<< "模拟串口协议读取数据:获取心跳包的反馈...【"
<<ODU_TYPE_331_FLAG<<"】"
<< endl;
string response;
cin >> response;
bool ret = false;
if (response == ODU_TYPE_331_FLAG) {
type = ODU_TYPE::ODU_TYPE_331;
ret = true;
}
return ret;
}
string ODU::name()
{
string ret;
switch (type) {
case ODU_TYPE::ODU_TYPE_331:
ret = "ODU331";
break;
case ODU_TYPE::ODU_TYPE_335:
ret = "ODU335";
break;
case ODU_TYPE::ODU_TYPE_UNKNOWN:
default:
ret = "ODU_UNKNOWN";
break;
}
return ret;
}
ODU_TYPE ODU::getODUType()
{
return type;
}
ODU335.cpp
#include "ODU335.h"
#include<iostream>
using namespace std;
ODU335::ODU335()
{
cout << "调用ODU335.h" << endl;
type = ODU_TYPE::ODU_TYPE_331;
}
bool ODU335::hearBeat()
{
cout << name() << "模拟串口协议读取数据:获取心跳包的反馈...【"
<< ODU_TYPE_335_FLAG << "】"
<< endl;
string response;
cin >> response;
bool ret = false;
if (response == ODU_TYPE_335_FLAG) {
type = ODU_TYPE::ODU_TYPE_335;
ret = true;
}
return ret;
}
main.cpp
#include <iostream>
#include <Windows.h>
#include "ODU.h"
#include "ODU335.h"
#include <thread>
using namespace std;
void test(ODU* odu) {
odu->setTxFre(35588);
cout << odu->getTxFre() << endl;
odu->hearBeat();
}
ODU* odu = NULL;
void oduMonitorHandler() {
while (true) {
if (odu->hearBeat() == false) {
//切换ODU
ODU_TYPE type = odu->getODUType();
switch (type)
{
case ODU_TYPE::ODU_TYPE_331:
delete odu;
odu = new ODU335;
break;
case ODU_TYPE::ODU_TYPE_335:
delete odu;
odu = new ODU;
break;
default:
odu = NULL;
return;
}
}
Sleep(3000);
}
}
int main(void) {
odu = new ODU();
//创建一个线程,用于对ODU进行监测
thread oduMonitor(oduMonitorHandler);
//主线程等待线程oduMonitor的结束
oduMonitor.join();
system("pause");
return 0;
}