Board logo

标题: LabWindows/cvi之RS-232串口通信编程源代码 [打印本页]

作者: wolfhui    时间: 2008-11-1 13:55     标题: 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;
}


作者: hpqian    时间: 2008-11-3 16:39

谢谢了!
作者: fjczd    时间: 2008-12-29 09:00

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

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

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


作者: hpqian    时间: 2009-2-11 14:20

谢谢了!
作者: nzw39444282000    时间: 2009-3-4 23:40

学习学习
作者: 521slj    时间: 2009-5-26 12:14

怎么判断发送完毕了可以发下一组数据了呢?输出输入阵列是在发送和接收完毕后自动为空吗?
作者: fspace88    时间: 2011-6-8 13:03

首先 我是购物狂 我承认!
在网上购物那么多年了,平时有遇好的店铺也有差的,有购物到满意按时服务却都很一般,但是我都是给好评,但是心中还是觉得算了下次再找加好些的,很难买到真的服务贴心东西又好的店铺.

今天我又在淘宝网上淘衣服裤子时无意发现了一家快升皇冠的店铺, F-Space时尚空间 ,这家男装店,一进店就完全的吸引了我,汇聚全球最顶级时尚奢华的风格男装店,老板人非常好,服务贴心,价格也不贵,更重要的是整店现在还在全场打折优惠促销活动! 现在我给大家参考一下我买的一些新款宝贝,真的很不错.




夏季到了,我挑的这些宝贝真的非常满意,品质都很好,现在活动{满五件就打7折优惠,满三件8折,满两件8.8折,一件也可以享受包邮优惠},其它部分单品还有单独打折优惠,真的太吸引人了,买了那么多真的很满意,现在推荐给大家,喜欢时尚喜欢潮流的兄弟们,可以进来看看..真的好我才推荐!!!

店铺地址是: http://shop57733499.taobao.com
作者: 你你    时间: 2011-8-8 14:18

让我学习了很多
作者: 偶偶    时间: 2011-8-8 14:37

好帖子让我学习了不少!
作者: coconut_520    时间: 2011-8-30 15:18

学习一下~~
作者: dulala1    时间: 2011-11-19 19:07

学习学习。。。




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0