guowenxue
2019-07-31 219a85586b96da0ae470a508967268e3aa6dd5a9
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
/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  cp_comport.h
 *    Description:  This head file is for the common TTY/Serial port operator library 
 *                   
 *        Version:  1.0.0(10/17/2011~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "10/17/2011 03:33:25 PM"
 *                     
 ********************************************************************************/
#ifndef  _CP_COMPORT_H
#define  _CP_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>
 
#define BUF_64  64
 
#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
 
//#define msleep(m)               {struct timespec cSleep; cSleep.tv_sec = 0; cSleep.tv_nsec = m * 1000; nanosleep(&cSleep, 0);}
 
typedef struct cp_comport_s
{
    unsigned char databit, parity, stopbit, flowctrl, connected;
    char dev_name[DEVNAME_LEN];
    unsigned char  used;     /* This comport used or not now */
    int fd;
    int frag_size;
    long baudrate;
} cp_comport_t;
 
cp_comport_t *comport_init(const char *dev_name, int baudrate, const char *settings);
void comport_close(cp_comport_t * comport);
int comport_open(cp_comport_t * comport);
void cp_comport_term(cp_comport_t * comport);
int comport_recv(cp_comport_t * comport, char *buf, int buf_size, unsigned long timeout);
int comport_send(cp_comport_t * comport, char *buf, int send_bytes);
 
void set_settings(cp_comport_t * comport, const char *settings);
void disp_settings(cp_comport_t * comport);
void nonblock();
int kbhit();
 
#endif