linux xmodem,linux xmodem
当前位置:我的异常网» 热门搜索 » linux xmodem
linux xmodem
www.myexceptions.net 网友分享于:2013-09-15 搜索量:89次
场景:嵌入式linux上串口采用xmodem协议 接受不成功
嵌入式linux下串口采用xmodem协议 接受不成功
接受时总是无法接受完整的包而超出最大次数,最后退出程序, 请问是什么原因? 怎么改?
int xmodemReceive(int fd, char *dest, int destsz)
{
unsigned char xbuff[38]; /* 1024 for XModem 1k + 3 head chars + 2 crc + nul */
//unsigned char *p;
int bufsz, crc = 0;
unsigned char c, trychar = 'C';
unsigned char packetno = 1;
int len = 0;
int retry, retrans = MAXRETRANS;
for(;;)
{
for( retry = 0; retry
{
if (trychar)
port_outbyte(fd, trychar);
}
while(c != SOH){
c = port_inbyte(fd);
if(c == EOT || c == CAN)
break;
}//(DLY_1S)<<1);
if (last_error == 0)
{
switch (c)
{
case SOH:
bufsz = 32;
goto start_recv;
case EOT:
flushinput();
port_outbyte(fd, ACK);
return len; /* normal end */
case CAN:
c = port_inbyte(fd);//DLY_1S);
if (c == CAN)
{
flushinput();
port_outbyte(fd, ACK);
return -1; /* canceled by remote */
}
break;
default:
break;
}
}
if (trychar == 'C')
{
trychar = NAK;
continue;
}
flushinput();
port_outbyte(fd, CAN);
port_outbyte(fd, CAN);
port_outbyte(fd, CAN);
return -2; /* sync error */
start_recv:
if (trychar == 'C') crc = 1;
trychar = 0;
usleep(10000);
retry = read(fd, xbuff, 38);
if (retry
{
printf("last_error2=%d\n", retry);
goto reject;
}
if (xbuff[1] == (unsigned char)(~xbuff[2]) &&
(xbuff[1] == packetno || xbuff[1] == (unsigned char)packetno-1) &&
check(crc, &xbuff[3], bufsz))
{
if (xbuff[1] == packetno)
{
int count = destsz - len;
if (count > bufsz)
count = bufsz;
if (count > 0)
{
memcpy (&dest[len], &xbuff[3], count);
len += count;
}
++packetno;
printf("count=%d len=%d packetno=%d\n", count, len, packetno);
retrans = MAXRETRANS+1;
}
if (--retrans <= 0)
{
flushinput();
port_outbyte(fd, CAN);
port_outbyte(fd, CAN);
port_outbyte(fd, CAN);
return -3; /* too many retry error */
}
port_outbyte(fd, ACK);
continue;
}
reject:
flushinput();
c = port_inbyte(fd);
printf("rej=%d\n", c);
port_outbyte(fd, NAK);
}
}
int xmodemTransmit(int fd, unsigned char *src, int srcsz)
{
unsigned char xbuff[38]; /* 1024 for XModem 1k + 3 head chars + 2 crc + nul */
int bufsz, crc = -1;
unsigned char packetno = 1;
int c, len = 0;
int retry;
for(;;)
{
//for( retry = 0; retry
//{
while(c != 'C'){
c = port_inbyte(fd);
if(c == NAK || c == CAN)
break;
}//(DLY_1S)<<1);
if (last_error == 0)
{
switch (c)
{
case 'C':
crc = 1;
goto start_trans;
case NAK:
crc = 1;
goto start_trans;
case CAN:
c = port_inbyte(fd);//DLY_1S);
if (c == CAN)
{
port_outbyte(fd, ACK);
flushinput();
return -1; /* canceled by remote */
}
break;
default:
break;
}
}
//}
port_outbyte(fd, CAN);
port_outbyte(fd, CAN);
port_outbyte(fd, CAN);
flushinput();
return -2; /* no sync */
for(;;)
{
start_trans:
xbuff[0] = SOH; bufsz = 32;
xbuff[1] = packetno;
xbuff[2] = ~packetno;
c = srcsz - len;
printf("c1=%d \n", c);
if (c > bufsz) c = bufsz;
if (c >= 0)
{
memset (&xbuff[3], 0, bufsz);
if (c == 0)
{
xbuff[3] = CTRLZ;
}
else
{
memcpy (&xbuff[3], &src[len], c);
if (c
}
if (crc)
{
unsigned short ccrc = crc16_ccitt(&xbuff[3], bufsz);
xbuff[bufsz+3] = (ccrc>>8) & 0xFF;
xbuff[bufsz+4] = ccrc & 0xFF;
}
for (retry = 0; retry
{
usleep(100000);
write(fd, xbuff, 38);
usleep(10000);
c = port_inbyte(fd);//DLY_1S);
if (last_error == 0 )
{
switch (c)
{
case ACK:
printf("ACK=%d\n", c);
++packetno;
len += bufsz;
goto start_trans;
case CAN:
c = port_inbyte(fd);//DLY_1S);
if ( c == CAN)
{
port_outbyte(fd, ACK);
flushinput();
return -3; /* canceled by remote */
}
break;
case NAK:
printf("NAK=%d\n", c);
//break;
default:
break;
}
}
}
printf("C=%d\n", c);
port_outbyte(fd, CAN);
port_outbyte(fd, CAN);
port_outbyte(fd, CAN);
flushinput();
return -4; /* xmit error */
}
else
{
for (retry = 0; retry
{
port_outbyte(fd, EOT);
c = port_inbyte(fd);//(DLY_1S)<<1);
if (c == ACK) break;
}
flushinput();
return (c == ACK)?len:-5;
}
}
}
}
------解决方案--------------------
楼主 有完整的程序嘛 发份看下 linuxarmcom@163.com
文章评论
linux xmodem,linux xmodem相关推荐
- 【Linux】Linux中rz和sz命令用法详解
[Linux]Linux中rz和sz命令用法详解 1. 简单介绍 rz,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具.优点就是不用再开一个sftp工具登录上去上传下载 ...
- nvidia命令不可用linux,在Linux命令行下如何正确配置nVIDIA显卡
在图形芯片领域,nVidia是一个后来者,它的历史仅相当于ATi的一半.1993年初,NVIDIA 由 Jen-Hsun Huang,,Chris Malachowsky和 Curtis Priem ...
- 20155301 滕树晨linux基础——linux进程间通信(IPC)机制总结
20155301 滕树晨linux基础--linux进程间通信(IPC)机制总结 共享内存 共享内存是在多个进程之间共享内存区域的一种进程间的通信方式,由IPC为进程创建的一个特殊地址范围,它将出现在 ...
- 【Linux 内核】调度器 ① ( 调度器概念 | 调度器目的 | 调度器主要工作 | 调度器位置 | 进程优先级 | 抢占式调度器 | Linux 进程状态 | Linux 内核进程状态 )
文章目录 一.调度器 0.调度器概念 1.调度器目的 2.调度器主要工作 3.调度器位置 4.进程优先级 5.抢占式调度器 二.Linux 内核进程状态 API 简介 三.Linux 进程状态 一.调 ...
- 在Windows里备份Linux分区,在Windows里分出Linux分区(Linux分区).pdf
在Windows里分出Linux分区(Linux分区) ks2886v的个人空间 分区 作者:红联 分区 和大部分同学一样,我的电脑全部都是FAT32格式和NTFS格式,之前并没有考虑其他的系统,也 ...
- linux mint 18.3 内核,Linux Mint Linux用户可以升级到18.2 18.3”
Linux Mint Linux用户可以升级到18.2 18.3"现在应该在造币厂"的技术 Linux Mint的升级路径--从"索尼娅".现在的操作系统是一个 ...
- VM之Linux:Linux的Ubuntu中,解决安装后屏幕太小的问题
VM之Linux:Linux的Ubuntu中,解决安装后屏幕太小的问题 导读 最讨厌网上回答的啰嗦和不清晰!本人最讨厌啰嗦,直接上来,图文表达,简单明了,快速解决问题!最讨厌网上答案一大片的废话和没有 ...
- samba权限跟linux权限,linux之samba与linux权限
当linux的文件夹或文件为用户或root时,则在window上共享出来的samba是不能进行修改的,当samba设置为 [share] path = /home #available = yes # ...
- 文件上传linux服务器,Linux 文件上传Linux服务器
进入命令行 在图形化桌面出现之前,与Unix系统进行交互的唯一方式就是借助由shell所提供的文本命令行界面(command line interface,CLI).CLI只能接受文本输入,也只能显示 ...
最新文章
- 主机漏洞-SSL/TLS 受诫礼(BAR-MITZVAH)攻击漏洞(CVE-2015-2808)【原理扫描】-RC4密码套件
- chrome自动调节窗口大小插件_高效使用Chrome浏览器,教你10个小技巧!
- android怎么创建一个包命名空间,从Android的jar lib中声明命名空间
- Abp 代码生成器使用说明
- Pandas 文本数据方法 wrap( )
- [Asp.net]Calendar+JqueryUi实现日程管理(右键菜单,添加,编辑,删除,源码)
- 服装关键点检测算法(CNN/STN)含(4点、6点以及8点)
- linux内核数据链路层,Linux网络内核数据帧的接收进程:数据链路层(概念篇)...
- 在cmd命令行运行一个python脚本
- Android实现立体滚轮控件,Camera和Matrix实现真正的3D(WheelView)滚轮控件
- 001深度强化学习的异步方法
- Unity.TimeLine
- ThinkPad笔记本电脑网络重置之后wifi没有了
- 微信小程序推广方式有哪些?
- html3d粒子球,Canvas粒子系统:3D球体
- GDT、GDTR、LDT、LDTR的理解 [zz]
- 2013年第4季度橱柜品牌网络知名度排名
- 关于vim gf问题
- 磁盘空间不足怎么办?教你重新分配磁盘空间
- 老师这样讲桥式整流电路,秒懂!
热门文章
- cordova ,ionic 中cordova.js 提示deviceready has not fired after 5 seconds
- Unity3D引擎之高级渲染技术
- 苹果“软”了,更好卖 | WWDC 2018
- 【图像分割】基于FLICM算法实现图像分割含Matlab源码
- pytorch官方教程中文版(二)学习PyTorch
- display布局混乱 高度坍缩
- 斗地主发牌Java课程设计
- 2021年全球电动驻车制动系统收入大约9422.3百万美元,预计2028年达到19330百万美元
- 求几何形状的面积之和
- 图像分割——语义分割:FCN与Unet