原创 Labwindows/cvi8.5 采用文件的方式保存/恢复参数

2010-6-13 15:01 3936 9 9 分类: 软件与OS

//*********************************************************
// Labwindows/cvi8.5  采用文件的方式保存/恢复参数
// szlihongtao
// 2010-06-13
//******************************************************************************
#include <ansi_c.h>
#include <formatio.h>
#include <userint.h>
#include "sample1.h"
//*********************************************************
static int panelHandle;
int File_Handle;
unsigned short int arr[8];
unsigned int cnt_test;
FILE *fp;
//*********************************************************
void restore(void)
{
 fp=fopen ("test1.bin", "r");
 if (fp==NULL)
 {
  MessagePopup ("友情提示", "文件不存在,需要创建!");
  fp = fopen ("test1.bin", "wb+");


  arr[0]=3000;    /* 默认值 */
  arr[1]=2000;
  arr[2]=5000;
 }
 else
 {
    fread (arr, 2, 3, fp);
    //fread (arr, 1, 6, fp); 这样写也是可以的


    ++cnt_test;
 }


 SetCtrlVal (panelHandle, PANEL_VOLT, arr[0]);
 SetCtrlVal (panelHandle, PANEL_CURRENT, arr[1]);
 SetCtrlVal (panelHandle, PANEL_FREQ, arr[2]);


 fclose(fp);
}
//*********************************************************
int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "sample1.uir", PANEL)) < 0)
  return -1;
 DisplayPanel (panelHandle);
 restore();    /* 恢复保存的数据 */
 RunUserInterface ();
 DiscardPanel (panelHandle);
 return 0;
}
//*********************************************************
int CVICALLBACK func_save (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 FILE *fp;


 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlVal (panelHandle, PANEL_VOLT, &arr[0]);
   GetCtrlVal (panelHandle, PANEL_CURRENT, &arr[1]);
   GetCtrlVal (panelHandle, PANEL_FREQ, &arr[2]);


   fp=fopen ("test1.bin", "wb+");/* 删除以前的数据,重新打开 */


   #if 0      /* 2种写法都是可以的 */
    fputc(arr[0]%256,fp); /* 先保存低位 */
    fputc(arr[0]/256,fp);


    fputc(arr[1]%256,fp);
    fputc(arr[1]/256,fp);


    fputc(arr[2]%256,fp);
    fputc(arr[2]/256,fp);
   #else
       fwrite (arr, 2, 3, fp);
   #endif


   fclose(fp);
   
   SetCtrlAttribute (panelHandle, PANEL_CMD_SAVE, ATTR_DIMMED, 1);
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK func_quit (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   QuitUserInterface (0);
   break;
 }
 return 0;
}
//*********************************************************
int CVICALLBACK func_enable (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   SetCtrlAttribute (panelHandle, PANEL_CMD_SAVE, ATTR_DIMMED, 0);
   break;
 }
 return 0;
}
//*********************************************************
//*********************************************************
//*********************************************************



 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
9
关闭 站长推荐上一条 /3 下一条