暂时注释掉dfs样例,增加读取文件内容

This commit is contained in:
james 2024-07-31 08:52:40 +08:00
parent 880f89d634
commit 318f8a8fcd
2 changed files with 114 additions and 85 deletions

View File

@ -1,99 +1,99 @@
//记得在menuconfig中开启支持旧版本功能Support legacy version // //记得在menuconfig中开启支持旧版本功能Support legacy version
#include <board.h> // #include <board.h>
#include <rtthread.h> // #include <rtthread.h>
#include <drv_gpio.h> // #include <drv_gpio.h>
#include <dfs_posix.h>//需要添加软件包进这里 // #include <dfs_posix.h>//需要添加软件包进这里
//定义要写入的内容 // //定义要写入的内容
char String[] = "Hello, RT-Thread.Welcom to RSOC!"; // char String[] = "Hello, RT-Thread.Welcom to RSOC!";
//定义接受文件内容的缓冲区 // //定义接受文件内容的缓冲区
char buffer[100] = {}; // char buffer[100] = {};
void FileSystem_Test(void *parameter) // void FileSystem_Test(void *parameter)
{ // {
//文件描述符 // //文件描述符
int fd; // int fd;
//用只写方式打开文件,如果没有该文件,则创建一个文件 // //用只写方式打开文件,如果没有该文件,则创建一个文件
fd = open("/fal/FileTest.txt", O_WRONLY | O_CREAT); // fd = open("/fal/FileTest.txt", O_WRONLY | O_CREAT);
//如果打开成功 // //如果打开成功
if (fd >= 0) // if (fd >= 0)
{ // {
//写入文件 // //写入文件
write(fd, String, sizeof(String)); // write(fd, String, sizeof(String));
rt_kprintf("Write done.\n"); // rt_kprintf("Write done.\n");
//关闭文件 // //关闭文件
close(fd); // close(fd);
} // }
else // else
{ // {
rt_kprintf("File Open Fail.\n"); // rt_kprintf("File Open Fail.\n");
} // }
//用只读方式打开文件 // //用只读方式打开文件
fd = open("/fal/FileTest.txt", O_RDONLY); // fd = open("/fal/FileTest.txt", O_RDONLY);
if (fd>= 0) // if (fd>= 0)
{ // {
//读取文件内容 // //读取文件内容
rt_uint32_t size = read(fd, buffer, sizeof(buffer)); // rt_uint32_t size = read(fd, buffer, sizeof(buffer));
if (size < 0) // if (size < 0)
{ // {
rt_kprintf("Read File Fail.\n"); // rt_kprintf("Read File Fail.\n");
return ; // return ;
} // }
//输出文件内容 // //输出文件内容
rt_kprintf("Read from file test.txt : %s \n", buffer); // rt_kprintf("Read from file test.txt : %s \n", buffer);
//关闭文件 // //关闭文件
close(fd); // close(fd);
} // }
else // else
{ // {
rt_kprintf("File Open Fail.\n"); // rt_kprintf("File Open Fail.\n");
} // }
} // }
//导出命令 // //导出命令
MSH_CMD_EXPORT(FileSystem_Test, FileSystem_Test); // MSH_CMD_EXPORT(FileSystem_Test, FileSystem_Test);
static void readdir_sample(void) // static void readdir_sample(void)
{ // {
DIR *dirp; // DIR *dirp;
struct dirent *d; // struct dirent *d;
/* 打开 / dir_test 目录 */ // /* 打开 / dir_test 目录 */
dirp = opendir("/fal"); // dirp = opendir("/fal");
if (dirp == RT_NULL) // if (dirp == RT_NULL)
{ // {
rt_kprintf("open directory error!\n"); // rt_kprintf("open directory error!\n");
} // }
else // else
{ // {
/* 读取目录 */ // /* 读取目录 */
while ((d = readdir(dirp)) != RT_NULL) // while ((d = readdir(dirp)) != RT_NULL)
{ // {
rt_kprintf("found %s\n", d->d_name); // rt_kprintf("found %s\n", d->d_name);
} // }
/* 关闭目录 */ // /* 关闭目录 */
closedir(dirp); // closedir(dirp);
} // }
} // }
/* 导出到 msh 命令列表中 */ // /* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(readdir_sample, readdir sample); // MSH_CMD_EXPORT(readdir_sample, readdir sample);
/* // /*
#define WIFI_CS GET_PIN(F, 10) // #define WIFI_CS GET_PIN(F, 10)
void WIFI_CS_PULL_DOWM(void) // void WIFI_CS_PULL_DOWM(void)
{ // {
rt_pin_mode(WIFI_CS, PIN_MODE_OUTPUT); // rt_pin_mode(WIFI_CS, PIN_MODE_OUTPUT);
rt_pin_write(WIFI_CS, PIN_LOW); // rt_pin_write(WIFI_CS, PIN_LOW);
} // }
INIT_BOARD_EXPORT(WIFI_CS GET_PIN); // INIT_BOARD_EXPORT(WIFI_CS GET_PIN);
*/ // */

View File

@ -21,6 +21,9 @@ int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]);
uint64_t HAL_UptimeMs(void); uint64_t HAL_UptimeMs(void);
int HAL_Snprintf(char *str, const int len, const char *fmt, ...); int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
//定义接受文件内容的缓冲区
char buffer[100] = {};
#define GPIO_LED_B GET_PIN(F,11) #define GPIO_LED_B GET_PIN(F,11)
#define GPIO_LED_R GET_PIN(F,12) #define GPIO_LED_R GET_PIN(F,12)
@ -96,6 +99,7 @@ static int example_subscribe(void *handle)
HAL_Free(topic); HAL_Free(topic);
return 0; return 0;
} }
void make_file(char *String) void make_file(char *String)
{ {
//文件描述符 //文件描述符
@ -103,7 +107,7 @@ void make_file(char *String)
//用只写方式打开文件,如果没有该文件,则创建一个文件 //用只写方式打开文件,如果没有该文件,则创建一个文件
fd = open("/fal/test/Data.txt", O_WRONLY | O_CREAT); fd = open("/fal/test/Data.txt", O_WRONLY | O_CREAT);
// rt_kprintf("\n%f %f tmp:%s\n",Humi,Temp,String);
//如果打开成功 //如果打开成功
if (fd >= 0) if (fd >= 0)
{ {
@ -119,6 +123,31 @@ void make_file(char *String)
{ {
rt_kprintf("File Open Fail.\n"); rt_kprintf("File Open Fail.\n");
} }
//用只读方式打开文件
fd = open("/fal/test/Data.txt", O_RDONLY);
if (fd>= 0)
{
//读取文件内容
rt_uint32_t size = read(fd, buffer, sizeof(buffer));
if (size < 0)
{
rt_kprintf("Read File Fail.\n");
return ;
}
//输出文件内容
rt_kprintf("Read from file test.txt : %s \n", buffer);
//关闭文件
close(fd);
}
else
{
rt_kprintf("File Open Fail.\n");
}
return; return;
} }
char tmp[256]; char tmp[256];
@ -129,9 +158,9 @@ void tmp_payload(void)
Humi = aht10_read_humidity(Dev); Humi = aht10_read_humidity(Dev);
Temp = aht10_read_temperature(Dev); Temp = aht10_read_temperature(Dev);
sprintf(tmp, "Temp:%f ; Humi:%f ; Count: %d", Temp, Humi,++cnt); sprintf(tmp, "Temp:%f ; Humi:%f ; Count: %d", Temp, Humi,++cnt);
// rt_kprintf("\n%f %f tmp:%s\n",Humi,Temp,tmp);
make_file(tmp); make_file(tmp);
sprintf(tmp, "{\"params\":{\"temperature\":%.2f,\"humidity\":%.2f}}", Temp, Humi); sprintf(tmp, "{\"params\":{\"temperature\":%.2f,\"humidity\":%.2f}}", Temp, Humi);
// rt_kprintf("\n%f %f tmp:%s\n",Humi,Temp,tmp);
return; return;
} }
static int example_publish(void *handle) static int example_publish(void *handle)