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 :
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 :
First off, consider compiling with all warnings enabled. Example: void *read will trigger lots of problems. read, write are standard names for UNIX syscalls.
Next -
means that as soon as the two threads are created (assuming they were since you do not check return codes), ALL threads will exit - when you call pthread_exit() from main() it shuts down everything. So if and why your code runs - I cannot see? check out pthread_join().
There are many other issues with your code. There is a concept in programming: 'not even wrong'. Code can be wrong for lots of reasons. But when code goes off into undefined behavior - which yours is doing - it then does not have actual meaning. No offense. Your write function is doing reads -getchar() is a read.
First step: turn on compiler warnings, example:
Fix your code until there are ZERO warnings.
Check and obey your return codes.
After you get that organized, please come back with more questions.
Thanks for your answer.
I changed names to readinput (ex read function) and serial (ex write function), sorry about wrong names.
In readinput function, I get the user input (getchar()) and then write it to the port. And in serial function I read the port and print it.
I fixed my code, and now there are no warnings. But still the problem remains!
Let me ask my question from an upper point of view. How can I read the port immediately after writing data? Without the need of using sleep command after writing data to the port?
One way is to synchronize with a select (or poll) and set the fd's to non-blocking.
Once the read gets a \n or a write sends an \n - leave the function - I would not use threads. Unless this is a school assignment.
Use select or poll on both. Never block on a read or a write like you are currently doing.
select() will block for you until one fd (ex, read or write) becomes ready for i/o.
pseudocode: use 3 fd's
If you get a file closed return it means some happened - user quit with ctrl/D or CISCO went South.
Last edited by jim mcnamara; 03-17-2013 at 11:27 AM..
This User Gave Thanks to jim mcnamara For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)