Sponsored Content
Top Forums Programming Serial programming using termios Post 302781565 by turner on Sunday 17th of March 2013 03:38:13 AM
Old 03-17-2013
Serial programming using termios

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 :

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 :


Code:
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?
 

10 More Discussions You Might Find Interesting

1. Programming

c programming or unix programming!?

i would like advice on the usbject of c programming (in the middle of reading a book on C). could i benefit more if i apply that knowledge in the unix format if i were able to, or would that take the point out of learning C, basically I want to stay away from strying too far away from unix and use... (1 Reply)
Discussion started by: moxxx68
1 Replies

2. Programming

Serial port programming

I am developing an application in c with Linux OS, where a radio modem working at baud rate 9600 will be attached to PC on serial port. More than four such units will be communicating at one time, so there may be jamming or data corruption. Each module will be transmitting Data packets less than... (2 Replies)
Discussion started by: raj8109
2 Replies

3. UNIX for Dummies Questions & Answers

Carreer:Networking Programming in Unix (C programming Language)

Hello, I am trying to learn Networking Programming in C in unix enviorment. I want to know how good it is to become a network programmer. i am crazy about Network programming but i also want to opt for the best carreer options. Anybody experienced Network Programmer, please tell me is my... (5 Replies)
Discussion started by: vibhory2j
5 Replies

4. Programming

Programming serial port

Hello! Can someone advice me which method I will use if I want to communicate with device via RS232 serial port with this requirements: 1. Serial port is opened in non-canonical mode. 2. All the time I need to check is there something to read. 3. If I have to write something I need to write it... (6 Replies)
Discussion started by: jvrlic
6 Replies

5. Programming

C Programming - Hardware Programming

Can someone help me on suggesting some ways to access the memory content in RAM directly from C/C++ source code. Please provide me any book name or any URL so that I can get an exhaustive knowledge over it. If possible please give me some tips on interacting with hardwares directly through... (3 Replies)
Discussion started by: nandumishra
3 Replies

6. Ubuntu

Ubuntu 9.04 Serial application to telnet to serial device

Hello! I am working on an application which reads environmental instruments which have serial ports. The application requires a serial port to be present to talk to the device (i.e. /dev/ttyS0 ). In some instances the environmental devices will be 100's of yards away from the computer, so a... (5 Replies)
Discussion started by: mvona
5 Replies

7. UNIX for Dummies Questions & Answers

How does unix system administration, unix programming, unix network programming differ?

How does unix system administration, unix programming, unix network programming differ? Please help. (0 Replies)
Discussion started by: thulasidharan2k
0 Replies

8. HP-UX

Help with serial ports.

Hi, This is my first post here. I would like to ask: how to connect the barcode scanner to the serial port? What are the names in the system (in / dev) and serialport1 serialport0? Please help with this. Regards. (2 Replies)
Discussion started by: ftwojtek
2 Replies

9. Programming

[ERROR:Resource temporarily unavailable!] Serial writing by termios library

Hello, I am using the termios library to write data that I get from a Bluetooth device to a modem via serial. The data arrive from the Bluetooth device correctly every 50ms and I have to bypass them on the serial ttyUSB3 where it is connected to a modem connected to a socket with static IP. The... (10 Replies)
Discussion started by: enaud
10 Replies

10. UNIX for Dummies Questions & Answers

From iOS programming to Linux system programming

Hello. I like Linux and C programming language. Allways wanted to understand kernel and become a Linux system programmer. And I also like Objective-C and iOS. These two programming areas have relations: 1. Linux and iOS are UNIX-like systems, POSIX compliant. 2. It is useful to know C language... (2 Replies)
Discussion started by: Rockatansky
2 Replies
cfsetispeed(3)						     Library Functions Manual						    cfsetispeed(3)

NAME
cfsetispeed - Sets input baud rate for a terminal LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <termios.h> int cfsetispeed( struct termios *termios_p; speed_t speed); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: cfsetispeed(): POSIX.1, XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Points to a termios structure containing the input baud rate. Specifies the new input baud rate. DESCRIPTION
The cfsetispeed() function sets the input baud rate stored in the structure pointed to by the termios_p parameter to the value specified by the speed parameter. If the input baud rate is set to 0 (zero), the input baud rate will be specified by the value of the output baud rate. There is no effect on the baud rates set in the hardware until a subsequent successful call is made to the tcsetattr() function on the same termios structure. RETURN VALUES
Upon successful completion, the cfsetispeed() function returns a value of 0 (zero). Otherwise, a value of -1 is returned. RELATED INFORMATION
Functions: cfgetispeed(3), cfgetospeed(3), cfsetospeed(3), tcsetattr(3) Files: termios(4) Standards: standards(5) delim off cfsetispeed(3)
All times are GMT -4. The time now is 02:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy