【发布时间】:2015-04-28 22:22:26
【问题描述】:
我正在处理一个项目,我必须处理多个传感器,并将错误值打印到文本文件中。目前我正在做随机生成器,只是生成一些虚拟值。这里的问题是相同的错误被存储到文本文件中,因为代码运行得很快并且是循环的。所以我正在考虑制作一个计时器来处理当前传感器的最后存储数据。我不确定这是否是个好主意,因为这听起来并不容易,因为最终我将不得不处理大约 30 个不同的传感器。知道如何在我的代码中实现这样的逻辑吗?
struct arg_struct{ //store the these data into the textfile
char *sensorname;
double sensorvalue;
int faultcode;
};
//insert the data into the textfile with the arguments from detect_error
void *write_sens_error(void *arguments){
struct arg_struct *args = arguments;
struct tm *local;
time_t t;
t = time(NULL);
local = localtime(&t);
FILE *fp;
fp = fopen("/home/sal/Desktop/sensors.txt","a");
fprintf(fp, "%s %s %f %s %d %s %s", args -> sensorname, "\t",args -> sensorvalue, "\t", args -> faultcode,"\t",asctime(local));
fclose(fp);
pthread_exit(NULL);
return NULL;
}
//create a new thread, so the second thread will handle the errorvalue storage
void detect_error(char *sensorname, double sensorvalue, int faultcode){
pthread_t thread_error1;
struct arg_struct args;
args.sensorname = malloc(7);
strcpy(args.sensorname, sensorname);
args.sensorvalue = sensorvalue;
args.faultcode = faultcode;
pthread_create(&thread_error1, NULL, &write_sens_error, (void *)&args);
pthread_join(thread_error1, NULL);
free(args.sensorname);
}
//generate random values and check for error
void rand_temperature_sensor(double EXTS[8], int FTS, int CTS, int OTS){
int i;
for (i = 0; i < 8; i++){
EXTS[i] = rand() % 10 + 820;
if(EXTS[i]>800){
char name[6];
sprintf(name, "EXTS%d", i+1);
detect_error(name,EXTS[i],((EXTS[i]>850) ? 2 : 1));
}
}
printf("%s,%f,%f,%f,%f,%f,%f,%f,%f\n","Temp",EXTS[0],EXTS[1],EXTS[2],EXTS[3],EXTS[4],EXTS[5],EXTS[6],EXTS[7];
}
这是此代码生成的日志的 sn-p。从日志中可以看出,相同的传感器在一秒钟内存储了 3 次。这就是我想要避免的,因此传感器的错误仅在上一次错误捕获后 5 秒后存储。
EXTS0 823.000000 1 Tue Apr 28 23:43:57 2015
EXTS1 820.000000 1 Tue Apr 28 23:43:57 2015
EXTS2 826.000000 1 Tue Apr 28 23:43:57 2015
EXTS3 827.000000 1 Tue Apr 28 23:43:57 2015
EXTS4 829.000000 1 Tue Apr 28 23:43:57 2015
EXTS5 826.000000 1 Tue Apr 28 23:43:57 2015
EXTS6 820.000000 1 Tue Apr 28 23:43:57 2015
EXTS7 823.000000 1 Tue Apr 28 23:43:57 2015
EXTS0 827.000000 1 Tue Apr 28 23:43:57 2015
EXTS1 828.000000 1 Tue Apr 28 23:43:57 2015
EXTS2 822.000000 1 Tue Apr 28 23:43:57 2015
EXTS3 826.000000 1 Tue Apr 28 23:43:57 2015
EXTS4 822.000000 1 Tue Apr 28 23:43:57 2015
EXTS5 822.000000 1 Tue Apr 28 23:43:57 2015
EXTS6 829.000000 1 Tue Apr 28 23:43:57 2015
EXTS7 826.000000 1 Tue Apr 28 23:43:57 2015
EXTS0 821.000000 1 Tue Apr 28 23:43:57 2015
EXTS1 823.000000 1 Tue Apr 28 23:43:57 2015
EXTS2 822.000000 1 Tue Apr 28 23:43:57 2015
EXTS3 824.000000 1 Tue Apr 28 23:43:57 2015
EXTS4 826.000000 1 Tue Apr 28 23:43:57 2015
EXTS5 826.000000 1 Tue Apr 28 23:43:57 2015
EXTS6 821.000000 1 Tue Apr 28 23:43:57 2015
EXTS7 824.000000 1 Tue Apr 28 23:43:57 2015
我真的不希望这个问题令人困惑。如果我必须清除某些内容,请告诉我。完整代码粘贴在here。
【问题讨论】:
-
只需为每个错误添加时间戳。无论是在生成错误时还是在您将其写入文件时。如果自上次记录错误以来的时间小于您的阈值时间,则传感器发出下一个错误时,只需删除错误,否则生成/存储错误。
-
你想避免什么? ... 我不明白!我在您的 main 中看到您每 600 微秒(每秒 1666 次)调用一次函数 rand_temperature_sensor() ...
-
如果等待线程结束,为什么要调用函数write_sens_error()作为线程呢?
-
In
detect_error():pthread_create(&thread_error1, NULL, &write_sens_error, (void *)&args); pthread_join(thread_error1, NULL);不会产生大量线程级并行性,是吗? -
@SergioFormiggini:伙计,我可以阅读。我在指出多余的线程是多么无用。并去掉感叹号!!!!!!!!!!!!!!!!!!!!!1111!!!!
标签: c timer pthreads delay multitasking