我的代码可以很好地将循环中的第一个文件上传到 ftp。但是一旦要上传第二个文件,它就会挂起。
我首先使用 lib 读取 SD 卡根文件夹,我设置 ftp 连接connectFTP()
用于建立数据传输端口。接下来我调用fileTransfer();
While 循环函数运行良好,直到传输第一个文件。一旦第二个文件满足 If 条件
if (fileTemp != fileName && fileTemp[0] == '1' && fileTemp[1] == '9')
并发送
client.print(F("STOR "));
client.println(fileTemp);
没有回应。
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(config.server, config.ftpport)) {
Serial.println("connected to server");
}
if(!eRcv()) Serial.println("fail");
client.print(F("USER "));
client.println(config.ftplogin);
if(!eRcv()) Serial.println("fail USER");
client.print("PASS ");
client.println(config.ftppass);
if(!eRcv()) Serial.println("fail PASS");
client.println(F("SYST"));
if(!eRcv()) Serial.println("fail SYST");;
client.println(F("Type I"));
if(!eRcv()) Serial.println("fail TYPE I");
client.println(F("PASV"));
if(!eRcv()) Serial.println("fail PASV");
char *tStr = strtok(outBuf,"(,");
int array_pasv[6];
for ( int i = 0; i < 6; i++) {
tStr = strtok(NULL,"(,");
array_pasv[i] = atoi(tStr);
if(tStr == NULL)
{
Serial.println(F("Bad PASV Answer"));
}
}
unsigned int hiPort,loPort;
hiPort = array_pasv[4] << 8;
loPort = array_pasv[5] & 255;
Serial.print(F("Data port: "));
hiPort = hiPort | loPort;
Serial.println(hiPort);
if (dclient.connect(config.server,hiPort)) {
Serial.println(F("Data connected"));
}
else {
Serial.println(F("Data connection failed"));
client.stop();
}
client.println(F("CWD tpv.cba.pl"));
if(!eRcv()) Serial.println("fail CWD");
delay(500);
}
void fileTransfer() {
if (!root.open("/")) {
sd.errorHalt("open root failed");
}
char fileTemp[13];
while (ftpfile.openNext(&root, O_RDONLY)) {
ftpfile.getName(fileTemp,13);
//Serial.println(fileTemp);
if (fileTemp != fileName && fileTemp[0] == '1' && fileTemp[1] == '9') {
Serial.println(fileTemp);
client.print(F("STOR "));
client.println(fileTemp);
if(!eRcv()) Serial.println("fail STOR");
size_t m;
while((m = ftpfile.read(fileBuf, sizeof(fileBuf)))>0) {
dclient.write(fileBuf, m);
}
if(!eRcv()) Serial.println("fail STOR");
}
ftpfile.close();
}
dclient.stop();
Serial.println(F("Data disconnected"));
if(!eRcv()) Serial.println("fail Data disconnet");
client.println(F("QUIT"));
if(!eRcv()) Serial.println("fail QUIT");
client.stop();
Serial.println(F("Command disconnected"));
}
收到的日志:
Starting connection to server...
connected to server
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 49 of 300 allowed.
220-Local time is now 14:11. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this serve331 User xxxxx OK. Password required
230-Your bandwidth usage is restricted
230-OK. Current restricted directory is /
230 Max allowed filesize is 10485760 bytes
215 UNIX Type: L8
200 TYPE is now 8-bit binary
227 Entering Passive Mode (81,171,31,230,225,201)
Data port: 57801
Data connected
250 OK. Current directory is /xxx.cba.pl
19080400.log
150 Accepted data connection
226-File successfully transferred
226 0.094 seconds (measured here), 59.55 Kbytes per second
19080402.log