没有合适的资源?快使用搜索试试~ 我知道了~
首页xmodem程序实例.doc
xmodem程序实例.doc
4星 · 超过85%的资源 需积分: 12 27 下载量 38 浏览量
更新于2023-03-16
评论
收藏 78KB DOC 举报
此文章收录纯属是从完善本xmodem题目以其系统性考虑。由于下面代码只有发送部分,所以供 读者参考程序设计的框架更有意义
资源详情
资源评论
资源推荐
.h 文件
/*****************************************************************************/
/*
* xmodem
*/
/
*******************************************************************************
******
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
******************************************************************************
*******/
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include<stdio.h>/*printf*/
#include<fcntl.h>/*open*/
#include<string.h>/*bzero*/
#include<stdlib.h>/*exit*/
#include<sys/times.h>/*times*/
#include<sys/types.h>/*pid_t*/
#include<termios.h>/*termios,tcgetattr(),tcsetattr()*/
#include<unistd.h>
#include<sys/ioctl.h>/*ioctl*/
#include<sys/wait.h>
#include<string.h>/*bzero*/
/*
Xmodem Frame form: <SOH><blk #><255-blk #><--128 data bytes--><CRC hi><CRC lo>
*/
#define XMODEM_SOH 0x01
#define XMODEM_STX 0x02
#define XMODEM_EOT 0x04
#define XMODEM_ACK 0x06
#define XMODEM_NAK 0x15
#define XMODEM_CAN 0x18
#define XMODEM_CRC_CHR 'C'
#define XMODEM_CRC_SIZE 2 /* Crc_High Byte + Crc_Low Byte */
#define XMODEM_FRAME_ID_SIZE 2 /* Frame_Id + 255-Frame_Id */
#define XMODEM_DATA_SIZE_SOH 128 /* for Xmodem protocol */
#define XMODEM_DATA_SIZE_STX 1024 /* for 1K xmodem protocol */
#define USE_1K_XMODEM 0 /* 1 for use 1k_xmodem 0 for xmodem */
#define TIMEOUT_USEC 0
#define TIMEOUT_SEC(buflen,baud) (buflen*20/baud+2)/*接收超时*/
#define TIMEOUT_USEC 0
// 是否使用 1K-xModem 协议
#if (USE_1K_XMODEM)
#define XMODEM_DATA_SIZE XMODEM_DATA_SIZE_STX
#define XMODEM_HEAD XMODEM_STX
#else
#define XMODEM_DATA_SIZE XMODEM_DATA_SIZE_SOH
#define XMODEM_HEAD XMODEM_SOH
#endif
#define SERIAL_DEVICE "/dev/ttyS0" //串口 1
#define MYBAUDRATE B115200
#define RECV_END 0x11 //自定义文件结束符
/*
* This function calculates the CRC used by the "Modem Protocol".
* The first argument is a pointer to the message block. The second argument is the number of
bytes in
* the message block. The message block used by the Modem Protocol contains 128 bytes.
* The function return value is an integer which contains the CRC.
*/
void delay(int x)
{
int y;
for(;x>0;x--)
for(y=10;y>0;y--);
}
unsigned short GetCrc16 ( char *ptr, unsigned short count )
{
unsigned short crc, i;
crc = 0;
while(count--)
{
crc = crc ^ (int) *ptr++ << 8;//从 packet_data 中取一个字节数据,强转为 16 为 int,
再把低八位移到高八位,赋值给 crc
for(i = 0; i < 8; i++)
{
if(crc & 0x8000)//判断数据的最高位数据是否为 1
crc = crc << 1 ^ 0x1021; // CRC-ITU
else
crc = crc << 1;
}
}
return (crc & 0xFFFF);
}
/*******************************************
*receivedata
*返回实际读入的字节数
*
********************************************/
int PortRecv(int fdcom,char* data,int datalen,int baudrate)
{
int readlen,fs_sel;
int readnum=0x00;
char readtemp=0;
fd_set fs_read;
struct timeval tv_timeout;
FD_ZERO(&fs_read);
FD_SET(fdcom,&fs_read);
tv_timeout.tv_sec=TIMEOUT_SEC(datalen,baudrate);
tv_timeout.tv_usec=TIMEOUT_USEC;
fs_sel=select(fdcom+1,&fs_read,NULL,NULL,NULL);
if(fs_sel)
{
//while(read(fdcom,&readtemp,1));
readnum=0;
剩余11页未读,继续阅读
snowlxq
- 粉丝: 1
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- stc12c5a60s2 例程
- Android通过全局变量传递数据
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论2