資源簡(jiǎn)介
1、ymodem發(fā)送代碼(c語言),網(wǎng)上大多只有接收代碼,有也是寫得比較亂,此代碼小巧便于移植到小系統(tǒng)。
2、優(yōu)點(diǎn):io可自定義,注釋清晰,傳輸效率高,短小精煉。
2、優(yōu)點(diǎn):io可自定義,注釋清晰,傳輸效率高,短小精煉。

代碼片段和文件信息
/*?by?kh0723?*/
#include?“common.h“
#include?“ymodem.h“
#include?“string.h“
#include?
#include?“vxWorks.h“
#include?“crc.h“
/*?初始化輸入輸出接口?*/
FUNCPTR?ymodem_putc_fun?=?NULL;
FUNCPTR?ymodem_getc_fun?=?NULL;
void?ymodem_io_init(FUNCPTR?putc?FUNCPTR?getc)
{
????ymodem_putc_fun?=?putc;
????ymodem_getc_fun?=?getc;
}
/*?輸出字符?*/
int?ymodem_debug?=?0;
void?ymodem_putc(char?c)
{
????if(ymodem_putc_fun)
????????ymodem_putc_fun(c);
????else
????????printf(“\r\n?Need:?ymodem_io_init()“);
????if((ymodem_debug?&?0x1)?==?0x1)
??????printf(“t:?%02x?“?c);
}
/*?輸入字符?*/
char?ymodem_getc()
{
????char?c?=?0xff;
????if(ymodem_getc_fun)
????????c?=?ymodem_getc_fun();
????else
????????printf(“\r\n?Need:?ymodem_io_init()“);????????
????if((ymodem_debug?&?0x2)?==?0x2)
????????printf(“\r\nr:?%02x?\r\n“?c);
????????
????return?c;
}
/*?延時(shí)?*/
void?ymodem_delay(int?ticks)
{
????taskDelay(ticks);
}
/*?報(bào)文填充?*/
int?ymodem_pkt_fill(int?pkt_mode?int?pkt_id?char*?a_data_ptr?int?a_data_len?YMODEM_PKT*?pkt)
{
????int?pkt_data_len?=?PKT_MODE_2_DATA_LEN(pkt_mode);
????if((pkt_id?0)?||?(NULL?==?a_data_ptr)?||?(a_data_len?<=?0)?||?(NULL?==?pkt))
????????return?-1;
????????
????//頭:?發(fā)送模式、報(bào)文id、報(bào)文id補(bǔ)碼
????pkt->mode?=?pkt_mode;
????if(EOT?!=?pkt_mode)
????{
????????pkt->id?=?pkt_id;
????????pkt->id2?=?~pkt_id;
????????//數(shù)據(jù)
????????memset(pkt->data_buf?0?sizeof(pkt->data_buf));
????????memcpy(pkt->data_buf?a_data_ptr?a_data_len);
????????pkt->data_len?=?pkt_data_len;
????????//crc
????????pkt->crc16?=?(int)cyg_crc16((unsigned?char*)(pkt->data_buf)?pkt->data_len);
????}
????
????return?0;
}
/*?報(bào)文發(fā)送?*/
void?ymodem_pkt_tx(YMODEM_PKT?pkt)
{
????int?i;
????ymodem_putc(pkt.mode);
????//非eot
????if(EOT?!=?pkt.mode)
????{
????????//printf(“\r\n?pkt_mode?%d?pkt_id?%d?pkt_data_len?%d?pkt.crc16?%x\r\n“?pkt.mode?pkt.id?pkt.data_len?pkt.crc16);
????????ymodem_putc(pkt.id);
????????ymodem_putc(pkt.id2);
????????for(i?=?0;?i?????????{
????????????ymodem_putc(pkt.data_buf[i]);
????????}
????????ymodem_putc(pkt.crc16?>>?8);
????????ymodem_putc(pkt.crc16?&?0xFF);
????}
????
????//發(fā)送等待
????ymodem_delay(1);
}
/*?報(bào)文回應(yīng)確認(rèn):?返回是否ok?*/
int?ymodem_pkt_ack(YMODEM_PKT?pkt)
{
????int?pkt_first?=?0;
????int?pkt_end?=?0;
????char?pkt_ack?ack_crc16;
????//判斷pkt標(biāo)記
????if((0?==?pkt.id)?&&?(0?!=?pkt.data_buf[0]))
????????pkt_first?=?1;
????else?if(EOT?==?pkt.mode)
????????pkt_end?=?1;
????????
????//接收等待?對(duì)端處理能力有限
????if(pkt_first)
????????ymodem_delay(20);
????else
????????ymodem_delay(1);
????//接收
????pkt_ack?=?ymodem_getc();
????if(ACK?!=?pkt_ack)
????????return?0;
????//開始和結(jié)束會(huì)返回crc????
????if(pkt_first?||?pkt_end)
????{
????????ack_crc16?=?ymodem_getc();
????}
????????
????return?1;
}
/*?報(bào)文傳輸?*/
int?ymodem_pkt_transfer(int?pkt_mode?int?pkt_id?char*?a_data_ptr?int?a_data_len)
{
????YMODEM_PKT?pkt;
????int?i;
????if(
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????1249??2018-08-23?16:13??ymodem.h
?????文件???????5879??2018-08-23?16:13??ymodem.c
-----------?---------??----------?-----??----
?????????????????7128????????????????????2
評(píng)論
共有 條評(píng)論