用c 写一个每隔1秒调用/usr/bin/sn_core.elf getstat 2>/dev/null指令 指令打印的内容是Status:WR mode : WRC_SLAVE_WR1 wr0 -> lnk:0 rx:0 tx:7141 lock:1 wr1 -> lnk:1 rx:2350 tx:678 lock:1 syncs:wr1 sv:1 ss:'TRACK_PHASE' aux:0 sec:721 nsec:492689488 mu:865675 dms:430067 dtxm:240682 drxm:187022 dtxs:241345 drxs:182145 asym:5541 crtt:14481 cko:0 setp:4107 hd:57674 md:33225 ad:65000 ucnt:619 temp: 45.437 C Time: Thu, Jan 1, 1970, 00:12:01 +965544976 nanoseconds. 然后将其中的temp mu dms crtt time的值存入数组 该数组中包含7个子数组 子数组最大拥有50个值 当第51个值输入时 将第一个值舍弃 以此类推
时间: 2023-09-10 13:14:06 浏览: 133
好的,以下是你需要的C代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#define MAX_VALUES 50
#define INTERVAL 1
int main() {
char command[] = "/usr/bin/sn_core.elf getstat 2>/dev/null";
char buffer[1024];
double temp_values[MAX_VALUES], mu_values[MAX_VALUES], dms_values[MAX_VALUES], crtt_values[MAX_VALUES];
time_t time_values[MAX_VALUES];
int temp_index = 0, mu_index = 0, dms_index = 0, crtt_index = 0, time_index = 0;
int i;
while (1) {
FILE *fp = popen(command, "r");
if (fp == NULL) {
perror("Failed to execute command");
exit(1);
}
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
if (strstr(buffer, "temp:") != NULL) {
double temp;
sscanf(buffer, "%*[^:]: %lf", &temp);
temp_values[temp_index % MAX_VALUES] = temp;
temp_index++;
} else if (strstr(buffer, "mu:") != NULL) {
double mu;
sscanf(buffer, "%*[^:]: %lf", &mu);
mu_values[mu_index % MAX_VALUES] = mu;
mu_index++;
} else if (strstr(buffer, "dms:") != NULL) {
double dms;
sscanf(buffer, "%*[^:]: %lf", &dms);
dms_values[dms_index % MAX_VALUES] = dms;
dms_index++;
} else if (strstr(buffer, "crtt:") != NULL) {
double crtt;
sscanf(buffer, "%*[^:]: %lf", &crtt);
crtt_values[crtt_index % MAX_VALUES] = crtt;
crtt_index++;
} else if (strstr(buffer, "Time:") != NULL) {
struct tm tm;
time_t time;
char tz[10];
sscanf(buffer, "%*[^:]: %*s %*s %*s %d:%d:%d %s %d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec, tz, &tm.tm_year);
tm.tm_year -= 1900;
tm.tm_mon = 0;
tm.tm_mday = 1;
time = mktime(&tm);
time_values[time_index % MAX_VALUES] = time;
time_index++;
}
}
pclose(fp);
printf("Temp values: ");
for (i = 0; i < (temp_index > MAX_VALUES ? MAX_VALUES : temp_index); i++) {
printf("%lf ", temp_values[i]);
}
printf("\n");
printf("Mu values: ");
for (i = 0; i < (mu_index > MAX_VALUES ? MAX_VALUES : mu_index); i++) {
printf("%lf ", mu_values[i]);
}
printf("\n");
printf("Dms values: ");
for (i = 0; i < (dms_index > MAX_VALUES ? MAX_VALUES : dms_index); i++) {
printf("%lf ", dms_values[i]);
}
printf("\n");
printf("Crtt values: ");
for (i = 0; i < (crtt_index > MAX_VALUES ? MAX_VALUES : crtt_index); i++) {
printf("%lf ", crtt_values[i]);
}
printf("\n");
printf("Time values: ");
for (i = 0; i < (time_index > MAX_VALUES ? MAX_VALUES : time_index); i++) {
printf("%s", ctime(&time_values[i]));
}
printf("\n");
sleep(INTERVAL);
}
return 0;
}
```
这段代码使用了 popen 函数来执行指令,并从输出中解析出相应的值。然后,我们将这些值存入相应的数组中,每个数组最多有50个值。当第51个值输入时,将第一个值舍弃,以此类推。我们可以使用模运算来实现这个循环缓冲区。
注意:在这个例子中,我们只是将值打印到控制台上,你需要根据你的需求修改代码,将这些值存储到文件中或者发送到服务器等等。
阅读全文