Search Unity

Write a given value in a file and update the value every second

Discussion in 'Scripting' started by Andreas12345, Apr 29, 2017.

  1. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    I like to write a file and update the value every second:

    my String is:
    Code (CSharp):
    1. public string formattedPower = string.Format ("{0}  W", power);
    Now i want to write only the Output from {0} to a file and update it every second.

    Another question is, can another program read the file?

    For write and repeating i think:
    Code (CSharp):
    1. InvokeRepeating("Write", 0.5f, 1.0f);
    2. PStreamWriter writer = PStreamWriter.FromFile("power");
    3.  
    4.      void Write()
    5.      {
    6.          print("Write...");
    7.          writer.power = power;
    8.          writer.Write();
    9.      }
    Or another way to solve this?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I don't know what PStreamWriter is but yes, you can write to a file every second..
    You can look up : StreamWriter online. Or "How to write to disk c#" or something like that.
    And yes, another program can read it.

    There are a few options for IPC.

    If the value is always changing, I can maybe see why you'd want to write it (for access elsewhere, which I assume is your plan), but if it doesn't change, really no need to write. Just a thought.
     
    Andreas12345 likes this.
  3. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    I read power from a table and want send to ant+ device.
    the value is changing, thats the reason why i need to write in the file as update the value..
     
    Last edited: Apr 29, 2017
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Having one program write a file while another is trying to read it isn't a great idea.

    Can you not send the vaule over a network/usb to the device?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It's possible to use a file, I think you'd just have to be careful.
    Pipe or socket would be good, also.
    I also don't know what ant+ device is, but that's okay. :)
     
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    I believe it's a device for commumicating between other devices over wifi. I could be wrong though.
     
  7. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    Yes it is used for send data to an ANT+ Stick.
    1 Stick is in raspberry pi as sender, other one is in pc for the application in this case zwift cycling.
    i want to have the exact power and cadence for training. And i want to share this with other people. non profit project.
    If you want to earn some $ look here: https://forum.unity3d.com/threads/i...-a-small-ant-c-to-python-json-project.468753/

    i cannot do it alone, because i am not a programmer, but i like to pay some bucks for it.
    I just ask for the possibility :)
     
  8. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You should be able to read directly from the device. I would expect it is nothing more than a hid device do you could just read the packet and take the bytes you need or send them.
     
  9. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    i cannot read the device, it have no connection to outside, thats why i want read Level and RPM (The Power is calculated) have the values stored in a table.:

    1. Code (CSharp):
      1.    LEVEL_TO_POWER = {
      2.        1: [6,12,20,29,40,53,69,79,92,106,121],
      3.     2: [8,16,26,38,53,68,88,103,120,138,152],
      4.     3: [9,20,32,47,66,84,107,125,148,172,186],
      5.     4: [11,23,39,56,79,101,126,150,173,206,219],
      6.     5: [13,27,45,65,92,117,145,175,202,238,254],
      7.     6: [15,31,52,75,105,135,166,202,231,275,289],
      8.         7: [16,35,58,85,118,152,185,226,260,305,332],
      9.         8: [18,39,65,96,131,169,208,249,289,333,375],
      10.     9: [19,42,71,104,144,184,227,272,318,361,408],
      11.     10:[21,46,77,113,157,199,245,295,345,386,442],
      12.     11:[23,50,84,123,170,216,262,318,372,413,480],
      13.     12:[24,53,89,131,183,230,279,342,398,441,512],
      14.     13:[26,56,94,139,196,245,296,365,424,468,548],
      15.     14:[28,60,101,148,209,261,318,389,449,494,585],
      16.     15:[30,64,108,158,222,277,337,415,476,518,620],
      17.     16:[32,68,115,168,235,296,355,439,503,548,658],
      18.     17:[33,72,122,177,248,312,373,463,530,576,694],
      19.     18:[35,76,129,187,261,328,390,484,556,606,727],
      20.     19:[37,79,134,195,274,342,407,507,572,632,763],
      21.     20:[39,83,140,204,287,354,424,528,598,659,790],
      22.     21:[40,87,146,213,300,368,442,551,616,689,812],
      23.     22:[42,91,153,223,313,385,461,574,645,720,840],
      24.     23:[44,95,160,234,326,401,479,598,673,752,872],
      25.     24:[47,101,171,246,340,418,501,625,706,788,908],
      26.     }
     
  10. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    I mean from the device connected to the pc. How do you intend on commuicating with the bike and unity?
     
  11. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    The Switches are connected to GPIO 20+21 Ports from a "raspberry pi"
    RPM (reed switch) is connected on GPIO 4