Statistics
| Revision:

root / trunk / code / projects / fp_math / test / serial.c @ 1602

History | View | Annotate | Download (2.15 KB)

1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <unistd.h>
5
#include <fcntl.h>
6
#include <errno.h>
7
#include <termios.h>
8
#include <unistd.h>
9
#include "serial.h"
10

    
11
/* open and configure the port */
12
int openport(char* portname){
13
  struct termios oldtio, newtio;
14
  char *error = 0;
15
  int fd;
16

    
17
  /* open read/write, non-controlling, non-blocking */
18
  fd = open(portname, O_RDWR | O_NOCTTY | O_NONBLOCK);
19
  if (fd == -1){
20
    fprintf(stderr, "Unable to open %s: ", portname);
21
    perror(error);
22
    exit(-1);
23
  }
24

    
25
  tcgetattr(fd, &oldtio);          //save current port settings
26
  bzero(&newtio, sizeof(newtio));  //clear struct for new settings
27

    
28
  newtio.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
29
  newtio.c_iflag = IGNPAR | ICRNL;
30

    
31
  newtio.c_oflag = 0;
32
  newtio.c_lflag = 0;
33

    
34
  newtio.c_cc[VINTR]    = 0;     /* Ctrl-c */
35
  newtio.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
36
  newtio.c_cc[VERASE]   = 0;     /* del */
37
  newtio.c_cc[VKILL]    = 0;     /* @ */
38
  newtio.c_cc[VEOF]     = 4;     /* Ctrl-d */
39
  newtio.c_cc[VTIME]    = 0;     /* inter-character timer unused */
40
  newtio.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives 
41
*/
42
  newtio.c_cc[VSWTC]    = 0;     /* '\0' */
43
  newtio.c_cc[VSTART]   = 0;     /* Ctrl-q */
44
  newtio.c_cc[VSTOP]    = 0;     /* Ctrl-s */
45
  newtio.c_cc[VSUSP]    = 0;     /* Ctrl-z */
46
  newtio.c_cc[VEOL]     = 0;     /* '\0' */
47
  newtio.c_cc[VREPRINT] = 0;     /* Ctrl-r */
48
  newtio.c_cc[VDISCARD] = 0;     /* Ctrl-u */
49
  newtio.c_cc[VWERASE]  = 0;     /* Ctrl-w */
50
  newtio.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
51
  newtio.c_cc[VEOL2]    = 0;     /* '\0' */
52

    
53
  /* flush the port, and apply the new settings now */
54
  tcflush(fd, TCIFLUSH);
55
  tcsetattr(fd, TCSANOW, &newtio);
56

    
57
  return (fd);
58
}
59

    
60
int putbyte (int fd, char c)
61
{
62
   int result;
63
   result= write(fd, &c, 1);
64
//   Sleep(25);
65
   return result;
66
}
67

    
68
unsigned char getbyte (int fd)
69
{
70
  char c;
71
  ssize_t retval;
72

    
73
  retval = read(fd, &c, 1); 
74

    
75
  if (retval <= 0)
76
    return 255;
77

    
78
  return c;
79
}
80

    
81
unsigned char getbyteblock (int fd)
82
{
83
  char c;
84
  ssize_t retval;
85

    
86
  while ((retval = read(fd, &c, 1)) <= 0){}
87
//    usleep(5000);
88

    
89
  return c;
90
}
91

    
92
void clearbuf(int fd)
93
{
94
  tcflush(fd, TCIFLUSH);
95
}
96