1. 在头文件中定义基类,再定义父类
- class SerialFactory
- {
- public:
- static SerialBase * creatRs485(void);
- static SerialBase * creatRs232(void);
- };
2.
- class SerialGdRs485 : public SerialBase
- {
- public:
- SerialGdRs485();
- virtual ~SerialGdRs485();
- virtual void close();
- virtual bool open(uint8_t);
- virtual void setParity(ParityType);
- virtual void setDataBits(DataBitsType);
- virtual void setStopBits(StopBitsType);
- virtual void setBaudRate(BaudRateType);
- virtual void setFlowControl(FlowType);
- virtual int readBlock(char *data, unsigned int maxlen);
- virtual int writeBlock(const char *data, unsigned int len);
- private:
- u8 serialPort;
- u8 parityType;
- u8 stopBits;
- u32 baudRates;
- };
当然这些驱动函数你需要在底层实现:
- // BSP_RS485_Init( bsp_rs485_port2 );
- // BSP_RS485_BaudRate_Set( bsp_rs485_port2, 115200);
- // BSP_RS485_Parity_Set( bsp_rs485_port2, BSP_UART_PAR_NONE );
- // BSP_RS485_StopBits_Set(bsp_rs485_port2, BSP_UART_STOP_1 );
- // BSP_RS485_Open( bsp_rs485_port2 );
然后在C文件中new一个对象,功能是在一个task中收发rs485数据:
- static uint8_t recvBuf[64] = {0};
- static SerialBase *pComm[2];
-
- /*!
- \brief 主板LED控制任务
- */
- void led_task(void *pvParameters)
- {
- uint8_t test[8] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x10, 0x44, 0x06};
-
- BSP_Led_init();
-
- pComm[0] = SerialFactory::creatRs485();
-
- pComm[0]->setBaudRate(BAUD9600);
- pComm[0]->setParity(PAR_NONE);
- /*数据位*/
- pComm[0]->setDataBits(DATA_8);
- pComm[0]->setStopBits(STOP_1); // 停止位
- pComm[0]->open(1);
- for(;;)
- {
- /* toggle LED */
- MainBoard_LED_TOOGLE();
- BSP_Feed_fwdgt();
- vTaskDelay(500);
-
- pComm[0]->writeBlock((const char *)test, 8);
- pComm[0]->readBlock((char *)recvBuf, 64 );
-
-
- }
- }
|