环形存储区
環形存儲區
?
概念:當多個不同線程調用同一接口如串口時,會出現干涉情況。增加環形存儲區后,會將不同線程的調用指令放入同一數組中輪詢調用同一接口。
?
?
具體內碼如下:
?
typedef?struct?
{?
????uint16_t?send_cmd[50];??????????//發送指令
????uint16_t?send_para[50];?????????//發送內容?
????uint8_t?send_add_in_number;?????//數組遞增序列號,遞增到50后變為0????
????uint8_t?send_sub_out_number;????//數組遞減序列號,遞增到50后變為0??
????uint8_t?send_times;?????????????//表示數組里,待發送的數量??
}modbus_huiling_t;
extern?modbus_huiling_t?modbus_huiling;?
?
//將調用指令增加到環形數組?
int?add_modbus_cmd(uint8_t?huiling_cmd,?uint16_t?para_time)?
{
????modbus_huiling.send_times++;?
????modbus_huiling.send_cmd[modbus_huiling.send_add_in_number]?=?huiling_cmd;?
????modbus_huiling.send_para[modbus_huiling.send_add_in_number]?=?para_time;??
?
????log_d("send_times?=?%d?\r\n",?modbus_huiling.send_times);??
????log_d("send_add_in_number?=?%d\r\n",?modbus_huiling.send_add_in_number);?
?
????if(modbus_huiling.send_add_in_number?>=?49)?????
????????modbus_huiling.send_add_in_number?=?0;
????else?
????????modbus_huiling.send_add_in_number++;
????return?0;?
}?
?
//環形數組遞減,串口完成后調用此接口
static?int?sub_modbus_cmd(void)???
{
????modbus_huiling.send_times?--;?
????if(modbus_huiling.send_sub_out_number?>=49)??
????????modbus_huiling.send_sub_out_number?=?0;
????else
????????modbus_huiling.send_sub_out_number?++;?
}
?
static?void?task_weight(void?const?*arg)?
{?
????uint16_t?em_temp?=?0;
????uint8_t?send_out_number?=?0;?
????uint32_t?current_value?=?0;?
?
????memset(&modbus_huiling,?0,?sizeof(modbus_huiling)?);
????ms_master_init();
????need_read_weight?=?1;
?
????current_value?=?STEP_CAL_INIT;
????handle_write_app_register(¤t_value,?1,?CMS_JAW_CAL_STEP_STATIC);?
????add_modbus_cmd(MODULE_WEIGHT_CAL_JAW_OFF,?0);? //增加退出果醬校準模式指令
?
????get_cur_time(&Last_time);
?
????while?(1)?
????{?
????????//?get_temp_adc_value(&em_temp);??
????????dur_time?=?cal_timeoff_us(&Last_time);?
?
????????if(?(dur_time?>=?1000000)?&&?(need_read_weight?==?1)?)??//1s?
????????{
????????????add_modbus_cmd(MODULE_WEIGHT_READ,?0);??/增加果醬重量檢測指令
????????????get_cur_time(&Last_time);?
????????}
????????
????????if(modbus_huiling.send_times?!=?0)
????????{
????????????log_d("send_times?=?%d?\r\n",?modbus_huiling.send_times);?
????????????log_d("send_sub_out_number?=?%d?\r\n",?modbus_huiling.send_sub_out_number);?
????????????log_d("send_sub_out_cmd?=?%d?\r\n",?modbus_huiling.send_cmd[modbus_huiling.send_sub_out_number]);??
????????????send_out_number?=?modbus_huiling.send_sub_out_number;?
?
????????????switch?(modbus_huiling.send_cmd[send_out_number])?
????????????{
????????????case?MODULE_MOTOR_DIR_WRITE:
????????????{
????????????????motor_dir_write(&mb,?modbus_huiling.send_para[send_out_number]);?
????????????}
????????????????break;
????????????case?MODULE_MOTOR_DIR_READ:
????????????{
????????????????motor_dir_read(&mb,?&modbus_huiling.send_para[send_out_number]);?
????????????}
????????????????break;
????????????case?MODULE_MOTOR_DATA_READ:
????????????{
????????????????piece_motor_sensor_read(&mb,?&modbus_huiling.send_para[send_out_number]);?
????????????}
????????????????break;
????????????case?MODULE_MOTOR_JAW1_WRITE:?
????????????{
????????????????jaw_run_time_write(&mb,?JAW1,?modbus_huiling.send_para[send_out_number]);?
????????????}
????????????????break;
????????????case?MODULE_MOTOR_JAW1_READ:
????????????{
????????????????jaw_run_time_read(&mb,?JAW1,?&jaw1_remain_run_time);???
????????????}
????????????????break;
????????????case?MODULE_MOTOR_JAW2_WRITE:
????????????{
????????????????jaw_run_time_write(&mb,?JAW2,?modbus_huiling.send_para[send_out_number]);?
????????????}
????????????????break;
????????????case?MODULE_MOTOR_JAW2_READ:
????????????{
????????????????jaw_run_time_read(&mb,?JAW2,?&jaw2_remain_run_time);?
????????????}
????????????????break;
????????????case?MODULE_MOTOR_JAW3_WRITE:
????????????{
????????????????jaw_run_time_write(&mb,?JAW3,?modbus_huiling.send_para[send_out_number]);?
????????????}
????????????????break;
????????????case?MODULE_MOTOR_JAW3_READ:
????????????{
????????????????jaw_run_time_read(&mb,?JAW3,?&jaw3_remain_run_time);?
????????????}
????????????????break;
????????????case?MODULE_MOTOR_PIECE1_WRITE:
????????????{
????????????????fruit_piece_run_time_write(&mb,?FRUIT_PIECE1,?modbus_huiling.send_para[send_out_number]);
????????????}
????????????????break;
????????????case?MODULE_MOTOR_PIECE1_READ:
????????????{
????????????????fruit_piece_run_time_read(&mb,?FRUIT_PIECE1,?&piece1_remain_run_time);???
????????????}
????????????????break;
????????????case?MODULE_MOTOR_PIECE2_WRITE:
????????????{
????????????????fruit_piece_run_time_write(&mb,?FRUIT_PIECE2,?modbus_huiling.send_para[send_out_number]);
????????????}
????????????????break;
????????????case?MODULE_MOTOR_PIECE2_READ:
????????????{
????????????????fruit_piece_run_time_read(&mb,?FRUIT_PIECE2,?&piece2_remain_run_time);?
????????????}
????????????????break;
????????????case?MODULE_MOTOR_PIECE3_WRITE:?
????????????{
????????????????fruit_piece_run_time_write(&mb,?FRUIT_PIECE3,?modbus_huiling.send_para[send_out_number]);
????????????}
????????????????break;
????????????case?MODULE_MOTOR_PIECE3_READ:??
????????????{
????????????????fruit_piece_run_time_read(&mb,?FRUIT_PIECE3,?&piece3_remain_run_time);????
????????????}
????????????????break;
????????????
????????????//稱重?
????????????case?MODULE_WEIGHT_READ:?
????????????{???
????????????????weight_read_weight(&weight1_value,?&weight2_value,?&weight3_value);?
????????????}
????????????????break;
????????????case?MODULE_WEIGHT_CAL_JAW_ON:?
????????????{
????????????????
????????????????if(weight_enter_cal()?==?0)?
????????????????{?//進入校準模式?
????????????????????need_read_weight?=?0;?
????????????????????current_value?=?STEP_CAL_ENTER;???
????????????????????handle_write_app_register(¤t_value,?1,?CMS_JAW_CAL_STEP_STATIC);?
????????????????}
????????????}
????????????????break;
????????????case?MODULE_WEIGHT_CAL_JAW_OFF:?
????????????{
????????????????if(weight_exit_cal()?==?0)?
????????????????{?//退出校準模式?
????????????????????need_read_weight?=?1;
????????????????????current_value?=?STEP_CAL_EXIT;???
????????????????????handle_write_app_register(¤t_value,?1,?CMS_JAW_CAL_STEP_STATIC);?
????????????????}
????????????}
????????????????break;
????????????case?MODULE_WEIGHT_CAL_JAW_1_0KG:
????????????{
????????????????if(weight_cal_channel(Jaw1,?0)?==?0)?
????????????????{?//
????????????????????current_value?=?STEP_CAL_JAW1_0_success;???
????????????????}
????????????????else
????????????????{
????????????????????current_value?=?STEP_CAL_JAW1_0_fail;???
????????????????}
????????????????handle_write_app_register(¤t_value,?1,?CMS_JAW_CAL_STEP_STATIC);?
????????????}
????????????????break;
????????????case?MODULE_WEIGHT_CAL_JAW_1_1KG:
????????????{????
????????????????if(weight_cal_channel(Jaw1,?1)?==?0)?
????????????????{?//
????????????????????current_value?=?STEP_CAL_JAW1_1_success;????
????????????????}
????????????????else
????????????????{
????????????????????current_value?=?STEP_CAL_JAW1_1_fail;???
????????????????}?
????????????????handle_write_app_register(¤t_value,?1,?CMS_JAW_CAL_STEP_STATIC);???
????????????}
????????????????break;
????????????case?MODULE_WEIGHT_CAL_JAW_2_0KG:
????????????{
????????????????if(weight_cal_channel(Jaw2,?0)?==?0)?
????????????????{?//
????????????????????current_value?=?STEP_CAL_JAW2_0_success;????
????????????????}
????????????????else
????????????????{
????????????????????current_value?=?STEP_CAL_JAW2_0_fail;???
????????????????}
????????????????handle_write_app_register(¤t_value,?1,?CMS_JAW_CAL_STEP_STATIC);?
????????????}
????????????????break;
????????????case?MODULE_WEIGHT_CAL_JAW_2_1KG:
????????????{
????????????????if(weight_cal_channel(Jaw2,?1)?==?0)?
????????????????{?//
????????????????????current_value?=?STEP_CAL_JAW2_1_success;????
????????????????}
????????????????else
????????????????{
????????????????????current_value?=?STEP_CAL_JAW2_1_fail;???
????????????????}
?
????????????????handle_write_app_register(¤t_value,?1,?CMS_JAW_CAL_STEP_STATIC);?
????????????}
????????????????break;
????????????case?MODULE_WEIGHT_CAL_JAW_3_0KG:
????????????{
????????????????if(weight_cal_channel(Jaw3,?0)?==?0)?
????????????????{?//
????????????????????current_value?=?STEP_CAL_JAW3_0_success;????
????????????????}
????????????????else
????????????????{
????????????????????current_value?=?STEP_CAL_JAW3_0_fail;???
????????????????}
????????????????handle_write_app_register(¤t_value,?1,?CMS_JAW_CAL_STEP_STATIC);?
????????????}
????????????????break;
????????????case?MODULE_WEIGHT_CAL_JAW_3_1KG:
????????????{
????????????????if(weight_cal_channel(Jaw3,?1)?==?0)?
????????????????{?//
????????????????????current_value?=?STEP_CAL_JAW3_1_success;????
????????????????}
????????????????else
????????????????{
????????????????????current_value?=?STEP_CAL_JAW3_1_fail;???
????????????????}
????????????????handle_write_app_register(¤t_value,?1,?CMS_JAW_CAL_STEP_STATIC);?
????????????}?
????????????????break;???
?
????????????default:
????????????????break;
????????????}
????????????
????????????sub_modbus_cmd();?
????????}???
????????osDelay(20);?
????}????
}?
?
?
?
總結
- 上一篇: 树莓派教程之树莓派系统镜像刷入和远程登陆
- 下一篇: mktime函数实现获取当前系统时间