jeudi 23 juin 2016

Storing data from arduino serial port to a text file via C

I am trying to use a simple C program to write the data from arduino to a text file using a C program. I had done this before in OSX but in Suse leap is not working at all.

The code is:

static void collect(GtkWidget *widget, gpointer data)
{

  pid = fork();

  //Here we begin te data transmission from our usb port... ( the port must be initialized before we run the code)
  if ( pid == 0 )
{
      execlp("/bin/bash", "/bin/bash", "-c", " cat /dev/ttyUSB0 > data.txt", (char *)NULL );

     //printf("hello");

    }
}

So I am using execlp to call cat and use this to store the data from the port ttyUSB0 were my arduino is connected to the text file. The original code en OSX looked different.

execlp("/bin/sh", "/bin/sh", "-c", " cat /dev/cu.usbmodem1411 -9600 > data1.txt");

This is because is not accepting the /9600 flag and also my port is different, also /bin/sh doesn’t appear to exists in my Suse leap.

After running the code, data.txt is created but sometimes the file doesn’t have any data and sometimes only stores the 1st value. Also i can not see the cat command when I use ps -e and this tells me that maybe is runing very briefly behind and that is why sometimes I only get the 1st value.

I suppose I am using wrong cat and also maybe sh must exist at SuSE leap but now I keep looking at the web if that is my problem and I try to move the code to fixit. Anyone has any idea about what is happening?.

Thanks in advance and I keep looking for the solution moving the code meanwhile and searching at the web. Ciao.

Aucun commentaire:

Enregistrer un commentaire