[分享]LabWindows/cvi之RS-232串口通信编程源代码
  • 回复 发帖

kanghuiwy发帖:129
发送消息  该帖被浏览 4098 次,回复 0 次
发表于:2009-08-07 00:00
楼主

包括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] = '';
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] = '';
SetCtrlVal (panelHandle, PANEL_STRING, "");

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

//当出现回车符后,在其后加上结束符
filename[bytesread]= '';
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;
}

 

 

喜欢的朋友别忘 顶贴哦~!!



 
       
     
您需要登录后才可以回复 登录 | 立即注册

 
发帖说明:如果不能直接拷贝粘贴到编辑框中,请在编辑框中点击右键选粘贴也可粘贴拷贝内容!