首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

LabWindows/cvi之RS-232串口通信编程源代码

LabWindows/cvi之RS-232串口通信编程源代码

包括rs232文件发送程序

232.h

/**************************************************************************/
/* LabWindows/CVI User Interface Resource (UIR) Include File */
/* Copyright (c) National Instruments 2006. All Rights Reserved. */
/* */
/* WARNING: Do not add to, delete from, or otherwise modify the contents */
/* of this include file. */
/**************************************************************************/

#include <userint.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Panels and Controls: */

#define PANEL 1
#define PANEL_OKBUTTON_2 2 /* callback function: receivefile */
#define PANEL_OKBUTTON 3 /* callback function: receivefilename */
#define PANEL_STRING_2 4
#define PANEL_QUITBUTTON 5 /* callback function: QuitCallback */
#define PANEL_STRING 6
#define PANEL_DECORATION_2 7
#define PANEL_COMMANDBUTTON 8 /* callback function: filesel */
#define PANEL_DECORATION 9
#define PANEL_TEXTMSG 10
#define PANEL_TEXTMSG_2 11


/* Menu Bars, Menus, and Menu Items: */

/* (no menu bars in the resource file) */


/* Callback Prototypes: */

int CVICALLBACK filesel(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
int CVICALLBACK QuitCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
int CVICALLBACK receivefile(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
int CVICALLBACK receivefilename(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);


#ifdef __cplusplus
}
#endif

#include <ansi_c.h>
#include <utility.h>
#include <rs232.h>
#include <cvirte.h>
#include <userint.h>
#include "232.h"

static int byteswritten;
static char filename[MAX_FILENAME_LEN];
static char pathname[MAX_PATHNAME_LEN];
static int panelHandle;

int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "232.uir", PANEL)) < 0)
return -1;

//打开并配置串口Com1
OpenComConfig (1, "", 57600, 1, 8, 1, 32767, 32767);

//设置通信超时时间
SetComTime (1, 5.0);

//禁止串口软件握手
SetXMode (1, 0);

//禁止硬件握手
SetCTSMode (1, LWRS_HWHANDSHAKE_OFF);
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}

int CVICALLBACK sendfilename (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int comstatus;
int outputqueuelen;
switch (event)
{
case EVENT_COMMIT:
strcat (filename, "\r");

//向Com1写入文件名字符串
byteswritten = ComWrt (1, filename, strlen(filename));
break;
}
return 0;
}

int CVICALLBACK QuitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:

//关闭串口Com1
CloseCom (1);
QuitUserInterface (0);
break;
}
return 0;
}

int CVICALLBACK filesel (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int selstatus;
switch (event)
{
case EVENT_COMMIT:
filename[0] = '\0';
selstatus = FileSelectPopup ("", "*.*", "*.*", "打开文件", VAL_LOAD_BUTTON, 0, 0, 1, 1, pathname);
if (selstatus >= 0)
{
SetCtrlVal (panelHandle, PANEL_STRING, pathname);

//获得文件名
SplitPath (pathname, NULL, NULL, filename);
}
break;
}
return 0;
}

int CVICALLBACK sendfile (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int outputqueuelen;
switch (event)
{
case EVENT_COMMIT:

//设置串口Com1调制解调器参数
XModemConfig (1, 10.0, 10, 5.0, 1024);

//设置从串口Com1发送文件数据
XModemSend (1, pathname);

//获得串口Com1输出队列的字符串数目
outputqueuelen = GetOutQLen (1);
if (outputqueuelen == 0)
{
MessagePopup ("文件传输", "文件传输完毕!");
}
break;
}
return 0;
}

接收程序

#include "toolbox.h"
#include <ansi_c.h>
#include <rs232.h>
#include <cvirte.h>
#include <userint.h>
#include "232.h"

static int bytesread;
static char filename[MAX_PATHNAME_LEN];
static char pathname[MAX_PATHNAME_LEN];
static int panelHandle;

int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "232.uir", PANEL)) < 0)
return -1;

//打开并配置串口Com2
OpenComConfig (2, "", 57600, 1, 8, 1, 32767, 32767);

//设置通信超时时间
SetComTime (2, 5.0);

//禁止串口软件握手
SetXMode (2, 0);

//禁止硬件握手
SetCTSMode (2, LWRS_HWHANDSHAKE_OFF);
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}

int CVICALLBACK receivefilename (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
filename[0] = '\0';
SetCtrlVal (panelHandle, PANEL_STRING, "");

//读取字符串直到回车符出现
bytesread = ComRdTerm (2, filename, 260, 13);

//当出现回车符后,在其后加上结束符
filename[bytesread]= '\0';
SetCtrlVal (panelHandle, PANEL_STRING, filename);
break;
}
return 0;
}

int CVICALLBACK QuitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:

//关闭串口Com2
CloseCom (2);
QuitUserInterface (0);
break;
}
return 0;
}

int CVICALLBACK filesel (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int selstatus;
switch (event)
{
case EVENT_COMMIT:
selstatus = DirSelectPopup ("", "保存文件", 1, 1, pathname);
if (selstatus)
{
strcat (pathname, "\\");
strcat (pathname, filename);
SetCtrlVal (panelHandle, PANEL_STRING_2, pathname);
}

break;
}
return 0;
}

int CVICALLBACK receivefile (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int result;
int filesize;
int inputqueuelen;
FILE *stream;
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal (panelHandle, PANEL_STRING_2, pathname);

//判断文件是否存在
result = FileExists (pathname, &filesize);
if (!result)
{
stream = fopen (pathname, "wb+");
fclose (stream);
}

//设置串口Com2调制解调器参数
XModemConfig (2, 10.0, 10, 5.0, 1024);

//设置从串口Com2接收文件数据
XModemReceive (2, pathname);

//获得串口Com2输入队列的字符串数目
inputqueuelen = GetInQLen (2);
if (inputqueuelen == 0)
{
MessagePopup ("文件保存", "文件保存完毕!");
}
break;
}
return 0;
}

未来是我们的
谢谢了!
QUOTE:
以下是引用hpqian在2008-11-3 16:39:00的发言:
谢谢了!

建议到www.cpubbs.com看看,那的资料不少,人气也挺旺的

欢迎访问我的博客
http://blog.sina.com.cn/mylabview
labview开发工具及翔实资料(正版破解)

谢谢了!
学习学习
怎么判断发送完毕了可以发下一组数据了呢?输出输入阵列是在发送和接收完毕后自动为空吗?
让我学习了很多
好帖子让我学习了不少!
学习一下~~
学习学习。。。
返回列表