Skip to content

Commit

Permalink
支持xmodem
Browse files Browse the repository at this point in the history
  • Loading branch information
heyuanjie87 committed Apr 22, 2019
1 parent 7dfd71f commit a3fa2af
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions Modem/Modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void Modem::startTransfer(char type)
{
showTransfer(1, 1, 0);
show();
ym->setModemMode(type);
ym->start();
}

Expand Down
2 changes: 1 addition & 1 deletion Modem/Modem.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Modem : public QDialog

void setFile(QString &name);
void getFile(QString &name);
void startTransfer(char type = 'C');
void startTransfer(char type = 'x');
void showStatus(const char *s);
void showStatus(QString &s);

Expand Down
42 changes: 33 additions & 9 deletions Modem/Ymodem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Ymodem::Ymodem(Modem *parent)
{
isrun = false;
ui = parent;
mMode = 'y';
}

void Ymodem::close()
Expand Down Expand Up @@ -65,26 +66,39 @@ int Ymodem::makeFirstRsp(string &name, int size, QByteArray &byte)
return len;
}

int Ymodem::makeNextRsp(char *data, int size, QByteArray &byte)
int Ymodem::makeNextRsp(char *data, int size, QByteArray &byte, int mode)
{
int len = 0;
ymhead_t *pkt;
uint16_t *sum;
int fmsize;
uint8_t start;

byte.resize(3 + 1024 + 2);
if (mode == 'x')
{
fmsize = 128;
start = 0x01;
}
else
{
fmsize = 1024;
start = 0x02;
}

byte.resize(3 + fmsize + 2);
pkt = (ymhead_t *)byte.data();
sn ++;
pkt->start = 0x02;
pkt->start = start;
pkt->sn = sn;
pkt->nsn = 0xFF - sn;
memcpy(pkt->data, data, size);
if (size < 1024)
if (size < fmsize)
{
memset(&pkt->data[size], 0, 1024 - size);
memset(&pkt->data[size], 0, fmsize - size);
}
len = 1024 + 3;
len = fmsize + 3;
sum = (uint16_t*)(((char*)pkt) + len);
*sum = crc16(pkt->data, 1024);
*sum = crc16(pkt->data, fmsize);
len += 2;

return len;
Expand Down Expand Up @@ -155,6 +169,11 @@ void Ymodem::showStatus(const char *s)
ui->showStatus(s);
}

void Ymodem::setModemMode(int m)
{
mMode = m;
}

void Ymodem::run()
{
QString filename;
Expand All @@ -165,6 +184,7 @@ void Ymodem::run()
QFile file;
string stext;
int remain = 0;
int fmsize;

showStatus("已启动Ymodem");
ui->getFile(filename);
Expand All @@ -183,6 +203,10 @@ void Ymodem::run()

Stage = msFirst;
isrun = true;
if (mMode == 'x')
fmsize = 128;
else
fmsize = 1024;

while (isrun)
{
Expand Down Expand Up @@ -236,9 +260,9 @@ void Ymodem::run()
{
int size;

size = file.read(fbuf, 1024);
size = file.read(fbuf, fmsize);
remain -= size;
makeNextRsp(fbuf, size, byte);
makeNextRsp(fbuf, size, byte, mMode);

isread = true;
outData(byte);
Expand Down
6 changes: 5 additions & 1 deletion Modem/Ymodem.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ class Ymodem : public QThread

void close();
void put(const QByteArray &data);
void setModemMode(int m);

private:

int makeFirstRsp(string &name, int size, QByteArray &byte);
int makeNextRsp(char *data, int size, QByteArray &byte);
int makeNextRsp(char *data, int size, QByteArray &byte, int mode);
int makeEotRsp(QByteArray &byte);
int makeFinishRsp(QByteArray &byte);

Expand Down Expand Up @@ -83,6 +86,7 @@ class Ymodem : public QThread
uint8_t sn;
queue <int> msgq;
time_t stx_time;
int mMode;
};

#endif // YMODEM_H
File renamed without changes.

0 comments on commit a3fa2af

Please sign in to comment.