Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto monitor COM port and save contents to text file

Status
Not open for further replies.

351cj

Technical User
Jan 25, 2007
4
US
I have a automation task that I am not able to figure out yet. I have two computers connected by a serial cable. One is networked and the other is stand alone (vision gage). I am having difficulties automating the task to monitor COM port #2, capture the contents and then save results to a text file called OGP Data.txt. The attached file is what I have modified from a existing script to get to where I am at and it does perform the task with the missing piece being that I want the script to constantly run or monitor Com port # 2 and save the text file without the user having to do anything after the initial startup.

;Receive.WAS

;*****************************************************************************
;* *
;* Receive.WAS *
;* This script file receives data from COM2 and saves it to download *
;* folder. *
;* *
;*****************************************************************************


;********************************************************************
;* GLOBAL VARIABLES *
;********************************************************************
string DownloadPath ;holds the download path
string TempFile ;holds file to be printed


;********************************************************************
;* Main () *
;* *
;* Called by: None *
;* Modifies globals: None *
;* Calls: Initialize(), Cleanup() *
;********************************************************************
proc main
integer XferProgress ;holds the transfer status

Initialize() ;init parameters
getfile ASCII TempFile ;download file using ASCII
XferProgress=$XFERSTATUS ;init transfer status
while XferProgress==1 ;while transfer in progress
yield ;yield CPU time
XferProgress=$XFERSTATUS ;update transfer status
endwhile
rxflush ;clear receive buffer
txflush ;clear transmit buffer
pause 2
if XferProgress==2 ;file transfer was successful
else
usermsg "Failed to download file, try again..."
Cleanup()
endif
endproc


;********************************************************************
;* Initialize () *
;* This procedure inits the Tempfile name and deletes if it is *
;* present on local harddrive *
;* Called by: main *
;* Modifies globals: TempFile *
;********************************************************************
proc Initialize

fetch dnldpath DownloadPath ;get the download path
strfmt TempFile "%s\OGP_Data.txt" DownloadPath ;set file name
if isfile TempFile ;if file is present
delfile TempFile ;delete it
endif
endproc
 
If everything is working OK except the continual running, then you should be able to wrap the main script in a while 1/endwhile loop to do that. The only possible sticky point I can see is if the ASCII download times out due to data not being sent by the other system. The default timeout is 10 seconds, but that can be changed in Procomm's Setup dialog (Options | Data Options | Transfer Protocol) or via the proper ASPECT set command if necessary.

 
Thanks for the response. Since I am new to scripts, and not familiar with the while 1/endwhile loop, I am wondering if it is possible to create two scripts that could work together. The first script would monitor the COM port and it will always stay open waiting until it senses activity on the COM port. As soon as it senses activity on the COM port, it will execute a second script to capture the contents and save it to a text file.
 
To go back a second, the while 1/endwhile loop would just be placed around the appropriate commands in your existing script and that will keep it running indefinitely. I don't know if that solution would work for you, but you could try by putting a while 1 before the call to the Initialize procedure and the matching endwhile prior to the endproc at the end of the main procedure.

As for your latest message, you should be able to do something like what you want by using when $RXDATA to call a procedure or separate script using the execute command. The master script would look something like this:

proc main
when $RXDATA call scriptname

while 1
yield
endwhile
endproc

$RXDATA can be a little moody at times if memory serves me right, so you might want to reference the help file discussion of that system variable.

 
The while 1 is working well. Thanks for all your help so far. I think that I am down to one last piece of functionality.

Application: I have a CMM type gage sending out the data thru the COM port, Procomm capturing the data and saving to text file on the network, and then a SPC software stripping out the data from the text file. The SPC software needs to look for the same file name each time.

Do you know of a way to append new incoming data from the COM port to the existing text file in case the SPC software does not strip out the data?
 
Are there one or two text files involved here? I'm a little unclear on that. If you have an open capture file, you can use the capturestr command to write a string to it. If the file is not open, I have a couple sample scripts that show how to use the finsblock/fdelblock commands to add text to an existing file on the samples page of my site.

 
Thanks again for feedback. To clarify, there is one text file that I am working with and it is called OGP_Data.txt. I am trying to have Procomm create the file called OGP_Data.txt and then close it so that the SPC software can access it and strip the data from it. If the SPC software does not strip the data, I am looking to have Procomm append new data from the COM port to the end of the existing text file and then close it. The process cycles.

I have looked at the sample scripts and will look at the finsblock/fdelblock commands.
 
OK, so what you need to do is have Procomm append to that capture file instead of overwriting it (this is an option with the capture files)? If so, you can use the set capture overwrite OFF command at the beginning of your script to set this.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top