[feat] first add demo of windows
This commit is contained in:
parent
42b77a3290
commit
787f8d20ad
419
windows/main.c
Normal file
419
windows/main.c
Normal file
@ -0,0 +1,419 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <windows.h>
|
||||
#include "../lark1s/lark1s.h"
|
||||
|
||||
#define MB_ADDRESS (7)
|
||||
#define MB_RESP_TIME (100)
|
||||
#define BUFFER_SIZE (1024)
|
||||
uint8_t buffer_w[BUFFER_SIZE];
|
||||
uint8_t buffer_r[BUFFER_SIZE];
|
||||
|
||||
void print_sn(HANDLE hComm, uint8_t *out, uint8_t *in)
|
||||
{
|
||||
uint8_t sn[16];
|
||||
int ret;
|
||||
DWORD actual_len;
|
||||
|
||||
ret = lark1s_req_sn(out);
|
||||
if (ret) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_sn failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_sn(in, actual_len, 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(HANDLE hComm, uint8_t *out, uint8_t *in)
|
||||
{
|
||||
struct lark1s_gas_info_s info;
|
||||
int ret;
|
||||
DWORD actual_len;
|
||||
|
||||
ret = lark1s_req_gas3_info(out);
|
||||
if (ret) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_gas3_info failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_gas3_info(in, actual_len, &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(HANDLE hComm, uint8_t *out, uint8_t *in)
|
||||
{
|
||||
struct lark1s_data_s data;
|
||||
int ret;
|
||||
DWORD actual_len;
|
||||
|
||||
ret = lark1s_req_data(out);
|
||||
if (ret) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_data failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_data(in, actual_len, &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(HANDLE hComm, uint8_t *out, uint8_t *in)
|
||||
{
|
||||
struct lark1s_status_s status;
|
||||
int ret;
|
||||
DWORD actual_len;
|
||||
|
||||
ret = lark1s_req_status(out);
|
||||
if (ret) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_status failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_status(in, actual_len, &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(HANDLE hComm, uint8_t *out, uint8_t *in)
|
||||
{
|
||||
struct lark1s_status_s status;
|
||||
int ret;
|
||||
DWORD actual_len;
|
||||
|
||||
/* zero record */
|
||||
ret = lark1s_req_gas3_cali_zero_record(out, LARK1S_CALI_ZERO);
|
||||
if (ret) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_gas3_cali_zero_record failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_gas3_cali_zero_record(in, actual_len);
|
||||
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) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_status failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_status(in, actual_len, &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) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_gas3_cali_zero_active failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_gas3_cali_active(in, actual_len);
|
||||
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) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_status failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_status(in, actual_len, &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(HANDLE hComm, uint8_t *out, uint8_t *in, int concentration)
|
||||
{
|
||||
struct lark1s_status_s status;
|
||||
int ret;
|
||||
DWORD actual_len;
|
||||
|
||||
/* span record */
|
||||
ret = lark1s_req_gas3_cali_span_record(out, concentration);
|
||||
if (ret) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_gas3_cali_span_record failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_gas3_cali_span_record(in, actual_len);
|
||||
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) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_status failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_status(in, actual_len, &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) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_gas3_cali_span_active failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
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) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_status failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_status(in, actual_len, &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(HANDLE hComm, uint8_t *out, uint8_t *in)
|
||||
{
|
||||
struct lark1s_status_s status;
|
||||
int ret;
|
||||
DWORD actual_len;
|
||||
|
||||
ret = lark1s_req_gas3_cali_restore(out);
|
||||
if (ret) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_gas3_cali_restore failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
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) {
|
||||
WriteFile(hComm, out, ret, &actual_len, NULL);
|
||||
} else {
|
||||
printf("Error: lark1s_req_status failed\r\n");
|
||||
return;
|
||||
}
|
||||
ReadFile(hComm, in, BUFFER_SIZE, &actual_len, NULL);
|
||||
ret = lark1s_parse_status(in, actual_len, &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 sel;
|
||||
HANDLE hComm = CreateFile(
|
||||
"\\\\.\\COM3", /* serial port name */
|
||||
GENERIC_READ | GENERIC_WRITE, /* write & read privilege */
|
||||
0, /* share mode */
|
||||
NULL, /* secure attribute */
|
||||
OPEN_EXISTING, /* open current device */
|
||||
0, /* file attribute */
|
||||
NULL /* template file */
|
||||
);
|
||||
if (hComm == INVALID_HANDLE_VALUE) {
|
||||
/* open serial port failed */
|
||||
printf("Error: open serial port failed\r\n");
|
||||
return -1;
|
||||
} else {
|
||||
DCB dcb = { 0 };
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
if (GetCommState(hComm, &dcb)) {
|
||||
dcb.BaudRate = 115200;
|
||||
dcb.ByteSize = 8;
|
||||
dcb.Parity = NOPARITY;
|
||||
dcb.StopBits = ONESTOPBIT;
|
||||
if (!SetCommState(hComm, &dcb)) {
|
||||
printf("Error: Set serial port parameter failed\r\n");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
printf("Error: Get serial port parameter failed\r\n");
|
||||
return -1;
|
||||
}
|
||||
COMMTIMEOUTS timeouts = { 0 };
|
||||
timeouts.ReadIntervalTimeout = MB_RESP_TIME;
|
||||
timeouts.ReadTotalTimeoutConstant = MB_RESP_TIME;
|
||||
timeouts.ReadTotalTimeoutMultiplier = MB_RESP_TIME;
|
||||
SetCommTimeouts(hComm, &timeouts);
|
||||
printf("Succeed: open serial port\r\n");
|
||||
}
|
||||
|
||||
lark1s_set_mb_address(MB_ADDRESS);
|
||||
print_help();
|
||||
while (1) {
|
||||
scanf("%d", &sel);
|
||||
if (sel == 1) {
|
||||
print_sn(hComm, buffer_w, buffer_r);
|
||||
} else if (sel == 2) {
|
||||
print_gas3_info(hComm, buffer_w, buffer_r);
|
||||
} else if (sel == 3) {
|
||||
print_data(hComm, buffer_w, buffer_r);
|
||||
} else if (sel == 4) {
|
||||
print_status(hComm, buffer_w, buffer_r);
|
||||
} else if (sel == 5) {
|
||||
cali_restore(hComm, buffer_w, buffer_r);
|
||||
} else if (sel == 0) {
|
||||
cali_zero(hComm, buffer_w, buffer_r);
|
||||
} else if (sel > 0) {
|
||||
cali_span(hComm, buffer_w, buffer_r, sel);
|
||||
} else {
|
||||
|
||||
}
|
||||
printf("\r\n");
|
||||
print_help();
|
||||
FlushFileBuffers(hComm);
|
||||
}
|
||||
|
||||
CloseHandle(hComm);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user