环形存储区
概念:当多个不同线程调用同一接口如串口时,会出现干涉情况。增加环形存储区后,会将不同线程的调用指令放入同一数组中轮询调用同一接口。
具体内码如下:
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);
}
}