serial programming using termios

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

serial programming using termios

Ted Reynard
Hi guys
I'm trying to connect to CISCO router using termios. So I decided to
have two threads one for reading data and the other one for writing
data.
And here's my code :

int mainfd=0;
char ch[2] = {NULL};

void *write(void *)
{
char temp;
while(1)
{
temp = getchar();
ch[0] = temp;   ch[1] = '\0';
if(temp == '~')
{
printf("connection closed.\r\n");
close(mainfd);
pthread_exit(NULL);
}
check=write(mainfd, ch, 1);
ch[0]='\0';
}
}

void *read(void *)
{
char outputbuffer[10000]= {0};
while(1)
{
outputbuffer[0]='\0';
int charnumber=read(mainfd, &outputbuffer, sizeof(outputbuffer));
outputbuffer[charnumber] = '\0';
printf("%s",outputbuffer);
outputbuffer[0] = '\0';
}
}

int main(int argc,char *argv[])
{
//////////////////
struct termios old = {0};
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
//////////////////
struct termios options;
static int portnum=atoi(argv[1]);

mainfd = open_port(portnum);

fcntl(mainfd, F_SETFL, FNDELAY);
tcgetattr(mainfd, &options);
cfsetspeed(&options, speed);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag |= CSTOPB;

options.c_cflag &= ~CSIZE;
options.c_cflag |=  CS8;
options.c_cflag &= ~CRTSCTS;
options.c_iflag &= ~(ISTRIP|ICRNL);
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
//
tcsetattr(mainfd, TCSAFLUSH, &options);
pthread_t threads[2];
pthread_create(&threads[0], NULL, write, NULL);
pthread_create(&threads[1], NULL, read, NULL);
pthread_exit(NULL);
}

The problem is that I have to add sleep(2) (at least 2s) after write
user input to the port (line 17 I think) otherwise the output would not
be what I expect. Without this sleep command, every character I type,
the output is shown with next character not at time :

Router>
Router>abc // While I typed "abcd". If I continue with typing "e", then the output will be "abcd" and so on ...

How can I fix this?
_______________________________________________
[hidden email] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-isdn
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: serial programming using termios

Hans Petter Selasky-5
On 03/17/13 08:56, Ted Reynard wrote:
> Hi guys
> I'm trying to connect to CISCO router using termios. So I decided to
> have two threads one for reading data and the other one for writing
> data.
> And here's my code :

What device(s) are you using under /dev/ ?

--HPS

_______________________________________________
[hidden email] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-isdn
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: serial programming using termios

Ted Reynard
>> Hi guys

>> I'm trying to connect to CISCO router using termios. So I decided to

>>have two threads one for reading data and the other one for writing

>>data.

>> And here's my code :


>What device(s) are you using under /dev/ ?


>--HPS

I'm using ttyu0 , ttyu1 , ...
_______________________________________________
[hidden email] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-isdn
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: serial programming using termios

Hans Petter Selasky-5
On 03/18/13 10:34, Ted Reynard wrote:

>>> Hi guys
>
>>> I'm trying to connect to CISCO router using termios. So I decided to
>
>>> have two threads one for reading data and the other one for writing
>
>>> data.
>
>>> And here's my code :
>
>
>> What device(s) are you using under /dev/ ?
>
>
>> --HPS
>
> I'm using ttyu0 , ttyu1 , ...
>

Hi,

This is not an ISDN device. This list is for ISDN related matters.

--HPS
_______________________________________________
[hidden email] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-isdn
To unsubscribe, send any mail to "[hidden email]"