//*********************************************************
// Labwindows/cvi8.5 RS232与电子天平的通讯
// szlihongtao
// 2010-06-20
// 参照 王建新的<Labwindows/cvi测试技术及工程应用> page189
// 稍有改写
//*********************************************************
#include <userint.h>
#include <rs232.h>
#include <utility.h>
#include <ansi_c.h>
#include <formatio.h>
#include "RS232实例.h"
//*********************************************************
static int panelHandle;
static int ComPort = 1;
static void *callbackdata;
void ComCallback(int COMport, int eventMask, void *callbackdata);
//*********************************************************
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "RS232实例.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
OpenComConfig (ComPort, "", 9600, 0, 8, 1, 32767, 32767); //打开并配置串口
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
//*********************************************************
int CVICALLBACK rs232quit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
CloseCom (ComPort);
QuitUserInterface (0);
break;
}
}
return 0;
}
//*********************************************************
int CVICALLBACK rs232receive (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
//给串口Com1安装指定的回调函数,当输入队列满50个字符时产生中断
InstallComCallback (ComPort, LWRS_RECEIVE, 50, 0, ComCallback, 0);
//设置Timer的Enable属性为1,开始计时
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 1);
SetCtrlAttribute (panelHandle, PANEL_CMD_232_RECEIVE, ATTR_DIMMED, 1);
break;
}
}
return 0;
}
//*********************************************************
//将接收到的数据储存在指定文件中
//*********************************************************
int CVICALLBACK timerweight (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char *time;
char *date;
char filename[260];
char value[256];
char weigh[20];
double weightvlaue;
int FileHandle;
switch (event)
{
case EVENT_TIMER_TICK:
{
date="\0"; //初始化指针
time="\0";
date = DateStr (); //获得系统当前时间和日期
time = TimeStr ();
filename[0]=0; //初始化指针
//GetProjectDir (filename); //以下代码为创建一个文件路径
//strcat (filename, "\\");
strcat (filename, date);
strcat (filename, "-");
strcat (filename, "weight.txt");
//获得天平重量值
GetCtrlVal (panelHandle, PANEL_NUMERICGAUGE, &weightvlaue);
Fmt(weigh,"%s<%f ", weightvlaue);
strcpy(value,date);
strcat(value," ");
strcat(value,time);
strcat(value," ");
strcat(value,weigh);
//将文本内容加上换行与回车
strcat (value, "\r\n"); // 注意这句话,已经修正,原来的程序有误
strcat (value, "\r\n"); // 注意这句话,已经修正,原来的程序有误
//写入到文件中
FileHandle = OpenFile (filename, VAL_READ_WRITE, VAL_APPEND, VAL_BINARY);
WriteFile (FileHandle,value ,strlen (value) );
CloseFile (FileHandle);
break;
}
}
return 0;
}
//*********************************************************
//Com1端口中断回调函数
//*********************************************************
void ComCallback(int COMport, int eventMask, void *callbackdata)
{
int inputqueuelength;
int bytesread;
char buffer[512];
char *matchedchar = 0;
double value;
int i;
inputqueuelength = GetInQLen (ComPort); //获得指定端口输入队列字符数
bytesread = ComRd (ComPort, buffer, inputqueuelength); //读取输入队列中字符串
for (i = 0; i <= bytesread; i ++)
{
//当字符为"+"(0x2B)时,执行以下代码
if (buffer == 0x2B)
{
//将matchedchar的指针对准字符"+"的下一位,即指向接收的数据
matchedchar = buffer + i + 1;
//将字符串格式数据转化为双精度格式
Fmt (&value, "%f<%s", matchedchar);
SetCtrlVal (panelHandle, PANEL_NUMERICGAUGE, value);
FlushInQ (ComPort); //清空指定端口的输入队列
}
break;
}
}
//*********************************************************
//*********************************************************
//*********************************************************
用户1028624 2014-3-20 08:40
用户1585902 2012-7-16 22:41