第一步:MCU把数据放到发送数组
第二步:MCU超时等待数组的数据发送完成
第三步:MCU超时等待接收DEVICE应答的数据
MCU的状态分为:
SEND_FREE:发送空闲
SEND_BUSY:正在发送数据
SEND_FAIL:发送失败(超时)
SEND_SUCCESS:发送成功
GET_BUSY:正在接收数据
GET_FAIL:接收失败(超时)
GET_SUCCESS:接收成功
unsigned char Normal_SendGet(unsigned char *status, unsigned long *time, unsigned char (*dataToBuf)(void), unsigned char (*bufToDevice)(void), unsigned char(*get)(void)) { ?? ?switch(*status) ?? ?{ ?? ??? ?case SEND_FREE:?? ??? ??? ??? ?//数据放到缓存区 ?? ??? ?{ ?? ??? ??? ?if( dataToBuf() ) ?? ??? ??? ??? ?*status = SEND_BUSY; ?? ??? ??? ?else ?? ??? ??? ??? ?*status = SEND_FAIL; ?? ??? ??? ??? ? ?? ??? ??? ?*time = time_ms; ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?case SEND_BUSY:?? ??? ??? ??? ?//等待数据发送完成 ?? ??? ?{ ?? ??? ??? ?if((time_ms-*time)<500) ?? ??? ??? ?{ ?? ??? ??? ??? ?if(bufToDevice()) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?*status = GET_BUSY; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?else ?? ??? ??? ?{ ?? ??? ??? ??? ?*status = SEND_FAIL; ?? ??? ??? ?}?? ? ?? ??? ??? ?*time = time_ms; ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?case SEND_FAIL:?? ??? ??? ?//失败 ?? ??? ?{ ?? ??? ??? ?if((time_ms-*time)>=500) ?? ??? ??? ?{ ?? ??? ??? ??? ?*status = SEND_FREE; ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?case GET_BUSY:?? ??? ??? ??? ?//等待接收数据 ?? ??? ?{ ?? ??? ??? ?if((time_ms-*time)<500) ?? ??? ??? ?{ ?? ??? ??? ??? ?if(get()) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?*status = SEND_FREE; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?else ?? ??? ??? ?{ ?? ??? ??? ??? ?*status = SEND_FREE; ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ? ?? ?return *status; }
|