#include "pointer_in.h"
#ifdef pointer
int pointer_test(void)
{
gef_if_serial_init();
while(1)
{
gef_if_s0_send(master_serial0);
}
return 0;
}
int x;
int gef_if_serial_init(void)
{
int len=60;
x=sizeof(gef_if_test);
#ifdef MALLOC
master_serial0=(serial_head_t *)malloc(len);
if(master_serial0==NULL)
{
return -1;
}
memset(master_serial0,0,len);
#else
master_serial0=(serial_head_t *)calloc(1,len);
if(master_serial0==NULL)
{
return -1;
}
#endif
memset(master_serial0,0x33,6);
memset((unsigned char *)master_serial0+6,0x99,6);
master_serial0->mac.type=0x8907;
len=len+1;
len=len-1;
return 0;
}
int gef_if_s0_send(serial_head_t *p)
{
gef_if_serial_frame_t gef_frame_p;
p->id=0x11;
p->subid=2;
gef_frame_p.id=0x11;
gef_frame_p.len=60;
gef_frame_p.flag=1;
gef_frame_p.payload=(serial_head_t *)p;
gef_if_serial_send(&gef_frame_p);
return 0;
}
int gef_if_serial_send(gef_if_serial_frame_t *p)
{
uart_send(p->payload,p->len,p->flag);
return 0;
}
int uart_send(unsigned char *data,int len,int flag)
{
switch(flag)
{
case 1:uart_send_1(data,len);break;
case 2:uart_send_2(data,len);break;
case 3:uart_send_3(data,len);break;
default:
SerPort_SendBuf(COM1,"flag err!\r\n",10);
}
return 0;
}
int uart_send_1(unsigned char *data,int len)
{
while(len--)
{
u_send(data++);
delay(10000);
}
return 0;
}
int uart_send_2(unsigned char *data,int len)
{
while(len--)
{
u_send(data++);
delay(20000);
}
return 0;
}
int uart_send_3(unsigned char *data,int len)
{
while(len--)
{
u_send(data++);
delay(30000);
}
return 0;
}
int u_send(unsigned char *data)
{
#ifdef RS_485
rs485_tx();
Led_Ctl(LED_485, 1);
UART1_SendByte(*data);
rs485_rx();
#else
UART0_SendByte(*data);
#endif
return 0;
}
#endif
1、定义一个结构体,该结构体有很多的描述符,在各层函数中,通过判断这些描述符来设定具体的一些参数。
2、该结构体中有个空指针,指向一个数据区的其实地址(被指定的数据区是一个已经开辟好的动态内存区)
3、malloc以及calloc申请动态内存后,一定要判断是否申请成功,否则return -1.
4、定义一个指针,不管是什么类型的指针,例如char *p,这个指针占用的内存空间都是4个字节。
例如计算gef_if_test的大小应该是16.
5、注意calloc的使用,calloc(1,len),开辟一块大小为len的内存区。应该包含头文件“stdlib.h”
在头文件中一些类型的定义和函数的声明:
#ifndef _POINTER_OUT_H
#define _POINTER_OUT_H
typedef struct
{
unsigned int msgid;
unsigned int type;
int len;
void *payload;
}gef_if_fast_data_t;
typedef struct
{
unsigned char dst[6];
unsigned char src[6];
unsigned short type;
}mac_head_t;
typedef struct
{
mac_head_t mac;
unsigned char id;
unsigned char subid;
}serial_head_t;
typedef struct
{
int id;
int len;
int flag;
void *payload;
}gef_if_serial_frame_t;
typedef struct
{
int a;
char *b;
int c;
void *payload;
}gef_if_test;
extern int pointer_test(void);
extern int gef_if_serial_init(void);
extern int gef_if_s0_send(serial_head_t *p);
extern int gef_if_serial_send(gef_if_serial_frame_t *p);
extern int uart_send(unsigned char *data,int len,int flag);
extern int uart_send_1(unsigned char *data,int len);
extern int uart_send_2(unsigned char *data,int len);
extern int uart_send_3(unsigned char *data,int len);
extern int u_send(unsigned char *data);
#endif
阅读(1500) | 评论(0) | 转发(0) |