lark1s_demo/linux/main.c

133 lines
3.4 KiB
C
Raw Normal View History

2024-05-26 14:54:40 +08:00
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
2024-06-01 16:18:56 +08:00
#include "../lark1s/lark1s.h"
2024-05-26 14:54:40 +08:00
2024-06-01 16:18:56 +08:00
#define MB_ADDRESS 9
#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");
}
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);
} else {
printf("SN: ");
for (int j = 0; j < 16; j++) {
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");
}
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);
} else {
printf("gas3_info.gas_id: %d\r\n", info.gas_id);
printf("gas3_info.gas_name: ");
for (int j = 0; j < 12; j++) {
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++) {
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");
}
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);
} 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);
}
}
int main(void) {
2024-05-26 14:54:40 +08:00
int fd;
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);
2024-06-01 16:18:56 +08:00
lark1s_set_mb_address(MB_ADDRESS);
2024-05-26 14:54:40 +08:00
while (1) {
2024-06-01 16:18:56 +08:00
print_sn(fd, buffer_w, buffer_r);
print_gas3_info(fd, buffer_w, buffer_r);
print_data(fd, buffer_w, buffer_r);
2024-05-26 14:54:40 +08:00
printf("\r\n");
fflush(stdout);
}
close(fd);
return 0;
}