409 lines
11 KiB
C
409 lines
11 KiB
C
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <termios.h>
|
|
#include <string.h>
|
|
#include "../lark1s/lark1s.h"
|
|
|
|
#define MB_ADDRESS (7)
|
|
#define MB_RESP_TIME (1)
|
|
#define BUFFER_SIZE (1024)
|
|
uint8_t buffer_w[BUFFER_SIZE];
|
|
uint8_t buffer_r[BUFFER_SIZE];
|
|
|
|
void print_sn(int fd, uint8_t *out, uint8_t *in)
|
|
{
|
|
uint8_t sn[16];
|
|
int ret;
|
|
|
|
ret = lark1s_req_sn(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_sn failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_sn(in, ret, sn);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_sn failed, %d\r\n", ret);
|
|
return;
|
|
} else {
|
|
printf("SN: ");
|
|
for (int j = 0; j < 16; j++) {
|
|
if (sn[j] <= ' ') {
|
|
continue;
|
|
}
|
|
printf("%c", sn[j]);
|
|
}
|
|
printf("\r\n");
|
|
}
|
|
}
|
|
|
|
void print_gas3_info(int fd, uint8_t *out, uint8_t *in)
|
|
{
|
|
struct lark1s_gas_info_s info;
|
|
int ret;
|
|
|
|
ret = lark1s_req_gas3_info(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_gas3_info failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_gas3_info(in, ret, &info);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_gas_info failed, %d\r\n", ret);
|
|
return;
|
|
} else {
|
|
printf("gas3_info.gas_id: %d\r\n", info.gas_id);
|
|
printf("gas3_info.gas_name: ");
|
|
for (int j = 0; j < 12; j++) {
|
|
if (info.gas_name[j] <= ' ') {
|
|
continue;
|
|
}
|
|
printf("%c", info.gas_name[j]);
|
|
}
|
|
printf("\r\n");
|
|
printf("gas3_info.unit_id: %d\r\n", info.unit_id);
|
|
printf("gas3_info.unit_name: ");
|
|
for (int j = 0; j < 8; j++) {
|
|
if (info.unit_name[j] <= ' ') {
|
|
continue;
|
|
}
|
|
printf("%c", info.unit_name[j]);
|
|
}
|
|
printf("\r\n");
|
|
printf("gas3_info.range: %d\r\n", info.range);
|
|
printf("gas3_info.min_cali: %d\r\n", info.min_cali);
|
|
}
|
|
}
|
|
|
|
void print_data(int fd, uint8_t *out, uint8_t *in)
|
|
{
|
|
struct lark1s_data_s data;
|
|
int ret;
|
|
|
|
ret = lark1s_req_data(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_data failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_data(in, ret, &data);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_data failed, %d\r\n", ret);
|
|
return;
|
|
} else {
|
|
printf("data.det_temp: %d\r\n", data.det_temp);
|
|
printf("data.air_pressure: %d\r\n", data.air_pressure);
|
|
printf("data.gas3_reading: %d\r\n", data.gas3_reading);
|
|
printf("data.cts_ref: %d\r\n", data.cts_ref);
|
|
printf("data.cts_gas3: %d\r\n", data.cts_gas3);
|
|
printf("data.gas3_compensated_reading: %d\r\n", data.gas3_compensated_reading);
|
|
}
|
|
}
|
|
|
|
void print_status(int fd, uint8_t *out, uint8_t *in)
|
|
{
|
|
struct lark1s_status_s status;
|
|
int ret;
|
|
|
|
ret = lark1s_req_status(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_status failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_status(in, ret, &status);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_status failed, %d\r\n", ret);
|
|
return;
|
|
} else {
|
|
printf("status.zero_record: %d\r\n", status.zero_record[2]);
|
|
printf("status.span_record: %d\r\n", status.span_record[2]);
|
|
printf("status.active: %d\r\n", status.active);
|
|
printf("status.restore: %d\r\n", status.restore);
|
|
}
|
|
}
|
|
|
|
void cali_zero(int fd, uint8_t *out, uint8_t *in)
|
|
{
|
|
struct lark1s_status_s status;
|
|
int ret;
|
|
|
|
/* zero record */
|
|
ret = lark1s_req_gas3_cali_zero_record(out, LARK1S_CALI_ZERO);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_gas3_cali_zero_record failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_gas3_cali_zero_record(in, ret);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_gas3_cali_zero_record failed, %d\r\n", ret);
|
|
return;
|
|
}
|
|
/* get zero record status */
|
|
ret = lark1s_req_status(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_status failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_status(in, ret, &status);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_status failed, %d\r\n", ret);
|
|
return;
|
|
} else {
|
|
if (0 == status.zero_record[2]) {
|
|
printf("cali zero record success\r\n");
|
|
} else {
|
|
printf("cali zero record failed, ret = %d\r\n", status.zero_record[2]);
|
|
return;
|
|
}
|
|
}
|
|
/* zero active */
|
|
ret = lark1s_req_gas3_cali_active(out, LARK1S_CALI_ZERO);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_gas3_cali_zero_active failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_gas3_cali_active(in, ret);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_gas3_cali_zero_active failed, %d\r\n", ret);
|
|
return;
|
|
}
|
|
/* get zero active status */
|
|
ret = lark1s_req_status(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_status failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_status(in, ret, &status);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_status failed, %d\r\n", ret);
|
|
return;
|
|
} else {
|
|
if (0 == status.active) {
|
|
printf("cali zero active success\r\n");
|
|
} else {
|
|
printf("cali zero active failed, ret = %d\r\n", status.active);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void cali_span(int fd, uint8_t *out, uint8_t *in, int concentration)
|
|
{
|
|
struct lark1s_status_s status;
|
|
int ret;
|
|
|
|
/* span record */
|
|
ret = lark1s_req_gas3_cali_span_record(out, concentration);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_gas3_cali_span_record failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_gas3_cali_span_record(in, ret);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_gas3_cali_span_record failed, %d\r\n", ret);
|
|
return;
|
|
}
|
|
/* get span record status */
|
|
ret = lark1s_req_status(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_status failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_status(in, ret, &status);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_status failed, %d\r\n", ret);
|
|
return;
|
|
} else {
|
|
if (0 == status.span_record[2]) {
|
|
printf("cali span record success\r\n");
|
|
} else {
|
|
printf("cali span record failed, ret = %d\r\n", status.span_record[2]);
|
|
return;
|
|
}
|
|
}
|
|
/* span active */
|
|
ret = lark1s_req_gas3_cali_active(out, LARK1S_CALI_SPAN);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_gas3_cali_span_active failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_gas3_cali_active(in, ret);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_gas3_cali_span_active failed, %d\r\n", ret);
|
|
return;
|
|
}
|
|
/* get span active status */
|
|
ret = lark1s_req_status(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_status failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_status(in, ret, &status);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_status failed, %d\r\n", ret);
|
|
return;
|
|
} else {
|
|
if (0 == status.active) {
|
|
printf("cali span active success\r\n");
|
|
} else {
|
|
printf("cali span active failed, ret = %d\r\n", status.active);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void cali_restore(int fd, uint8_t *out, uint8_t *in)
|
|
{
|
|
struct lark1s_status_s status;
|
|
int ret;
|
|
|
|
ret = lark1s_req_gas3_cali_restore(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_gas3_cali_restore failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_gas3_cali_restore(in, ret);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_gas3_cali_restore failed, %d\r\n", ret);
|
|
return;
|
|
}
|
|
/* get restore status */
|
|
ret = lark1s_req_status(out);
|
|
if (ret) {
|
|
write(fd, out, ret);
|
|
} else {
|
|
printf("Error: lark1s_req_status failed\r\n");
|
|
return;
|
|
}
|
|
sleep(MB_RESP_TIME);
|
|
ret = read(fd, in, BUFFER_SIZE);
|
|
ret = lark1s_parse_status(in, ret, &status);
|
|
if (ret) {
|
|
printf("Error: lark1s_parse_status failed, %d\r\n", ret);
|
|
return;
|
|
} else {
|
|
if (0 == status.active) {
|
|
printf("cali restore success\r\n");
|
|
} else {
|
|
printf("cali restore failed, ret = %d\r\n", status.active);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void print_help(void)
|
|
{
|
|
printf("Usage:\r\n");
|
|
printf("1: print sn\r\n");
|
|
printf("2: print gas3 info\r\n");
|
|
printf("3: print data\r\n");
|
|
printf("4: print status\r\n");
|
|
printf("5: cali restore\r\n");
|
|
printf("0: cali zero\r\n");
|
|
printf("other number: cali span\r\n");
|
|
printf("\r\n");
|
|
}
|
|
|
|
int main(void) {
|
|
int fd;
|
|
int sel;
|
|
struct termios options;
|
|
|
|
fd = open("/dev/ttyS3", O_RDWR | O_NOCTTY | O_NDELAY);
|
|
if (fd == -1) {
|
|
perror("can not open serial port");
|
|
return -1;
|
|
}
|
|
|
|
/* config serial port */
|
|
tcgetattr(fd, &options);
|
|
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
|
|
options.c_iflag = IGNPAR;
|
|
options.c_oflag = 0;
|
|
options.c_lflag = 0;
|
|
tcflush(fd, TCIFLUSH);
|
|
tcsetattr(fd, TCSANOW, &options);
|
|
|
|
lark1s_set_mb_address(MB_ADDRESS);
|
|
print_help();
|
|
while (1) {
|
|
scanf("%d", &sel);
|
|
if (sel == 1) {
|
|
print_sn(fd, buffer_w, buffer_r);
|
|
} else if (sel == 2) {
|
|
print_gas3_info(fd, buffer_w, buffer_r);
|
|
} else if (sel == 3) {
|
|
print_data(fd, buffer_w, buffer_r);
|
|
} else if (sel == 4) {
|
|
print_status(fd, buffer_w, buffer_r);
|
|
} else if (sel == 5) {
|
|
cali_restore(fd, buffer_w, buffer_r);
|
|
} else if (sel == 0) {
|
|
cali_zero(fd, buffer_w, buffer_r);
|
|
} else if (sel > 0) {
|
|
cali_span(fd, buffer_w, buffer_r, sel);
|
|
} else {
|
|
|
|
}
|
|
printf("\r\n");
|
|
print_help();
|
|
fflush(stdout);
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
} |