Skip to content

模块间通讯

全局变量

  • 通过extern让全局变量可以被外部文件调用

优点:调用简单直接

缺点:增加了文件之间的耦合性

  • 改进:增加接口对其访问
C
//module.h
void val_set(int value);
void val_get(void);

//module.c
int global_val;
void val_set(int value)
{
    global_val = value;
}
void val_get(void)
{
    return global_val;
}

回调函数

stdio.c

C
  1 #include <stdio.h>                                                                    
  2 int send_data(char *buf, int len)
  3 {
  4     char data[100];
  5     int i;
  6     for(i=0;i<len;i++)
  7         data[i] = buf[i];
  8     for(i=0;i<len;i++)
  9         printf("receive data[%d] = %d\n", i, data[i]);
 10     return len;
 11 }

main.c

C
  1 #include <stdio.h>                                                                    
  2 
  3 int send_data(char *buf,int len);
  4 
  5 int main(void)
  6 {
  7     char buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
  8     int return_data;
  9     return_data = send_data(buffer, 10);
 10     printf("send data len :%d\n", return_data);
 11     return 0;
 12 }

缺点:不能实现双向通讯

  • 解决方法:回调函数

module1.c

C
  1 #include <stdio.h>                                                                    
  2 
  3 void runcallback(void (*fp)(void))
  4 {
  5     fp();
  6 }
  7

module1.h

C
  1 //module.h                                                                            
  2 
  3 #ifndef __RUNCALLBACK__H
  4 #define __RUNCALLBACK__H
  5 void runcallback(void (*fp)(void));
  6 #endif

app.c

C
  1 #include <stdio.h>                                                                    
  2 #include "module1.h"
  3 
  4 void func1(void)
  5 {
  6     printf("func1 ...");
  7 }
  8 void func2(void)
  9 {
 10     printf("func2 ...");
 11 }
 12 
 13 int main(void){
 14     runcallback(func1);
 15     runcallback(func2);
 16     return 0;
 17 }
  • 示例2

device_manager.h

C
  1 #ifndef __STORAGE_DEVICE__H                                                           
  2 #define __STORAGE_DEVICE__H
  3 typedef int (* read_fp)(void);
  4 struct storage_device
  5 {
  6     char name[20];
  7     read_fp read;
  8 };
  9 extern int register_device(struct storage_device dev);
 10 extern int read_device(char *device_name);
 11 
 12 #endif

device_manager.h

C
  1 #include <stdio.h>                                                                    
  2 #include "device_manger.h"
  3 #include <string.h>
  4 //保存登记
  5 struct storage_device device_list[100] = {0};
  6 unsigned char num;//现有的登记的数据的数量
  7 //进行登记
  8 int register_device(struct storage_device dev)
  9 {
 10     device_list[num++] = dev;
 11     return 0;
 12 }
 13 int read_device(char *device_name)
 14 {//调用对应的函数
 15     int i;
 16     for(i=0;i<100;i++)
 17     {
 18         if(!strcmp(device_name,device_list[i].name))
 19             break;
 20     }
 21     if(i == 100)
 22     {
 23         printf("Error! can't find device: %s\n", device_name);
 24         return -1;
 25     }
 26     return device_list[i].read();
 27 }
 28 
 29

app.c

C
  1 #include <stdio.h>                                                                    
  2 #include "device_manger.h"
  3 
  4 
  5 int sd_read(void)
  6 {
  7     printf("sd read data ...\n");
  8     return 10;
  9 }
 10 
 11 int udisk_read(void)
 12 {
 13     printf("udisk read data ...\n");
 14     return 20;
 15 }
 16 struct storage_device sd = {"sdcard", sd_read};
 17 struct storage_device udisk = {"udisk", udisk_read};
 18 
 19 int main(void)
 20 {
 21     register_device(sd);
 22     register_device(udisk);
 23 
 24     read_device("udisk");
 25     read_device("udisk");
 26     read_device("sd");
 27     read_device("sdcard");
 28     read_device("sdcard");
 29     return 0;
 30 }

result:


udisk read data ...
udisk read data ...
Error! can't find device: sd
sd read data ...
sd read data ...