The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Assigning output of a command to variable jeriryan87 Shell Programming and Scripting 9 06-30-2008 05:55 PM
ls command output to variable in script dsrookie UNIX for Dummies Questions & Answers 1 03-10-2008 04:14 PM
Assigning output of command to a variable oma04 Shell Programming and Scripting 5 06-27-2006 01:11 PM
redirect command output to variable hugow UNIX for Dummies Questions & Answers 1 06-22-2005 07:43 AM
Command output to a variable. videsh77 Shell Programming and Scripting 8 12-16-2004 06:06 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-19-2009
daaran daaran is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
Command output into a variable

Hi, with this command:

cu -l /dev/ttyACM0 -s 9600 > name.txt

I put the output of the port in a txt

Is posible to do the same (or similar) in a var directly, inside a C program?

cu -l /dev/ttyACM0 -s 9600 > variable ?

I have trying this withs pipes, but i dont know how to adress te port to a pipe.
Also i have tried with function read, fgets, etc.. but I don't why, it doesn't works, just write.

Thank you


  #2 (permalink)  
Old 10-19-2009
jlliagre jlliagre is online now Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,493
Using a POSIX compatible shell (ksh, bash):

Code:
variable=$(cu -l /dev/ttyACM0 -s 9600)

  #3 (permalink)  
Old 10-19-2009
ghp ghp is offline
Registered User
  
 

Join Date: Dec 2008
Location: Zwevegem, Belgium
Posts: 13
You'd probably want to use "popen" for that.
  #4 (permalink)  
Old 10-19-2009
jlliagre jlliagre is online now Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,493
Indeed, I missed the question was about doing it from C.
  #5 (permalink)  
Old 10-19-2009
daaran daaran is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
Hi, thank you very much.
popen seems to works ok. But there s a problem with pclose();

when I write:

file = popen ("cu -l /dev/ttyACM0 -s 9600", "w"); //WRITE
fputs("AT\n",file);
pclose(file)


this run ok. In console:

Connected.
cu: End of file on terminal
Disconnected.


But with:

file = popen ("cu -l /dev/ttyACM0 -s 9600", "r"); //READ

there is no response when I try to close the pipe. Not Disconnected, and so i can't write one more time.

I'm trying to do this, because I need a bidirectional pipe , and the only way I know to do this is to: open to write -- > close --> open to read ---> close and another time the same
BUt pclose function failed in read mode .
thank you
  #6 (permalink)  
Old 10-26-2009
ghp ghp is offline
Registered User
  
 

Join Date: Dec 2008
Location: Zwevegem, Belgium
Posts: 13
popen is unidirectional, closing and reopening will not allow you to establish any decent communication.


Ever heard of named pipes?

Create a named pipe /tmp/cu.in, use it:
file = popen ("cu -l /dev/ttyACM0 -s 9600 < /tmp/cu.in", "r"); //READ

Then you could write to /tmp/cu.in what you want as input for cu, and retrieve the results with fgets.

But you really should be using some kind of inter-process communication, messaging.
  #7 (permalink)  
Old 10-26-2009
jlliagre jlliagre is online now Forum Advisor  
ɹǝsn sıɹɐlosuǝdo
  
 

Join Date: Dec 2007
Location: Paris
Posts: 1,493
For bidirectional pipes, you can use the "pipe" system call.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0