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

I like to write a file and update the value every second:

my String is: 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:

InvokeRepeating("Write", 0.5f, 1.0f);
PStreamWriter writer = PStreamWriter.FromFile("power");

     void Write()
     {
         print("Write...");
         writer.power = power;
         writer.Write();
     }

Or another way to solve this?

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.

1 Like

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…

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?

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. :slight_smile:

I believe it’s a device for commumicating between other devices over wifi. I could be wrong though.

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-need-a-programmer-for-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 :slight_smile:

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.

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.:

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

I mean from the device connected to the pc. How do you intend on commuicating with the bike and unity?

The Switches are connected to GPIO 20+21 Ports from a “raspberry pi”
RPM (reed switch) is connected on GPIO 4