Shop OBEX P1 Docs P2 Docs Learn Events
Read/write to pc files with prop — Parallax Forums

Read/write to pc files with prop

MauvaiMauvai Posts: 45
edited 2014-10-28 19:02 in Propeller 1
I am trying to rapidly collect data (~200Hz) with the prop and export it to a pc for analysis. The code I am using is as follows:
#include <stdio.h>
#include <propeller.h>

int main()
{
    char name[20];
    FILE *fp1;


    waitcnt(CLKFREQ+CNT); // pause for terminal to open


    sprintf(name,"Test"); // test file name
    printf("Opening file '%s'.\n", name);


    char sentence[] = {128, 13};


    fp1=fopen("myfile.txt", "w");


    if(fp1==0) 
    {
        printf("File was opened.\n");
        fputs (sentence,fp1);
        fputs("\nHI", fp1);
        fclose(fp1);
        
    }
    else {
        printf("Can't open file %s.\n",name);;
    }
    while(1);
    return 0;
}




This however doesn't do a whole lot. Any suggestions?

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-10-27 12:59
    When fopen fails it returns 0 so your logic is off (provided nothing else is wrong).
  • MauvaiMauvai Posts: 45
    edited 2014-10-27 13:14
    Ah. in that case is should have been clearer: it returns 0 every time, and thus is failing
  • kuronekokuroneko Posts: 3,623
    edited 2014-10-27 13:28
    From looking at the example code (e.g. SD Datalogger.side) you seem to be missing the sd_mount call:
    /*
      SD Datalogger.side
    
      Application prompts you to type numbers into SimpleIDE Terminal, and
      records to SD.  Plays back all numbers after you type q and press Enter.
      
      http://learn.parallax.com/propeller-c-simple-devices/sd-card-data
    */
    
    #include "simpletools.h"                      // Include simpletools header    .
    
    int DO = 22, CLK = 23, DI = 24, CS = 25;      // SD card pins on Propeller BOE
    
    int main(void)                                // main function
    {
      print("Enter several values.\n\n");         // User instructions
      print("To see logged values, press\n");
      print("Enter a second time.\n\n");
    
      sd_mount(DO, CLK, DI, CS);                  // Mount SD card
    
      FILE* fp = fopen("test.txt", "w");          // Open a file for writing
    
      int n, status;                              // Number & status variables
      while(1)                                    // Endless loop
      {
        print("> ");                              // User prompt
        status = scan("%d", &n);                  // Get number & status
        if(!status) break;                        // Status 0? Break out of loop
        fwrite(&n, 4, 1, fp);                     // Write number to SD card
      } 
      fclose(fp);                                 // Close the file
      
      print("\nYou entered:\n");                  // List heading
    
      fp = fopen("test.txt", "r");                // Open a file for writing
      while(1)
      {
        status = fread(&n, 4, 1, fp);             // Read from SD & get status
        if(!status) break;                        // If no more, break loop
        print("n = %d\n", n);                     // Display value
      } 
      fclose(fp);                                 // Close the file
    }    
    
  • MauvaiMauvai Posts: 45
    edited 2014-10-27 13:35
    export it to a pc for analysis.

    If possible I want to export to pc over the USB, I don't have an SD card setup
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-10-27 17:06
    I think you're a little confused as to what PropGCC's implementation of fopen does. It only opens files that are stored on an SD card mounted via PropGCC's sd_mount function.

    To work with files on your PC, you'll need to send them via some sort of communication port. Generally, this means a serial port. You can use Simple Librarie's print and printi, PropGCC's printf (or tiny printf), or any number of UART/RS232 drivers. Take a look at PropWare's UART classes, libpropeller's Serial classes, Simple Librarie's FDSerial functions, and others.

    On the PC side, you'll need to write your own application to read the serial console and copy the data to a file.
  • MauvaiMauvai Posts: 45
    edited 2014-10-27 17:14
    Ah, I see... I wouldn't say confused so much as completely unaware. That makes a lot of sense though. Thank you
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-10-27 17:56
    It's no problem. Hope you're finding the Propeller enjoyable :)
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-10-28 10:27
    Mauvai wrote: »
    I am trying to rapidly collect data (~200Hz) with the prop and export it to a pc for analysis.
    With a 115,200 baud connection you can print 11,520 characters per second. This works out to about 57 characters for every interval if the update rate is at 200Hz. So if 57 characters is sufficient you can just print out your data, and then capture it with a terminal emulator program that can save the received data. This assumes that the data contains standard ASCII characters. Otherwise you may need to print it out in hex format.

    If you require more than 57 characters per data interval you could buffer the data into memory and dump it out later. This assumes you only need to buffer a few kilobytes of data, and there is enough space in hub RAM to buffer it. Another possibility is to run the serial port at a higher rate. The full duplex serial driver should be able to handle 230,400 baud, and maybe even higher than that.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-10-28 11:55
    Hmm... seeing the math, that looks like it could present an actual challenge :) Now i'm interested.

    Are you familiar with the structure of a circular buffer? If single-cog performance becomes a problem, I'd recommend a writing to a global, circular buffer from your data collector cog and reading from that buffer in your UART cog. Assuming you're doing write-only out the serial port (never taking input from the PC), PropWare's unbuffered UART classes would work great for this (and in LMM mode, they can reach an average of 673,230 bits/second at ~1.4MHz).

    There are other implementations that are even faster, but I'll refer you to this thread for those.
  • MauvaiMauvai Posts: 45
    edited 2014-10-28 16:43
    Im going to be honest,,,, 57 characters per cycle is probably loads. I'm only looking for gyroscope outputs, so actually i only want 6 characters per second (2 registers per axis).
    Also, i probably wont implement this... I don't have any experience writing code to control the hyperterminal from the pc, and i dont need it all that badly - i can come up with a way of processing on chip if i need to. thanks though
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-10-28 19:02
    As I understand it, many terminal programs already have the ability to log data to a file. For instance, a quick Google showed this:

    http://technet.microsoft.com/en-us/library/cc738294(v=ws.10).aspx

    If you want to do it via your own program, there are lots of ways. A couple years ago, my senior design team wrote a program that monitored various parameters from a car and communicated them via UART to a PC, where they were displayed in a Java GUI. The Java program communicated directly with the computer's COM port - no need for hyperterminal in the middle :)

    I'm working on pulling up that code.... i'll post it when I find it.
Sign In or Register to comment.