//AT+FREQ=<value> sprintf(buf,"AT+FREQ=%u\r\n",rxdev->freq); printf("BUF =%s\n",buf); ret = uart_write(rxdev->uartfd, buf, strlen(buf)); if(ret != strlen(buf)) { printf("Uart write ap freq err\n"); } memset(recv_buf,0,sizeof(recv_buf)); read_len = read_datas_tty(rxdev->uartfd,recv_buf,6,500); if(strstr(recv_buf,"OK")==NULL) { printf("Set ap freq err\n"); } memset(buf,0,sizeof(buf)); strcpy(buf,"AT+RX=1\r\n"); ret = uart_write(rxdev->uartfd, buf, strlen(buf)); if(ret != strlen(buf)) { printf("Start uart rx err\n"); } memset(recv_buf,0,sizeof(recv_buf)); read_len = read_datas_tty(rxdev->uartfd,recv_buf,6,500); if(strstr(recv_buf,"OK")==NULL) { printf("Start uart rx err\n"); }
时间: 2024-03-28 10:40:46 浏览: 155
这段代码似乎是在使用串口通信设置某种设备的频率,其中 AT+FREQ=<value> 是设置频率的命令,后面的参数是具体的频率设置。其中 sprintf 函数是用来格式化字符串的,读取串口数据使用了 read_datas_tty 函数。最后判断是否设置成功,如果未成功则输出错误信息。接着使用 strcpy 函数将字符串 "AT+RX=1\r\n" 复制到 buf 中,然后通过 uart_write 函数将 buf 中的数据写入到串口中。最后再次使用 read_datas_tty 函数读取串口数据,并判断是否设置成功,如果未成功则输出错误信息。
相关问题
#include "dht11.h" void Delay_us(uint16_t delay) { __HAL_TIM_DISABLE(&htim3); __HAL_TIM_SET_COUNTER(&htim3,0); __HAL_TIM_ENABLE(&htim3); uint16_t curCnt=0; while(1) { curCnt=__HAL_TIM_GET_COUNTER(&htim3); if(curCnt>=delay) break; } __HAL_TIM_DISABLE(&htim3); } void DHT11_OUT(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } void DHT11_IN(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } void DHT11_Strat(void) { DHT11_OUT(); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET); HAL_Delay(20); HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET); Delay_us(30); } uint8_t DHT11_Check(void) { uint8_t retry = 0 ; DHT11_IN(); while(GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_8) && retry <100) { retry++; Delay_us(1);//1us } if(retry>=100) {return 1;} else retry = 0 ; while(GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_8) && retry<100) { retry++; Delay_us(1);//1us } if(retry>=100) {return 1;} return 0 ; } uint8_t DHT11_Read_Bit(void) { uint8_t retry = 0 ; while(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) && retry <100) { retry++; Delay_us(1); } retry = 0 ; while(GPIO_PIN_RESET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) && retry<100) { retry++; Delay_us(1); } Delay_us(40); if(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) return 1; else return 0 ; } uint8_t DHT11_Read_Byte(void) { uint8_t i , dat ; dat = 0 ; for(i=0; i<8; i++) { dat <<= 1; dat |= DHT11_Read_Bit(); } return dat ; } uint8_t DHT11_Read_Data(uint8_t* temp , uint8_t* humi) { uint8_t buf[5]; uint8_t i; DHT11_Strat(); if(DHT11_Check() == 0) { for(i=0; i<5; i++) { buf[i] = DHT11_Read_Byte(); } if(buf[0]+buf[1]+buf[2]+buf[3] == buf[4]) { *humi = buf[0]; *temp = buf[2]; } }else return 1; return 0 ; } void func_1() { uint8_t temperature = 1 ; uint8_t humidity = 1; uint8_t aTXbuf[32] ; while(1){ DHT11_Read_Data(&temperature , &humidity); sprintf((char*)aTXbuf,"%d , %d %% \r\n" ,temperature ,humidity); HAL_UART_Transmit(&huart1, aTXbuf, strlen((const char*)aTXbuf), 200); HAL_Delay(5000); } } int temperature_humidity_device_control(protocol_package_t *pk) { printf("temperature_humidity_device_control\r\n"); if(pk->function == 0x16 && pk->data[0] == 0x00) { printf("temperature_humidity_device_control success\r\n"); uint8_t temperature = 1 ; uint8_t humidity = 1; uint8_t aTXbuf[32] ; //DHT11_Read_Data(&temperature , &humidity); pk->data[0] = 0x35; } return 0; }改错
在你提供的代码中,没有包含头文件 "main.h" 和 "stdio.h"。你需要添加这两个头文件到你的代码中,这样才能正确使用其中的函数和变量。
另外,你在调用 sprintf 函数时,第二个参数类型应该是 char*,而不是 uint8_t*。因此,你需要将 aTXbuf 声明为 char 类型数组,而不是 uint8_t 类型数组。同时,在使用 strlen 函数计算字符串长度时,应该传入一个 char* 类型的参数。
最后,你可以将 DHT11_Read_Data 函数的返回值直接返回,而不需要使用 if-else 语句判断后再返回。因为函数内部已经有了返回值,可以直接将其返回给调用者。
以下是修改后的代码:
```c
#include "main.h"
#include "stdio.h"
#include "dht11.h"
extern UART_HandleTypeDef huart1;
extern TIM_HandleTypeDef htim3;
void Delay_us(uint16_t delay) {
__HAL_TIM_DISABLE(&htim3);
__HAL_TIM_SET_COUNTER(&htim3,0);
__HAL_TIM_ENABLE(&htim3);
uint16_t curCnt=0;
while(1)
{
curCnt=__HAL_TIM_GET_COUNTER(&htim3);
if(curCnt>=delay)
break;
}
__HAL_TIM_DISABLE(&htim3);
}
void DHT11_OUT(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void DHT11_IN(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void DHT11_Strat(void) {
DHT11_OUT();
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET);
HAL_Delay(20);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET);
Delay_us(30);
}
uint8_t DHT11_Check(void) {
uint8_t retry = 0 ;
DHT11_IN();
while(GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_8) && retry <100)
{
retry++;
Delay_us(1);//1us
}
if(retry>=100)
{return 1;}
else retry = 0 ;
while(GPIO_PIN_RESET == HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_8) && retry<100)
{
retry++;
Delay_us(1);//1us
}
if(retry>=100)
{return 1;}
return 0 ;
}
uint8_t DHT11_Read_Bit(void) {
uint8_t retry = 0 ;
while(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) && retry <100)
{
retry++;
Delay_us(1);
}
retry = 0 ;
while(GPIO_PIN_RESET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) && retry<100)
{
retry++;
Delay_us(1);
}
Delay_us(40);
if(GPIO_PIN_SET==HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8)) return 1;
else return 0 ;
}
uint8_t DHT11_Read_Byte(void) {
uint8_t i , dat ;
dat = 0 ;
for(i=0; i<8; i++)
{
dat <<= 1;
dat |= DHT11_Read_Bit();
}
return dat ;
}
uint8_t DHT11_Read_Data(uint8_t* temp , uint8_t* humi) {
uint8_t buf[5];
uint8_t i;
DHT11_Strat();
if(DHT11_Check() == 0)
{
for(i=0; i<5; i++)
{
buf[i] = DHT11_Read_Byte();
}
if(buf[0]+buf[1]+buf[2]+buf[3] == buf[4])
{
*humi = buf[0];
*temp = buf[2];
return 0;
}
}
return 1;
}
void func_1() {
uint8_t temperature = 1;
uint8_t humidity = 1;
char aTXbuf[32];
while(1){
DHT11_Read_Data(&temperature , &humidity);
sprintf(aTXbuf,"%d , %d %% \r\n" ,temperature ,humidity);
HAL_UART_Transmit(&huart1, (uint8_t*)aTXbuf, strlen(aTXbuf), 200);
HAL_Delay(5000);
}
}
int temperature_humidity_device_control(protocol_package_t *pk) {
printf("temperature_humidity_device_control\r\n");
if(pk->function == 0x16 && pk->data[0] == 0x00)
{
printf("temperature_humidity_device_control success\r\n");
uint8_t temperature = 1 ;
uint8_t humidity = 1;
char aTXbuf[32];
DHT11_Read_Data(&temperature , &humidity);
sprintf(aTXbuf,"%d , %d %% \r\n" ,temperature ,humidity);
strncpy((char*)pk->data, aTXbuf, strlen(aTXbuf));
pk->data_len = strlen(aTXbuf);
pk->data[0] = 0x35;
}
return 0;
}
```
基于STM32HAL库不用ADC转换直接读取出的温度通过广和通通讯模组L610运用串口打印AT指令上传腾讯云的代码
以下是基于STM32HAL库,通过L610通讯模组上传腾讯云的代码:
```c
#include "stdio.h"
#include "string.h"
#include "stm32f1xx_hal.h"
#define UART_TX_BUF_SIZE 1024
#define UART_RX_BUF_SIZE 1024
#define WIFI_TX_BUF_SIZE 1024
#define WIFI_RX_BUF_SIZE 1024
UART_HandleTypeDef huart2;
uint8_t uart_tx_buf[UART_TX_BUF_SIZE];
uint8_t uart_rx_buf[UART_RX_BUF_SIZE];
uint8_t wifi_tx_buf[WIFI_TX_BUF_SIZE];
uint8_t wifi_rx_buf[WIFI_RX_BUF_SIZE];
uint8_t temperature[5] = {0}; // 保存温度值
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
void uart_send_string(const char *str)
{
HAL_UART_Transmit(&huart2, (uint8_t*)str, strlen(str), 0xFFFF);
}
void wifi_send_string(const char *str)
{
HAL_UART_Transmit(&huart2, (uint8_t*)str, strlen(str), 0xFFFF);
}
void wifi_send_command(const char *command, const char *response, uint32_t timeout)
{
memset(wifi_rx_buf, 0, WIFI_RX_BUF_SIZE);
wifi_send_string(command);
HAL_Delay(timeout);
HAL_UART_Receive(&huart2, wifi_rx_buf, WIFI_RX_BUF_SIZE, timeout);
if (strstr((const char*)wifi_rx_buf, response) == NULL)
{
// 未收到期望的响应
uart_send_string("ERROR\r\n");
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
while (1)
{
// 读取温度值
uint16_t adc_value = HAL_ADC_GetValue(&hadc1);
float voltage = adc_value * 3.3 / 4096; // 计算电压值
float temperature_value = (voltage - 0.76) / 0.0025 + 25; // 计算温度值
sprintf((char*)temperature, "%.2f", temperature_value); // 转换为字符串
// 连接WiFi网络
wifi_send_command("AT+RST\r\n", "ready", 2000);
wifi_send_command("AT+CWJAP=\"your_ssid\",\"your_password\"\r\n", "OK", 5000);
// 连接腾讯云
wifi_send_command("AT+CIPSTART=\"TCP\",\"your_server_ip\",your_server_port\r\n", "OK", 5000);
wifi_send_command("AT+CIPSEND\r\n", ">", 1000);
sprintf((char*)wifi_tx_buf, "{\r\n\"temperature\": %s\r\n}\r\n", temperature);
wifi_send_string((const char*)wifi_tx_buf);
HAL_Delay(1000);
wifi_send_command("AT+CIPCLOSE\r\n", "OK", 5000);
HAL_Delay(5000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
HAL_ADC_Start(&hadc1);
}
```
需要注意的是,此代码中的以下几个参数需要根据实际情况进行修改:
- `your_ssid`: WiFi SSID
- `your_password`: WiFi 密码
- `your_server_ip`: 上传数据服务器 IP 地址
- `your_server_port`: 上传数据服务器端口号
此外,还需要在腾讯云上创建一个设备,并将该设备的证书信息配置到 L610 模组中。
阅读全文