| 网站首页 | 新闻 | SOPC | FPGA | DSP | ARM | 嵌入式操作系统 | 下载 | 所有产品 | 留言 | 论坛 | 购买指南 | 网络协议 | 驱动设计 | 
您现在的位置: 21嵌入式控制研究室 >> 嵌入式操作系统 >> Vxworks >> 文章正文 用户登录 新用户注册
VxWorks系统编程==例子       ★★★ 【字体:
VxWorks系统编程==例子
作者:佚名    文章来源:21control    点击数:    更新时间:2005-12-22
VxWorks系统函数loadModule()程序示例

loadModule()在VxWorks中用来加载.o,.out文件,然后用moduleFindByName()来找到符号表中固定入口函数所在的位置。
可以参考usrLib.h和moduleLib.h来获得详细信息。



#include
#include
#include
#include
#include
#include
#include
#include


extern SYMTAB_ID sysSymTbl ;

int loadTestModuleAndRun()
{
int fd = ERROR ;
int status = ERROR ;
MODULE_ID hModule ;
FUNCPTR taskEntry = NULL ;
SYM_TYPE * pType ;

fd = open("/sd0/test.out",O_RDONLY,0) ;

if (fd==ERROR)
{
printf("can not open binary file.\n") ;
return ERROR ;
}
else
{
printf("binary file opened.\n") ;
}

if ((hModule=loadModule(fd,LOAD_ALL_SYMBOLS))==NULL)
{
printf("loadModule error = 0x%x.\n",errno) ;
return ERROR;
}

close(fd) ;

status = symFindByName(sysSymTbl,"test",
(char **)&taskEntry,pType ) ;

if (status==ERROR)
{
printf("symFindByName error=%d\n", errno) ;
return ERROR;
}
else
{
/* Type N_ABS=2,N_TEXT=4,N_DATA=6,N_BSS=8;N_EXT=1 */

printf("taskEntryr=0x%x, type=%d\n.",
(int)taskEntry,(int)*pType);
}

status = taskSpawn("test",100,0,30000,taskEntry,
     0,0,0,0,0,0,0,0,0,0) ;

if (status==ERROR)
{
printf("taskSpawn error=%d\n",errno) ;
return ERROR;
}

return OK ;
}



 

基于RAM的文件系统的实现

如果的系统上安装有硬盘,则在BSP中装载你的硬盘驱动即可。如果没有,建议建立基于RAM的文件系统。建立RAM文件系统的方式如下:


#ifndef RAMDISK_VERSION
#define RAMDISK_VERSION "1.0 built by tiefeng@vip.sina.com"
#endif // RAMDISK_VERSION

#include
#include
#include
#include
#include
#include


/**********************************************************************

Function: Create a ram disk device
Parameters:
name -> device name, such as "ramdisk0:".
size -> block device size.
Returned:
The actualy disk size. Or ERROR.

**********************************************************************/

STATUS CreateRamDisk(char * name,int size)
{
int nBlock = NULL ;
BLK_DEV * pBlkDev = NULL ;
DOS_VOL_DESC * pVolDesc = NULL ;

// the disksize should be integral multiple of the blocksize.

size = size - size%512 ;
nBlock = size/512 ;

// You can simultaneously open 20 files

dosFsInit(20) ;

// Create a ram-disk.
// The base address is the return value of alloc.
// The block size is 512.
// nBlock blocks per track
// Total nBlock blocks.
// The base address offset is 0.

pBlkDev = ramDevCreate(0,512,nBlock,nBlock,0) ;
if (NULL==pBlkDev)
{
fprintf(stderr,"Can not create ram block device.\n") ;
return ERROR ;
}

// Make DOSFS by a ram block device.

pVolDesc = dosFsMkfs(name,pBlkDev) ;
if (NULL==pVolDesc)
{
fprintf(stderr,"Can not create ram-dos-fs.\n") ;
return ERROR ;
}

// The size is actualy disk size.

return size ;
}

/**********************************************************************

Function: Delete a ram disk device
Parameters:
name -> device name, such as "ramdisk0:".
Returned:
Return OK if the device is removed successfuly.
Otherwise return ERROR.

**********************************************************************/

STATUS DeleteRamDisk(char * name)
{
DEV_HDR * pDevHdr = NULL ;

// Find ram-disk device by name

if ( NULL==(pDevHdr=iosDevFind(name,NULL)) )
{
fprintf(stderr,"Can not find device (%s).\n",name) ;
return ERROR ;
}

// Delete the device and free the alloced memory

iosDevDelete(pDevHdr) ;
free(pDevHdr) ;

return OK ;
}

/**********************************************************************

Function: Create a ram disk device & set is as default path.
Parameters:
name -> device name, such as "ramdisk0:".
size -> block device size.
Returned:
The actualy disk size. Or ERROR.

**********************************************************************/

STATUS InitRamFsEnv(char * name,int size)
{
STATUS iReturn = CreateRamDisk(name,size) ;

if (ERROR!=iReturn) ioDefPathSet(name) ;

return iReturn ;
}


串口通信

在VxWorks中,串口作为设备出现,而在POSIX标准中,设备可以使用标准IO函数来进行操作。

在VxWorks Shell中查看,可以看到目前的端口设备名字,如下:

-> devs
drv name
 0 /null
 1 /tyCo/0 (这个是COM1)
 1 /tyCo/1
 1 /tyCo/2
 1 /tyCo/3
 5 omcr-2:
 6 /vio
value = 0 = 0x0
->

使用标准UNIX/IO打开这个设备,并设置baudrate,就可以使用read和write向端口读取或者写入数据。
势力程序如下:

打开VxWorks On COM1,在Terminal上可以看到输出。也可以在Terminal上输入(回车结束),在Console Window里可以看到输出。

Terminal的COM工作模式一般为LINE,即发现行结束标志(ENTER)才发送一次缓冲的数据。

这里VxWorks Host上的COM工作模式为RAW,无缓冲。

关于LINE和ROW模式的区别,请参考VxWorks Programmer's Guide。

完整的示例程序如下:

#include
#include
#include

int tyRecv( int fd )
{
char szBuf[2] = { 0 } ;
FOREVER
{
if ( NULL!=read(fd,szBuf,1) )
printf("%s",szBuf) ;
taskDelay(1) ;
}
}

int tySend( int fd )
{
FOREVER
{
write(fd,"A",1) ;
taskDelay(60) ;
}
}

int testMain()
{
// Open COM1

int fd = open("/tyCo/0",O_RDWR,0) ;

if (ERROR==fd)
{
printf("can not open device!\n") ;
return 0 ;
}

// Set baudrate

if ( ERROR==ioctl(fd,FIOBAUDRATE,9600) )
{
printf("can not set BAUDRATE!\n") ;
return 0 ;
}

// Start receiving task

taskSpawn("recv",60,VX_FP_TASK,
1024*40,(FUNCPTR)tyRecv,fd,
0,0,0,0,0,0,0,0,0
) ;

// Start sending task

taskSpawn("send",60,VX_FP_TASK,
1024*40,(FUNCPTR)tySend,fd,
0,0,0,0,0,0,0,0,0
) ;

FOREVER taskDelay(60) ;
}


 

Win32下协议编程

用snmp++发送get请求示例     

#include “snmp_pp.h”
#define SYSDESCR “1.3.6.1.2.1.1.1.0”   // Object ID for System Descriptor

void get_system_descriptor()
{
  int status;               // return status   
  CTarget ctarget( (IpAddress) “10.4.8.5”);// SNMP++ v1 target
  Vb vb( SYSDESCR);             // SNMP++ Variable Binding
  Pdu pdu;                  // SNMP++ PDU

  //-------[ Construct a SNMP++ SNMP Object ]---------------------------
  Snmp snmp( status);            // Create a SNMP++ session
  if ( status != SNMP_CLASS_SUCCESS)

{   // check creation status
   cout << snmp.error_msg( status);    // if fail, print error string
   return; 

}

  //-------[ Invoke a SNMP++ Get ]---------------------------------------
  pdu += vb;                 // add the variable binding
  if ( (status = snmp.get( pdu, ctarget)) != SNMP_CLASS_SUCCESS)
   cout << snmp.error_msg( status);
  else

{
   pdu.get_vb( vb,0);           // extract the variable binding
   cout << “System Descriptor = ”<< vb.get_printable_value(); 

} // print out

}; 


使用SNMP++,在VC环境下的配置

To compile your SNMP++ based application in VC, please consult the following rules:

Header files:

(这里是SNMP++的include目录的位置)
#include "C:\System Backup\D$\Snmp\Agent++\snmp++\include\snmp_pp.h"
#include "C:\System Backup\D$\Snmp\Agent++\snmp++\include\collect.h"
#include "C:\System Backup\D$\Snmp\Agent++\snmp++\include\notifyqueue.h"

Linked Libs:

snmpPP.lib deslib.lib agentpp.lib ws2_32.lib

Compiler options:

/nologo /MLd /W3 /Gm /GX /ZI /Od /I "C:\System" /I "C:\System Backup\D$\Snmp\Agent++\snmp++\include"(这里是你自己的SNMP++的include目录的位置) /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Fp"Debug/trap_recv.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /GZ




TCP/IP协议编程,UDP广播包发送接收示例

#include
// 这里例子使用了UdpSocket类,可以在下面下载
#include "UdpSocket.h"

const char szBufReq[40]=
{
'\x30','\x26','\x02','\x01','\x00',
'\x04','\x06','\x70','\x75','\x62',
'\x6c','\x69','\x63','\xa0','\x19',
'\x02','\x01','\x03','\x02','\x01',
'\x00','\x02','\x01','\x00','\x30',
'\x0e','\x30','\x0c','\x06','\x08',
'\x2b','\x06','\x01','\x02','\x01',
'\x01','\x01','\x00','\x05','\x00'
};

void ProcessSnmpGet(sockaddr_in & addr)
{
// 打印IP地址
printf("Answer from %s.",inet_ntoa(addr.sin_addr)) ;
// 下面添加发送GET消息的功能,偶没写
}

int main()
{
int i = 0 ;
char szBufRecv[2048] = { 0 } ;
sockaddr_in addr ;
int status = -1 ;

CUdpSocket udpObject ;
udpObject.Init(4700,"192.168.2.255",161) ;
udpObject.Send(40,(void *)szBufReq) ;

// 在五秒之内没得到回应的全认为是超时

while (i<5)
{
// 按照超时时间为0.001秒接收数据报
// 数据源的IP地址储存在addr中
status = udpObject.Recv(2048,szBufRecv,1,&addr) ;
if (status>0)
{
ProcessSnmpGet(addr) ;
continue ;
}
i += 1 ;
::Sleep(1000) ;
}

return 0 ;
}



 

文章录入:fengfeiyi    责任编辑:fengfeiyi 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
  • 嵌入式Linux开发工具选择和应…

  • 一步一步的制作arm-linux 交…

  • 高可用性系统的硬件和软件设…

  • 利用电子邮件实现与网络嵌入…

  • 什么样的处理器会引领嵌入式…

  • 配合Tornado使用的Wind Powe…

  • 嵌入式Internet技术及其应用…

  • 嵌入式Internet技术及其应用…

  • 嵌入式Internet技术及其应用…

  • 嵌入式Internet技术及其应用…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)