LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Text files and audio capture

Hello everyone!

I have a problem on a fairly complex VI and I'm not an expert.

In the main loop I have a capture of strings via serial port, I analyze some numeric values from these strings and when these values exceed the predefined I generate "alarms" with LED.


The point is that, once turned on the alarm, I have to save two files: the text string from the serial and the registration of 2-3 seconds of audio. These files must have the name taken from the "get date / time".

 

How can I do?

 

I tried to do three different loops at the same time:
1. Main

2. Text saving

3. Audio recording. 

 

But unfortunately I can not exchange information on the file name and string read between the various loops, even though the "local variable".

 

Can anyone help me?

 

Thanks!!!

0 Kudos
Message 1 of 11
(4,581 Views)

This is the English version of this post.

 

What do you mean that you cannot pass the value even with local variables? Is the local variable outside of the loop? Please post your code.

0 Kudos
Message 2 of 11
(4,566 Views)

I wrote in 2 languages to get more answers.

I post a part of the code.

In the bottom you can see the main loop with the serial acquisition. In this loop ther is also a boolean switch (this will replace with over-value allarm) that will start the text saving with file name generated from the date.

In the upper part you can see the second loop wich should open a file (with the name right), save the text and close the file. All this at the command.

For the audio recording (third loop) I think to try the same way of this.

 

The problem is that in this way when I push the boolean the second loop doesn't save anything, even if the path is generated correctly.

 

Can you help me?

0 Kudos
Message 3 of 11
(4,550 Views)

@Ermanno wrote:

I wrote in 2 languages to get more answers.


That doesn't usually help. It just irritates people when you post the same question in multiple places. Plus, it leads people to ask you the same exact questions or give you the same exact suggestions when they don't know you've already asked the question somewhere else and are already being helped.

 

As for your code... you have a classic race condition. Actually more than one. You are reading the "appended path" via the local variable before it's actually been written to. The value is set inside the loop, but your program will read the value before either loop has even started.Your second race condition has to do with the Stringa local variable. Again, you are reading its value without actually knowing if it's been updated by the other loop.

 

Why do you have 2 loops? Why isn't the writing of the file in the same loop as the read operation?

 

Other comments:

  • Why are you creating the filename inside the loop? That makes no sense. Why would you want to create a new file each and every time you loop around?
  • There is a simpler way to create the filename: If you look at the help for the Format Into String function you will see that you can use format codes to convert the timestamp to a string:
0 Kudos
Message 4 of 11
(4,539 Views)

I just wanted to help users of both languages ​​so that they could answer in the native language. However, I apologize for the trouble.

Thank you for the advice.

 

For the code:

The "Stringa" local variable is shared in the correct way between the loops, the problem is the "appended path".

I used 2 loops because when I put the writing operation in the main loop, the result is a file text with a single string, instead I need the flow of strings until the alarm end.

Also because in the case of "audio recording" I have to acquire the string in the same time, if I put the audio recording in the main loop I lose the string from the serial until the acquiring is finished.

Have you any idea?

 

For your comments:

- I create only tha name of the file, not the file. The real file is created when the alarm starts. How else can I do?

- Your are right, it is faster.

 

Thank you for the help!

0 Kudos
Message 5 of 11
(4,531 Views)

Ermanno wrote:

For the code:

The "Stringa" local variable is shared in the correct way between the loops, .


It's not. Look at the code again. That loop runs every 200 msec and write the contents of StringA, even if the value hasn't changed. Your occurence isn't doing anything for you once the loops have started.

 


I used 2 loops because when I put the writing operation in the main loop, the result is a file text with a single string, instead I need the flow of strings until the alarm end.

This sounds like you were wiring the path to the Write file function. In this case it will open the file and write to it starting at the beginning. If you open the file outside of the loop (or in the first iteration of the loop) and use the refnum, then the Write File function will write where it left off.

 


Also because in the case of "audio recording" I have to acquire the string in the same time, if I put the audio recording in the main loop I lose the string from the serial until the acquiring is finished.

Have you any idea?


I don't really understand what this means. What "audio recording"? I do not see any kind of audio recording in the image you posted. Can you upload your actual VI?

0 Kudos
Message 6 of 11
(4,521 Views)

 

It's not. Look at the code again. That loop runs every 200 msec and write the contents of StringA, even if the value hasn't changed. Your occurence isn't doing anything for you once the loops have started.

 


 

Ok, but the sharing works. I just have to sinchronize the flow in the second loop (200ms of delay is a test).

 

 

 

This sounds like you were wiring the path to the Write file function. In this case it will open the file and write to it starting at the beginning. If you open the file outside of the loop (or in the first iteration of the loop) and use the refnum, then the Write File function will write where it left off.


I don't understand. If I put "open file" and "write file" in the loop the result is a file with only a string so I put "open file" out of loop. Can you post an example?

 

 

 

I don't really understand what this means. What "audio recording"? I do not see any kind of audio recording in the image you posted. Can you upload your actual VI?


The whole VI does not yet exist. I will put the third loop with the audio recording when I have solved the problem of the text file, it will have a similar problem I think.

0 Kudos
Message 7 of 11
(4,510 Views)

I don't understand. If I put "open file" and "write file" in the loop the result is a file with only a string so I put "open file" out of loop. Can you post an example?


You don't want to put the open file inside the loop. The open file goes outside the loop. As the Help for Write to Text File explains: "If you wire a file refnum to the file input, writing begins at the current file position.". Thus, the following code will create a text file with each line generated in the iteration appended to the text file:

 

 

 

EDIT: Oops, forgot to connect the error out from the Write File to the Close File so it can be shown externally.

0 Kudos
Message 8 of 11
(4,503 Views)

Thank you!

Ok. So the code in the my first image (loops.jpg) is enough correct, but now, how can I get the name of the file, updated with each event?

0 Kudos
Message 9 of 11
(4,480 Views)

I don't understand. Your initial picture showed you logging the serial port to file. Are you saying that you only want to write to file when some condition occurs? What is this condition? Is it when you see some specific text on the serial port? If so, then you'd want to do something like this:

0 Kudos
Message 10 of 11
(4,474 Views)