LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to store data from TCI-IP ?

Hello,

 

My problem is the following:

 

I made a program witch contains a TCP-IP server ( my program is the server).

 

I based myself on the samples. Everything works but  I would like to be able to store all of the client's messages.

 

Let me explain: In the program, the received messages are stored in the "receivebuf" variable, but, logically, the new messages overwrite the previous ones. How to deal with this problem and be able to store all the messages ?

 

Especially since I would like to be able to make a condition message.

 

For example, if the first message is "Condition", then we enter a loop, to welcome the next messages. But the Callback brings back to the beginning (logical)

 

Thank you in advance for your help

 

Cordially

 

0 Kudos
Message 1 of 6
(1,613 Views)

If you are looking for some way to log the activity on the TCP channel you could simply write messages to a text file once you have them in memory, possibly prepending date and time for clarity. In case your messages are not text messages you will need to convert them someway tho make them readable, and it depends from the scope of your logging. To treat text messages you could either use ANSI C functions (fopen,fwrite, fclose) or their CVI counterparts (OpenFile, WriteFile, CloseFIle).

As an alternative, or at the same time, you could have a textbox or a listbox on screen and write messages there if you want to have an immediate feedback on screen.

 

I must admit I cannot fully understand the second part of your question: can you explain it better?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 6
(1,580 Views)

First of all , thank you for your help, I will try what you suggested.

 

Here a screenshot about my second problem ( with a little explanation in comment).

 

 

0 Kudos
Message 3 of 6
(1,575 Views)

To face your second problem I suggest you to implement a state machine: when you receive the first message you switch to a separate status that handles all subsequent messages until some condition is met that switches back to the base state.

 

You could implement the state machine inside a timer or by calling it from the TCP callback: basically you need a status variable that holds the current working status of the machine: while entering the machine you execute the code corresponding the active status inside which you can change the active status value so that at the next execution the other code is run. If you want to implement it from the TCP callback I suggest you prepare the environment and call the status machine with a PostDeferredCall so that the TCP callback terminates as quick as possible.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 4 of 6
(1,569 Views)

Thanks for your help , I will try to do the finite state machine later.

 

I managed to write the messages sent by the client in a txt file with OPENFILE as you had suggested. But I have some questions about that:

 

1- My openfile function is the following : 

 

fH = OpenFile ("logFile.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
WriteFile (fH, fgTesteur.Thread_serveur.message_client , strlen (fgTesteur.Thread_serveur.message_client));
fH = OpenFile ("logFile.txt", VAL_READ_ONLY, VAL_APPEND, VAL_ASCII);
CloseFile (fH);

 

I want to introduce a linefeed in my text file beetween each message , but where i put the "\n" in the code ?

 

2-I would want to read the message in the text file. I know I have to use VAL_READ_ONLY but I don't get anything back. The message will be stored in fH ? How can we choose the specific line  where we want to read in the text file ? Or maybe scan file contents.

 

 

3-A question a little apart but that I was able to observe:

 

you suggested to write the message in a text box or a screen (like the sample). It's a good idea but I had a problem when I tried to recover the differents messages with GetCrtlVal.

 

I only get the first message each time.

 

How to make GetCtrlVal understand that it is necessary to read at such line of the listbox and not only the first one as I could observe

 

Thank you in advance

 

0 Kudos
Message 5 of 6
(1,519 Views)

1. Use WriteLine instead: it will automatically add a linefeed at the end of line

 

2. While you can open a file twice for reading and writing, the OS may defer file write buffering the file content for efficiency, so you are likely to miss the last items written to it. You could try to use ANSI C functions (fopen, fputs, fclose) and try calling fflush to force writing to disk but I really wouldn't bet on the ability to immediately read back something from the file. And don't reuse the same variable to store the file handle in subsequent openings of the file!

 

3. The textbox can be addressed in lines (GetNumTextBoxLines, GetTextBoxLine and related functions) but it's not so efficient: better would be to use a listbox instead, which additionally permits you to align text in columns, highligt in colors and other interesting features: see the help for Item Label field in InsertListItem () command



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 6
(1,513 Views)