Работа с файловой системой на удаленном компьютере через последовательный порт, страница 2

case 0xDB : WriteSer(0xDB); WriteSer(0xDD); break;

default   : WriteSer(c); break;

}

}

printf("Sending EOF...\n");

WriteSer(0xC0);

CloseSerial();

fclose(inf);

}

/* This function implements a CD command (MS-DOS style) */

void exec_cd(char * d)

{

char buf[128];

//We have something to change to - do it

if (d != NULL) chdir(d);

/* Get and echo current working directory */

getcwd(buf, 128);

_puts(buf);

_puts("\r\n");

}

/* Implementation of MS-DOS style DIR command */

void exec_dir(void)

{

struct ffblk ffblk;

int          done;

_puts("Directory listing of *.*\r\n");

done = findfirst("*.*", &ffblk, FA_DIREC);

while (!done) {

char scratch[80];

if (ffblk.ff_attrib & FA_DIREC) {

/* This is a directory - echo <DIR> */

sprintf(scratch, "  %-12s  <DIR>\r\n", ffblk.ff_name);

}

else {

/* This is a file - echo file size */

sprintf(scratch, "  %-12s  %d\r\n", ffblk.ff_name, ffblk.ff_fsize);

}

/* Send next line of a result */

_puts(scratch);

done = findnext(&ffblk);

}

}

void main(void)

{

char scratch[128];

char init_ok = 0;

/* Set up serial port */

printf("Setting up serial link...\n");

OpenSerial(COM_2, SER_BAUD_600, SER_STOP_2 | SER_BITS_8 | SER_PARITY_ODD);

//Flushing queue (in case of some bytes reside in FIFO)

printf("Flushing receive buffer...\n");

while (DataReadyCount() != 0) ReadQueue();

/* Wait for 0xFF character - signal, that client was connected */

printf("Waiting for connection...\n");

while (DataReadyCount() == 0);

if (ReadQueue() == 0xFF) {

//Ok, client connected

printf("Client connected!\n");

init_ok = 1;

}

else {

/* Something went wrong! We MUST NOT receive this on connection */

printf("Oops, something gone wrong!\n");

}

/* We initialized normally - start a command loop */

while (init_ok) {

int ok = 0;

char * cmd;

char * par;

/* Show command prompt */

_puts("> ");

/* Receive a command from client */

_gets(scratch);

/* Separate tokens - command and parameters */

cmd = strtok(scratch, " \r\n");

par = strtok(NULL, " \r\n");

/* Check for one of four commands and execute them */

if (stricmp(cmd, "dir") == 0 && !ok) {ok = 1; exec_dir();}

if (stricmp(cmd, "cd") == 0 && !ok) {ok = 1; exec_cd(par);}

if (stricmp(cmd, "copy") == 0 && !ok) {ok = 1; exec_copy(par);}

if (stricmp(cmd, "exit") == 0 && !ok) break;

}

/* Send termination signal and shut down serial port */

printf("Bringing down serial link...\n");

WriteSer(0xFF);

CloseSerial();

}

Листинг серверной программы:

#include <stdio.h>

#include <conio.h>

#include <dos.h>

#include "comm.cc"  //Include shared module

volatile int   marker = 0;

void main(void)

{

int marker1=0;

FILE * outf;

int c,d,k,e;

/* Set up serial port */

printf("Setting up serial link...\n");

OpenSerial(COM_1, SER_BAUD_600, SER_STOP_2 | SER_BITS_8 | SER_PARITY_ODD);

/* Send signal, that client is connected and active */

printf("Sending connection request...\n");

WriteSer(0xFF);

while(DataReadyCount()!=0) ReadQueue();

outf=fopen("c:\\temp\\1.txt","wb");

/* Terminal loop */

while (1)

{

if (kbhit()) {

e = getche();

if (e == 13) putch(10);

WriteSer(e);

}

if (DataReadyCount())

{d = ReadQueue();

if (d==0x00) {marker=1;break;} else putch(d);

}

}

while (1)

{

int to_write =-1;

if (kbhit()) {

k = getche();

if (k == 13) putch(10);

WriteSer(k);

}

if (DataReadyCount()){

c=ReadQueue();

if (c==0xC0) break;

if (marker==1)

{

if(marker1)

{

if (c==0xDC)

to_write= 0xC0;

else

to_write = 0xDB;

marker1=0;

}

else

{

if (c==0xDB) marker1 =1;

else to_write=c;

}

if (to_write!=-1) {fputc(to_write,outf); putch(to_write); }

}}}

CloseSerial();

fclose(outf);

}

Листинг подключаемого модуля:

#define SER_RBF              0 /*the read buffer*/

#define SER_THR              0 /*the write buffer*/

#define SER_IER              1 /*the int. enable register*/

#define SER_IIR              2 /*the int. identiffication register*/

#define SER_LCR              3 /*control data config. and divisor latch*/

#define SER_MCR              4 /*modem control register*/

#define SER_LSR              5 /*line status register*/

#define SER_MSR              6 /*modem status of cts,ring etc.*/

#define SER_DLL              0 /*the low byte of baud rate divisor*/

#define SER_DLH              1 /*the hi byte of divisor latch*/

#define SER_GP02             8 /*enable interrupt*/

#define COM_1                0x3F8

#define COM_2                0x2F8