guowenxue
2022-04-08 c7a69ad99ba1500722b3eb3dd8777893574a4be1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*********************************************************************************
 *      Copyright:  (C) 2020 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  comport.h
 *    Description:  This head file is for the common TTY/Serial port operator library 
 *                   
 *        Version:  1.0.0(06/29/2020~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "06/29/2020 03:33:25 PM"
 *                     
 ********************************************************************************/
#ifndef  __COMPORT_H_
#define  __COMPORT_H_
 
#include  <stdio.h>
#include  <stdlib.h>
#include  <unistd.h>
#include  <string.h>
#include  <getopt.h>
#include  <fcntl.h>
#include  <errno.h>
#include  <termios.h>
#include  <sys/stat.h>
#include  <sys/wait.h>
#include  <sys/types.h>
#include  <sys/stat.h>
#include  <sys/select.h>
 
#ifndef DEVNAME_LEN
#define DEVNAME_LEN          64
#endif
 
//#define COM_DEBUG
#ifdef  COM_DEBUG
#define COM_PRINT(format,args...) printf(format, ##args)
#else
#define COM_PRINT(format,args...) do{} while(0);
#endif
 
typedef struct comport_s
{
    int            fd;
    char           dev_name[DEVNAME_LEN];
 
    long           baudrate;
    unsigned char  databit;
    unsigned char  parity; 
    unsigned char  stopbit;
    unsigned char  flowctrl;
 
    int            frag_size;  
} comport_t;
 
 
/*+-----------------------------------------------------------------------------------------+
 *  description: Initialise the comport structure
 *
 *   input args: 
 *                $comport:  The comport work context pointer
 *               $dev_name:  The comport device name path, such as '/dev/ttyS3'
 *               $baudrate:  The baudrate, such as 115200
 *               $settings:  The databit,parity,stopbit,flowctrl settings, such as '8N1N'
 *
 * return value: 0: Successfully      <0: Failure
 *+-----------------------------------------------------------------------------------------+*/
int comport_init(comport_t *comport, const char *dev_name, int baudrate, const char *settings);
 
 
 
/*+-----------------------------------------------------------------------------------------+
 *  description: display current comport settings such as databit,parity,stopbit,flowctrl
 *   input args: $comport:  corresponding comport point 
 *+-----------------------------------------------------------------------------------------+*/
#ifdef COM_DEBUG
extern void disp_settings(comport_t * comport);
#endif
 
 
 
/*+-----------------------------------------------------------------------------------------+
 *  description: Open the comport specified by $comport
 *   input args: $comport: comport work context
 * return value: >0: The comport opened file description, <0: Failure
 *+-----------------------------------------------------------------------------------------+*/
extern int comport_open(comport_t * comport);
 
 
 
/*+-----------------------------------------------------------------------------------------+
 *  description: close comport 
 *   input args: $comport:  corresponding comport point 
 *+-----------------------------------------------------------------------------------------+*/
extern void comport_close(comport_t * comport);
 
 
 
/*+-----------------------------------------------------------------------------------------+
 *  description: write $send_bytes bytes data from $buf to $comport
 * return value: 0: write ok  <0: write failure
 *+-----------------------------------------------------------------------------------------+*/
extern int  comport_send(comport_t * comport, char *buf, int send_bytes);
 
 
 
/*+-----------------------------------------------------------------------------------------+
 *  description: read data from $comport in $timeout <ms> to $buf, no more than $buf_size bytes
 * return value: the actual read data bytes, <0: read failure
 *+-----------------------------------------------------------------------------------------+*/
extern int  comport_recv(comport_t * comport, char *buf, int buf_size, unsigned long timeout);
 
 
 
#endif