commit | author | age
|
d6b4a7
|
1 |
/********************************************************************************* |
G |
2 |
* Copyright: (C) 2023 LingYun IoT System Studio. |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* Filename: comport.c |
|
6 |
* Description: This file is linux comport common API functions |
|
7 |
* |
|
8 |
* Version: 1.0.0(11/08/23) |
|
9 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
10 |
* ChangeLog: 1, Release initial version on "11/08/23 16:18:43" |
|
11 |
* |
|
12 |
********************************************************************************/ |
|
13 |
|
|
14 |
#ifndef _COMPORT_H_ |
|
15 |
#define _COMPORT_H_ |
|
16 |
|
|
17 |
#include <stdio.h> |
|
18 |
#include <stdlib.h> |
|
19 |
#include <unistd.h> |
|
20 |
#include <string.h> |
|
21 |
#include <getopt.h> |
|
22 |
#include <fcntl.h> |
|
23 |
#include <errno.h> |
|
24 |
#include <termios.h> |
|
25 |
#include <sys/stat.h> |
|
26 |
#include <sys/wait.h> |
|
27 |
#include <sys/types.h> |
|
28 |
#include <sys/stat.h> |
|
29 |
#include <sys/select.h> |
|
30 |
|
|
31 |
#define CONFIG_DEF_FRAGSIZE 128 |
|
32 |
typedef struct comport_s |
|
33 |
{ |
|
34 |
char devname[32]; |
|
35 |
unsigned char databit, parity, stopbit, flowctrl; |
|
36 |
long baudrate; |
|
37 |
|
|
38 |
int fd; |
|
39 |
int fragsize; /* frag size when do large data send */ |
|
40 |
} comport_t; |
|
41 |
|
|
42 |
|
|
43 |
/* |
|
44 |
* description: Open the comport and returned by $comport |
|
45 |
* |
|
46 |
* input args: $comport: corresponding comport handler |
|
47 |
* $devname: The comport device name path, such as '/dev/ttyS3' |
|
48 |
* $baudrate: The baudrate, such as 115200 |
|
49 |
* $settings: The databit,parity,stopbit,flowctrl settings, such as '8N1N' |
|
50 |
* |
|
51 |
* return value: The comport opened file description, <0 means failure |
|
52 |
*/ |
|
53 |
extern int comport_open(comport_t *comport, const char *devname, long baudrate, const char *settings); |
|
54 |
|
|
55 |
/* |
|
56 |
* description: close comport |
|
57 |
* input args: $comport: corresponding comport handler |
|
58 |
*/ |
|
59 |
extern void comport_close(comport_t *comport); |
|
60 |
|
|
61 |
/* |
|
62 |
* description: write $bytes $data to $comport |
|
63 |
* return value: 0: write ok <0: write failure |
|
64 |
*/ |
|
65 |
extern int comport_send(comport_t *comport, char *data, int data_bytes); |
|
66 |
|
|
67 |
/* |
|
68 |
* description: read data from $comport in $timeout <ms> to $buf no more than $buf_size bytes |
|
69 |
* return value: the actual read data bytes, <0: read failure |
|
70 |
*/ |
|
71 |
#define TIMEOUT_NONE 0 |
|
72 |
extern int comport_recv(comport_t *comport, char *buf, int buf_size, unsigned long timeout); |
|
73 |
|
|
74 |
#endif |