Help with C in linux Ubuntu 12.04


 
Thread Tools Search this Thread
Top Forums Programming Help with C in linux Ubuntu 12.04
# 1  
Old 06-20-2012
Help with C in linux Ubuntu 12.04

I'm trying to use the sytem() to send a terminal command. I haven't been able to run the program. Can someone show me how am I suppose to write this command: (echo "#o1" > /dev/ttyUSB1). Is there a better way to use the command on the C file so that when I call the file and run it on the terminal it recognize and execute that command?

Last edited by radoulov; 06-20-2012 at 06:24 PM..
# 2  
Old 06-20-2012
Code:
#include <stdio.h>
.............  in your code somewhere:
FILE *out=fopen("/dev/ttyUSB1", "wb");
fwrite("#o1", 3, 1, out);
fclose(out);

One way....
# 3  
Old 06-20-2012
That's not quite the same as echo:

Code:
fwrite("#o1\n", 4, 1, out);

# 4  
Old 06-20-2012
I agree - but he does not need the "\n" character, that is a control code sequence.
# 5  
Old 06-21-2012
Thanks for the help,I'm wirking wiht that code right now. to be more clear at my goal, I'm trying to read data from a microcotroller using the Ubuntu terminal. I manage to read the data and send the instruction using this commands:
echo "#o1" > /dev/ttyUSB1
cat /dev/ttyUSB1 > /home/hectormasencio/Desktop/text.txt

I use the cat command for reading the data received from the microcontroller and save it to a text.

what I need to do is create a .c file to send those commands automatically just by executing the file from the terminal in Ubuntu.
# 6  
Old 06-21-2012
You don't need C to put two statements in a text file:

Code:
# Create retrieve.sh
cat <<EOF >retrieve.sh
#!/bin/sh
echo "#o1" > /dev/ttyUSB1
cat /dev/ttyUSB1 > /home/hectormasencio/Desktop/text.txt
EOF
# Set it executable
chmod +x ./retrieve.sh
# Run it
./retrieve.sh

# You now have a file which retrieves data from your microcontroller when you run it.

But if you insist:

Code:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void)
{
        ssize_t ret;
        char buf[512];
        int fd=open("/dev/ttyUSB1", O_RDWR);
        if(fd < 0) return(1);

        write(fd, "#o1\n", 4);

        while( (ret=read(fd, buf, 512)) > 0)
                write(STDOUT_FILENO, buf, ret);

        close(fd);
        return(0);
}

# 7  
Old 06-29-2012
Thanks for the help bot code are very helpful, I still have a small problem in the C code.I run that code but I have a doubt, in the write function the STDOUT_FILENO, what does it mean or how it work? because I'm trying to create a C file that is going to be called from another program and the data a retrieve I need to save it to a text file.
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Ubuntu

Application clustering in Linux Ubuntu

Dear, Please help me to configure application clustering in linux ubuntu. Application is running apache server. Please help Jewel ---------- Post updated at 01:07 PM ---------- Previous update was at 12:48 PM ---------- linuxvirtualserver dot org in this link i go there's three... (2 Replies)
Discussion started by: Jewel100
2 Replies

2. Ubuntu

Modifying PATH (LINUX, Ubuntu)

Hello, I'm a newbi to Unix and the last few weeks I have been trying to learn Unix through a book called Unix in 24 hours. I have tried advanced shell programming (that's what the chapter is called) today and what the excersise was all about was to create mylocate - a version of locate that is... (1 Reply)
Discussion started by: Tolmac
1 Replies

3. UNIX for Dummies Questions & Answers

How do I mount a new disk on ubuntu linux?

Hello, I am fairly new to ubuntu and have been learning linux using this distro. I am using Ubuntu 11.04 server. Recently I added a new HD to the desktop.. however I know I need to mount it, My question is how would I mount this disk? so I can use it as a drive? Thanks here are my... (5 Replies)
Discussion started by: NelsonC
5 Replies

4. Ubuntu

XP and Linux (Ubuntu) on same disk, Can I install Ubuntu on not-yet partitioned portion of disk?

My PC (Esprimo, 3 yeas old) has one hard drive having 2 partitions C: (80 GB NTFS, XP) and D: (120 GB NTFS, empty) and and a 200 MB area that yet is not-partitioned. I would like to try Ubuntu and to install Ubuntu on the not-partitioned area . The idea is to have the possibility to run... (7 Replies)
Discussion started by: C.Weidemann
7 Replies
Login or Register to Ask a Question