资源简介
串口传输文件可以采用三种协议xmodem、ymodem、zmodem,本程序只做针对ymodem协议的实现,包括发送方的程序和接收方的程序,下载后在linux平台下执行make即可用,已通过多次测试。
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include “ymodem.h“
#include “serial.h“
#define RESEND 0
#define SENDED 1
#define PACKET_SIZE 128
//#define PACKET_SIZE 1024
enum STATE{
START
RECEIVE_DATA
END
STOP
};
int ymodem_receive(const char *path int fd) {
int fd_file;
unsigned char start_char tmp_char;
int state len i;
int trans_end = 0;
char packet_num;
int packet_size;
int file_size = 0;
char buf[1024] = {0};
fd_file = open(path O_RDWR | O_CREAT | O_TRUNC 0644);
if (fd_file < 0)
perror(“open() file“);
packet_num = 0;
packet_size = 0;
state = START;
while(1) {
if (trans_end == 1) break;
switch(state) {
case START:
printf(“\033[32mSTART_MODE:\033[0m\n“);
put_char(fd ‘C‘);
//start_char = get_char(fd);
//printf(“%x error \n“ start_char);
start_char = get_char(fd);
if (start_char == SOH)
printf(“SOH: packet_128\n“);
else if (start_char == STX)
printf(“STX: packet_1024\n“);
else {
printf(“start_char %x error \n“ start_char);
//exit(1);
}
if (start_char == SOH || start_char == STX){
tmp_char = get_char(fd);
if (tmp_char != 0x00)
return -1; //not 00
printf(“0x00: %x\n“ tmp_char);
tmp_char = get_char(fd);
if (tmp_char != 0xff) {
printf(“0xff: %x error\n“ tmp_char);
return -1; //not ff
}
printf(“0xff: %x\n“ tmp_char);
//receive data
printf(“sizeof buf is %d\n“ sizeof(buf));
for (i = 0; i < PACKET_SIZE; i++) {
buf[i] = get_char(fd);
//printf(“i = %d “ i);
}
//get name and file size
printf(“receive file name: %s length :%d\n“buf strlen(buf));
//printf(“file size: %s\n“ (buf + strlen(buf) + 1));
file_size = atoi((buf + strlen(buf) + 1));
printf(“file size: %d\n“ file_size);
}
//crc
tmp_char = get_char(fd);
printf(“crc1 : %x\n“ tmp_char);
tmp_char = get_char(fd);
printf(“crc2 : %x\n“ tmp_char);
put_char(fd ACK);
packet_num = 1;
put_char(fd ‘C‘);
state = RECEIVE_DATA;
break;
case RECEIVE_DATA:
printf(“RECEIVE_DATA:\n“);
start_char = get_char(fd);
switch(start_char){
case EOT:
state = END;
printf(“\033[32mEOT\033[0m\n“);
put_char(fd ACK);
break;
case STX:
packet_size = 1024;
printf(“STX “);
break;
case SOH:
packet_size = 128;
printf(“SOH “);
break;
case CAN: break;
default: break;
}
if (state == END) break;
tmp_char = get_char(fd);
printf(“%x “ tmp_char);
tmp_char = get_char(fd);
printf(“%x “ tmp_char);
for(i = 0; i < packet_size; i++){
buf[i] = get_char(fd);
putchar(buf[i]);
}
tmp_char = get_char(fd);
printf(“%x “ tmp_char);
tmp_char = get_char(
- 上一篇:计算机专业考研复试英语自我介绍
- 下一篇:DDR3 PCB布线规则
相关资源
- Modbus协议官方文档中、英文全
- 易语言OTG串口通信
- delphi 串口通讯
- STM32蓝牙和串口程序
- LCD显示温度+串口接收温度.rar
- WPF USB 网络 串口 通信软件
- DELPHI与西门子200PLC的串口通信实例
- Verilog FPGA UART串口控制器
- USB转串口驱动,FT232R驱动程序,最新
- PC -- 单片机的串口数据传输系统设计
- STM32F103 串口程序(完整版)
- stm32 ds18b20 温度传感器 测试通过
- PC 串口调试软件
- 51单片机读取温度数据存储到SD卡中并
- Openssl给文件传输加密
- 单片机与PC机串口通讯仿真
- 串口调试助手V5.0
- 双串口调试助手 V4.0 _ 可同时调试两个
- 读取串口数据并画实时曲线的VC 程序
- tftp文件传输工具
- 网络(UDP)转串口程序
- 串口绘制曲线 将收到的数据进行曲线
- 基于ARM蓝牙传输源程序
- JS操作本地网页串口源码
- AT89S ISP下载编程软件(串口)
- 串口操作类(justinio)
- 《Visual Basic 串口通信与测控应用技术
- socket文件传输源码
- 耀华XK3190-A9地磅串口代码
- ch452串口键盘驱动程序
评论
共有 条评论