OpenCores
URL https://opencores.org/ocsvn/sata_controller_core/sata_controller_core/trunk

Subversion Repositories sata_controller_core

[/] [sata_controller_core/] [trunk/] [sata2_bus_v1_00_a/] [base_system/] [sata_test/] [simplecom.c] - Blame information for rev 11

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 ashwin_men
/*
2
 * Building: cc -o com com.c
3
 * Usage   : ./com /dev/device [speed]
4
 * Example : ./com /dev/ttyS0 [115200]
5
 * Keys    : Ctrl-A - exit, Ctrl-X - display control lines status
6
 * Darcs   : darcs get http://tinyserial.sf.net/
7
 * Homepage: http://tinyserial.sourceforge.net
8
 * Version : 2009-03-05
9
 *
10
 * Ivan Tikhonov, http://www.brokestream.com, kefeer@brokestream.com
11
 * Patches by Jim Kou, Henry Nestler, Jon Miner, Alan Horstmann
12
 *
13
 */
14
 
15
 
16
/* Copyright (C) 2007 Ivan Tikhonov
17
 
18
  This software is provided 'as-is', without any express or implied
19
  warranty.  In no event will the authors be held liable for any damages
20
  arising from the use of this software.
21
 
22
  Permission is granted to anyone to use this software for any purpose,
23
  including commercial applications, and to alter it and redistribute it
24
  freely, subject to the following restrictions:
25
 
26
  1. The origin of this software must not be misrepresented; you must not
27
     claim that you wrote the original software. If you use this software
28
     in a product, an acknowledgment in the product documentation would be
29
     appreciated but is not required.
30
  2. Altered source versions must be plainly marked as such, and must not be
31
     misrepresented as being the original software.
32
  3. This notice may not be removed or altered from any source distribution.
33
 
34
  Ivan Tikhonov, kefeer@brokestream.com
35
 
36
*/
37
 
38
#include <termios.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <string.h>
42
#include <unistd.h>
43
#include <sys/signal.h>
44
#include <sys/types.h>
45
#include <sys/ioctl.h>
46
#include <fcntl.h>
47
#include <errno.h>
48
 
49
int transfer_byte(int from, int to, int is_control);
50
 
51
typedef struct {char *name; int flag; } speed_spec;
52
 
53
 
54
void print_status(int fd) {
55
        int status;
56
        unsigned int arg;
57
        status = ioctl(fd, TIOCMGET, &arg);
58
        fprintf(stderr, "[STATUS]: ");
59
        if(arg & TIOCM_RTS) fprintf(stderr, "RTS ");
60
        if(arg & TIOCM_CTS) fprintf(stderr, "CTS ");
61
        if(arg & TIOCM_DSR) fprintf(stderr, "DSR ");
62
        if(arg & TIOCM_CAR) fprintf(stderr, "DCD ");
63
        if(arg & TIOCM_DTR) fprintf(stderr, "DTR ");
64
        if(arg & TIOCM_RNG) fprintf(stderr, "RI ");
65
        fprintf(stderr, "\r\n");
66
}
67
 
68
int main(int argc, char *argv[])
69
{
70
        int comfd;
71
        struct termios oldtio, newtio;       //place for old and new port settings for serial port
72
        struct termios oldkey, newkey;       //place tor old and new port settings for keyboard teletype
73
        char *devicename = argv[1];
74
        int need_exit = 0;
75
        speed_spec speeds[] =
76
        {
77
                {"1200", B1200},
78
                {"2400", B2400},
79
                {"4800", B4800},
80
                {"9600", B9600},
81
                {"19200", B19200},
82
                {"38400", B38400},
83
                {"57600", B57600},
84
                {"115200", B115200},
85
                {NULL, 0}
86
        };
87
        int speed = B9600;
88
 
89
        if(argc < 2) {
90
                fprintf(stderr, "example: %s /dev/ttyS0 [115200]\n", argv[0]);
91
                exit(1);
92
        }
93
 
94
        comfd = open(devicename, O_RDWR | O_NOCTTY | O_NONBLOCK);
95
        if (comfd < 0)
96
        {
97
                perror(devicename);
98
                exit(-1);
99
        }
100
 
101
        if(argc > 2) {
102
                speed_spec *s;
103
                for(s = speeds; s->name; s++) {
104
                        if(strcmp(s->name, argv[2]) == 0) {
105
                                speed = s->flag;
106
                                fprintf(stderr, "setting speed %s\n", s->name);
107
                                break;
108
                        }
109
                }
110
        }
111
 
112
        fprintf(stderr, "C-a exit, C-x modem lines status\n");
113
 
114
        tcgetattr(STDIN_FILENO,&oldkey);
115
        newkey.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;
116
        newkey.c_iflag = IGNPAR;
117
        newkey.c_oflag = 0;
118
        newkey.c_lflag = 0;
119
        newkey.c_cc[VMIN]=1;
120
        newkey.c_cc[VTIME]=0;
121
        tcflush(STDIN_FILENO, TCIFLUSH);
122
        tcsetattr(STDIN_FILENO,TCSANOW,&newkey);
123
 
124
 
125
        tcgetattr(comfd,&oldtio); // save current port settings 
126
        newtio.c_cflag = speed | CS8 | CLOCAL | CREAD;
127
        newtio.c_iflag = IGNPAR;
128
        newtio.c_oflag = 0;
129
        newtio.c_lflag = 0;
130
        newtio.c_cc[VMIN]=1;
131
        newtio.c_cc[VTIME]=0;
132
        tcflush(comfd, TCIFLUSH);
133
        tcsetattr(comfd,TCSANOW,&newtio);
134
 
135
        print_status(comfd);
136
 
137
        while(!need_exit) {
138
                fd_set fds;
139
                int ret;
140
 
141
                FD_ZERO(&fds);
142
                FD_SET(STDIN_FILENO, &fds);
143
                FD_SET(comfd, &fds);
144
 
145
 
146
                ret = select(comfd+1, &fds, NULL, NULL, NULL);
147
                if(ret == -1) {
148
                        perror("select");
149
                } else if (ret > 0) {
150
                        if(FD_ISSET(STDIN_FILENO, &fds)) {
151
                                need_exit = transfer_byte(STDIN_FILENO, comfd, 1);
152
                        }
153
                        if(FD_ISSET(comfd, &fds)) {
154
                                need_exit = transfer_byte(comfd, STDIN_FILENO, 0);
155
                        }
156
                }
157
        }
158
 
159
        tcsetattr(comfd,TCSANOW,&oldtio);
160
        tcsetattr(STDIN_FILENO,TCSANOW,&oldkey);
161
        close(comfd);
162
 
163
        return 0;
164
}
165
 
166
 
167
int transfer_byte(int from, int to, int is_control) {
168
        char c;
169
        int ret;
170
        do {
171
                ret = read(from, &c, 1);
172
        } while (ret < 0 && errno == EINTR);
173
        if(ret == 1) {
174
                if(is_control) {
175
                        if(c == '\x01') { // C-a
176
                                return -1;
177
                        } else if(c == '\x18') { // C-x
178
                                print_status(to);
179
                                return 0;
180
                        } else if(c == '\x7F') {
181
                          // convert ctrl-? to ctrl-h
182
                          // kinda silly, but useful.
183
                          c = '\x08';
184
                        }
185
                }
186
                while(write(to, &c, 1) == -1) {
187
                        if(errno!=EAGAIN && errno!=EINTR) { perror("write failed"); break; }
188
                }
189
        } else {
190
                fprintf(stderr, "\nnothing to read. probably port disconnected.\n");
191
                return -2;
192
        }
193
        return 0;
194
}
195
 
196
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.