Subversion Repositories factorylink.valmet

Rev

Rev 4 | Blame | Compare with Previous | Last modification | View Log | Download

/*
 ******************************************************************************
 *  Copyright 1996 DeltaLink bv. All Rights Reserved.
 ******************************************************************************
 *
 * DeltaLink Valmet I/O File simulator
 *
 * File: val_file.c
 *
 *
 */

#pragma warning(disable: 4121 4214)
#define NOMSG             // typedef MSG and associated routines
#define _OLE2_H_          // no ole

#include  <windows.h>
#include  <stdio.h>
#include  <stdlib.h>
#include  <stdio.h>
#undef ERROR
#include  <flib.h>                /* FactoryLink definitions */


#include  <fl_utils.h>
#include  <modules.h>

#include  <mci3.h>                /* Mailbox Communication Interface definitions */
#include  <format3.h>             /* Mailbox Communication Interface definitions */

#include  <mci3_mt.h>             /* MCI multi-threaded extension */
#include  <serial.h>

#include  "val_ct.h"
#include  "val_fil.h"

/*----------------------------------------------------------
* FUNCTION: Fil_Receive() 
*
* PURPOSE: to recieve a string into a buffer, simulating
* I/O through a COM-port by actually reading from file
* The functions stops reading from file when the end of the 
* file is reached or when the line read contains
* '###'.
* The format of the file should be such that each character
* to be 'read' is placed on a single separate line. 
* The character is put there in hex-format.
* So for example, the file that looks like
*
* 02
* 1D
* ###
*
* will result in reading 2 characters, having a value of 2 and
* 29 respectively
----------------------------------------------------------- */
#pragma warning(disable: 4100)
int Fil_Receive(
  VAL_PHYS_DEV * phys_dev, 
  int bufsize, 
  unsigned char * buf , 
  int * received
)
{
#define COM_FILE "C:\\Com_File.Txt"
  static FILE * ComFile = NULL;

  // if the file is not yet open, open it
  if (ComFile == NULL)
  {
    ComFile = fopen(COM_FILE, "r");
  }

  if (ComFile == NULL)
    return FL_ERROR;

  // now read as many characters from file as requested or until
  // the end offile is reached
  *received = 0;
  while (!feof(ComFile) && ! ( (*received) == bufsize) )
  {
    char CurrentLine[100];
    int d;
    // read the current line
    if (fscanf(ComFile, "%s", CurrentLine) != 1)
      break;

    // stop if the stop-code is encountered
    if (strcmp(CurrentLine, "###") == 0)
      break;

    // convert the hex-string to a value

    sscanf(CurrentLine, "%x", &d);
    buf[*received] = (unsigned char) d;
    (*received)++;
  }

  // if we reached end of file, close the file and remove it, so
  // that we can place a new file
  if (feof(ComFile))
  {
    fclose(ComFile);
    remove(COM_FILE);
    ComFile = NULL;

  }
  return GOOD; /* OK */

} /* -- Fil_Receive() */
#pragma warning(default: 4100)