05-17-2010 02:33 AM
Hello,
I realize a project during my training course in France.
I must develop an communication with serial port.
I would like look for a text file and send this text file with serial port?
Thank you for your help,
Regards
Jérome
05-17-2010 02:51 AM - edited 05-17-2010 02:58 AM
Hello Jérome,
I can give you a conceptual framework for you to study on and to adapt to your actual needs; afterwards, if you have specific questions about some aspect we can deep into technical details.
Basically your application will have the following steps:
1. Locate the text file to handle
CVI offers some functions to browse system resources and locate the files: FileSelectPopup is the most flexible you can use, since it permits you to browse either local, removable and remote resources
2. Open and configure the serial port
OpenComConfig is the function that permits you to open the port with desired settings. Be sure to match speed, parity and so on with receiver settings. I suggest you to set -1 as the output queue size so to bypass windows internal buffers and write directly to the port: you will achieve a slightly more predictable behaviour in your program
3. Open the file
I personally prefere to use native CVI Formatting and I/O library functions, which I consider more user friendly with reference to error checking; in any case CVI offers both those functions (OpenFile) and standard ANSI C function (fopen): if you may have options to port this code to other platforms perhaps ANSI C code will be better
4. Create a loop to read a bunch of file and transmit to the port
Continuing with CVI library, you can use ReadFile to read a fixed amount of bytes from the file and ComWrt to send it to the serial port. The equivalent ANSI C function will be fgets.
Within the loop you may add instructions to read user input (e.g. cancel requests) and system events if you want
5. At file end, close the file (CloseFile / fclose) and the port (CloseCom) and that's all.
Don't forget to add error checking to your program: when dealing with disk and serial I/O this is a crucial aspect that differentiates a well developed program to a bad one!
Some informations on error checking can be found in the online help: look for "Checking for errors in CVI" item in the Programmer Reference help; additionally you may want to look to my contribution on this subject.
05-17-2010 03:02 AM
Thank you very much for your rapid answer !
I have taken the example in Labwondows for create the communication of serial port. I have taken serial like example.
And I want added the selection of text file and the send of this text file.
Can I have a bar of selection to choose the text file ? Because I dont see the function in the *.iur that I must use to choose the text file.
And after I create a buckle "while" to send all octet in my text file ?
Thank you for your help and sorry for my english!
Regards
05-17-2010 03:36 AM
05-17-2010 04:24 AM
Ah yes ok !! Thank you !
And when my file text is loaded, I have all character in my output it's true ?
I can send all character after ?
And I would like affich all character when I load, Can I use an Text Box or other function ?
05-17-2010 04:46 AM
FileSelectPopup simply returns you the complete pathname of the selected file, it does not actually opens it. It's up to you to do what you want on the file.
Depending on file size, after opening it you can read it completely or read in pieces (the loop I mentioned before).
Yes, you can display file contents in a textbox with ResetTextBox
It has come to my mind that you can use ComFromFile to perform your task: read the command instructions for a detailed explanation on how to use it.
Please remember that all serial functions are syncronous one: i.e. the system is freezed until they terminate. It is important to understand it since transferring large files over the serial channel may seriously impact over your program responsiveness. That is why I suggested you to split the transmission in a loop.
05-17-2010 07:12 AM
Ok ok Thank you !
I have programming this :
case EVENT_COMMIT:
retVal = FileSelectPopup("", "*.txt","*.txt", "Load Text File", VAL_LOAD_BUTTON, 0, 0, 1, 0, imageFileBuf);
if (retVal == VAL_EXISTING_FILE_SELECTED)
{
Status1 = OpenFile(imageFileBuf, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
But FileSelectPopup return the adress of text and no the text.
To recup the text, he exist a function for recup the text in a variable ?
Thank you
05-17-2010 07:30 AM
How have you defined imageFileBuf? It should be
char imageFileBuf[MAX_PATHNAME_LEN];
What does it mean "returns the address of the text and no the text"? When seeing a char[] variable in the variable window you should see both the address of the array and the string content.
05-17-2010 07:46 AM
Yes imageFileBuf is defined char imageFileBuf[MAX_PATHNAME_LEN].
But when I do ResetTextBox(panel_handle, PANEL_2_TEXTBOX_3, imageFileBuf), I see in my text box d:\Documents and Settings\... But I would like all characters contents in the text file.
05-17-2010 08:00 AM