ZBLOG

#include #include #include #define MAXSIZE 1000 #define SIZE 8 typedef enum StdRet { E_OK = 0, E_NOT_OK }StdRetType; struct { /* data */ }; // 数据封装函数 StdRetType package(unsigned char *source_point, i...

以下是修改后的代码,实现了单帧或多帧传输:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXSIZE 1000
#define SIZE 8

typedef enum StdRet {
    E_OK = 0,
    E_NOT_OK
} StdRetType;

unsigned char str_input[MAXSIZE];
unsigned char str_output[MAXSIZE];

// 数据封装函数
StdRetType package(unsigned char *source_point, int length);

StdRetType output_data(unsigned char *data, int length)
{
    if (length > MAXSIZE) {
        return E_NOT_OK;
    }
    
    printf("Output Data: ");
    for(int index = 0; index < length; index++) {
        printf("0x%02X ", data[index]);
    }
    printf("\n");
    
    return E_OK;
}

int main()
{
    // 在主函数输入一个任意长度的字符
    printf("请输入数据:");
    scanf("%s", str_input); //使用fget函数输入字符串

    package(str_input, strlen((char*)str_input));

    return 0;
}

// 单帧传输
// SF: SingleFrame   8bytes 无效数据0x00填充
// byte0:          高4bits  0x0 低4bits DataLength
// byte1-byten:    data

// 多帧传输
// FF: FirstFrame     8bytes 
// byte0:          高4bits  0x1 低4bits 0x0
// byte1:          CF包数量
// byte2-byte7:   data

// CF: ConsecutiveFrame
// byte0:          高4bits   0x2  低4bits   包数(从1开始)
// byte1-byte7:   data

// 获取数据的长度
int get_len(unsigned char* str)
{
    int len = 0;
    while (str[len] != '\0') {
        len++;
    }
    return len;
}

//封装函数
StdRetType package(unsigned char *source_point, int length)
{
    if (length <= SIZE-1) { // 单帧传输
        printf("Single Frame Transmission\n");
        str_output[0] = 0x00 + length;
        
        for (int i = 1; i <= length; i++) {
            str_output[i] = source_point[i - 1];
        }
        
        output_data(str_output, length+1);
    } 
    else { // 多帧传输
        printf("Multiple Frame Transmission\n");
        int num_of_frames = ((length - SIZE + 2) + (SIZE - 2) - 1) / (SIZE - 2);
        
        str_output[0] = ((num_of_frames << 4) & 0xF0) | 0x10;
        str_output[1] = num_of_frames;

        int src_index = 0;
        
        for (int frame_num = 1; frame_num <= num_of_frames; frame_num++) {
            unsigned char header_byte_2_to_7[SIZE-2];
            
            for (int j = 0; j < SIZE-2 && src_index < length; j++, src_index++) {
                header_byte_2_to_7[j] = source_point[src_index];
            }
            
            printf("Frame %d: ", frame_num);
            
            str_output[0] |= frame_num;
            
            for (int j = 0; j < SIZE-2; j++) {
                str_output[j+2] = header_byte_2_to_7[j];
            }
            
            output_data(str_output, SIZE);
        }
    }
    
    return E_OK;
}

这个修改后的代码实现了单帧或多帧传输。在主函数中,您可以输入任意长度的字符数据。然后通过package函数对数据进行封装,并通过output_data函数打印输出封装后的数据。根据数据长度,如果是小于等于8字节,则使用单帧传输方式;如果大于8字节,则采用多帧传输方式,其中每个分帧都会输出到控制台。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=2383

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?