tty0tty.c
2.9 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* ########################################################################
tty0tty - linux null modem emulator
########################################################################
Copyright (c) : 2010 Luis Claudio Gambôa Lopes
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
For e-mail suggestions : lcgamboa@yahoo.com
######################################################################## */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termio.h>
int
ptym_open(char *pts_name, char *pts_name_s , int pts_namesz)
{
char *ptr;
int fdm;
strncpy(pts_name, "/dev/ptmx", pts_namesz);
pts_name[pts_namesz - 1] = '\0';
fdm = posix_openpt(O_RDWR | O_NONBLOCK);
if (fdm < 0)
return(-1);
if (grantpt(fdm) < 0)
{
close(fdm);
return(-2);
}
if (unlockpt(fdm) < 0)
{
close(fdm);
return(-3);
}
if ((ptr = ptsname(fdm)) == NULL)
{
close(fdm);
return(-4);
}
strncpy(pts_name_s, ptr, pts_namesz);
pts_name[pts_namesz - 1] = '\0';
return(fdm);
}
int
conf_ser(int serialDev)
{
int rc=0;
struct termios params;
// Get terminal atributes
rc = tcgetattr(serialDev, ¶ms);
// Modify terminal attributes
cfmakeraw(¶ms);
rc = cfsetispeed(¶ms, B9600);
rc = cfsetospeed(¶ms, B9600);
// CREAD - Enable port to read data
// CLOCAL - Ignore modem control lines
params.c_cflag |= (B9600 |CS8 | CLOCAL | CREAD);
// Make Read Blocking
//fcntl(serialDev, F_SETFL, 0);
// Set serial attributes
rc = tcsetattr(serialDev, TCSANOW, ¶ms);
// Flush serial device of both non-transmitted
// output data and non-read input data....
tcflush(serialDev, TCIOFLUSH);
return EXIT_SUCCESS;
}
int main(void)
{
char master1[1024];
char slave1[1024];
char master2[1024];
char slave2[1024];
int fd1;
int fd2;
char c1,c2;
fd1=ptym_open(master1,slave1,1024);
fd2=ptym_open(master2,slave2,1024);
printf("(%s) <=> (%s)\n",slave1,slave2);
conf_ser(fd1);
conf_ser(fd2);
while(1)
{
if(read (fd1,&c1,1) == 1) write(fd2,&c1,1);
usleep(20);
if(read (fd2,&c2,1) == 1) write(fd1,&c2,1);
usleep(20);
};
close(fd1);
close(fd2);
return EXIT_SUCCESS;
}