How to allow simultaneous reading/writing of a file? - c

I am new in programming C language, but I have to do a project with three micro-controllers, each using a bluetooth device. The micro-controllers have to access a text file on my computer where they update their activity in order to have one of them measuring temperature (Activity1), another controlling a motor (Activity2) and the other waiting (Activity3) or doing something else (Activity4), but not two of them doing the same activity.
I need to program "something" to make this file available to all micro-controllers. For instance, the text file shows:
Line 1- Activity1 //for micro1
Line 2- Activity3 //for micro2
Line 3- Activity2 //for micro3
After some event, micro1 had to change its activity and update the file
Line 1- Activity4 //for micro1
Line 2- Activity3 //for micro2
Line 3- Activity2 //for micro3
Here, micro1 only have the Activity4 as a free choice to change, but it knew that through reading the file.
I need you tell me what is that "something"? Do I need a server?

Related

Reading data from a continuously updating log file

I have a log file (similar to a web server's access log) which I need to continuously read and use a regular expression to fetch a value in each line.
For example, if you could imagine reading from a web server's access log, I would be getting the IP address of each visitor as their visit is written to the log.
Of course this is easy to do using the linux command line (combination of tail and sed, or something like that), but I want to do it using C code.
I guess I could open the file, read X lines, save the number of the last line I read, and then on the next round open the file, move to the line X, and read from there, and so on, but this seems very clumsy.
Is there a known way or best practice for reading data from a continuously updating log file?
Thanks

read line of file in realtime in C

I know how to do basic file operations in C, but what I'd like to do if it is possible is somehow create a variable that represents a live running file (for example, an access_log from apache that updates every second). Then I want to be able to read the last line that is entered into the file as it happens regardless of whether any other process currently has the file open or not.
I'm thinking of code like this:
int main(){
FILE *live=fattachto("/path/to/apaches/access_log");
long lastupdated=live->lastwrite();
while(1){
if(live->lastwrite() != lastupdated){printf("File was just updated now\n");}
sleep(1);
}
}
Yes I did put in sleep in my code because I want to ensure my code doesn't oveheat the cpu.
and the code above as-is won't execute because I'm looking for the correct set of functions to use to produce the end result.
Any idea?
Why don't you just set line buffering or use small buffer read size, once you've seeked to the end of the log file? Then simply do blocking reads on the lines from the file as they're added?
Then when the read returns, you can signal, set flags or initiate processing, on any interesting input.

Contiki with Tmote data I/O

Here I have a Tmote Sky node. And I have printf the RSSI on the terminal. Now I want to store these RSSI data to my computer. I have tried cfs which is used to operate the external flash of a node. So how can I save the data to my computer with contiki.
/platform/sky/Makefile.common provides a target serialdump, which will also print the output to a file named serialdump-<current time>. Therefore you want to tun make serialdump TARGET=sky.
Or do you want to get the data from the external flash? In that case you need to add a function that dumps the file contents to the serial (e.g. when pushing the button or sending a special command via serial). You can then save that output to a file.

Retrieve data from COM port using a batch file

I'm trying to automatically retrieve data from a COM port using a batch file.
I'm able to configure the com port and to send the command in other to ask my device for the info.
The problem is that I'm not able to capture the data that the device sends. I've tried with RealTerm and the device is working and sends the info back to the pc, but I really need the batch file to do it automatically, here is the code:
echo off
MODE COMxx ...
COPY retrievecommand.txt \\\\.\COMxx:
COPY \\\\.\COMxx: data.txt
Any suggestions?
Use the TYPE command in a recursive loop using the DOS GOTO command to a DOS LABEL. Use 'append output' to capture text like TYPE COM1:>>Data.txt The double > means continually concatenate (or append) to Data.txt. A single > or 'redirect output' would replace the text in Data.txt every loop (if com data present on port). Add a 2nd line that redirects to the monitor screen so you can watch activity too (i.e. TYPE COM1:>CON [CON means console or monitor screen but you can omit it as console is default anyway])
Control-Z is not needed by TYPE command. It will just dump text continually until operator does a Control-C and then a Y to break the loop. You really don't need to stop the loop unless you are done with the batch file all together. The Data.txt file will be available to other programs live and will not present a 'Sharing Violation' if you try to access it with another program like NOTEPAD.EXE while this batch file is still looping.
Also if you make a 3rd line in the batch file that says TYPE COM1:>Data1.txt [notice only one redirect], you will have a single line of instant text that will disappear with next iteration. But sometimes that is helpful if you need only one line of data. There are creative ways to extract one line of data to another text file using the DOS FIND command.
When reading, the COPY command will continue until it detects the end of file. As the source is a device (with a potentially infinite stream) it only knows to stop when it detects an end of file marker. This is the Ctrl-Z (0x1A) character.
The suggestion in the duplicate question of using the TYPE command to read is likely to result in the same problem.
There is no standard mechanism to read a single line. If you can port your application to PowerShell, you should be able to read single lines with the results you expect.

How can I use a text file from the internet in my C program?

I'm working on a project, and I made a C program that reads the date, time, and wave height from a .txt file stored on my computer, converts the date and time to GPS time for use at a scientific research institution, and outputs GPS time and wave height to the screen. However, the text file that I am working with is actually stored at http://www.ndbc.noaa.gov/data/realtime2/SPLL1.txt . Is there any way that I could have my C program open the text file from the web address rather than from my local hard drive?
FYI: to access the file on my computer I used fopen and to interact with the data contained I used a combination of fgets and fscanf.
It is much more involved to get a web-resource than to read a file from disk, but you can absolutely do it, for example by using a library such as libcurl.
An alternative strategy is to make components and tie them together with bash or other scripting. Your C program could for example read from standard input, and you could make a bash script something like this:
curl http://www.ndbc.noaa.gov/data/realtime2/SPLL1.txt | ./the_program
This way, you could keep your core C program simpler.

Resources