/* DM&P Mity SoC Example Copyright (C) 2002 by DM&P. This example will show you how to drive serial port of Mity SoC. It use our SerPort library to drive COM port. Go to http://www.dmp.com.tw/tech/serport to get more information. Also, you can use it to upload file to Mity SoC. Usage: sender com_port file_name Ex: sender 1 (receiving mode, it will wait for remote host data) sender 2 aaa.txt (sending mode, it will send aaa.txt to remote host) */ #include "serport.h" #include #include #include int _nComPort = COM1; int _nIrq; int _nIoAddr; /* Buffer to save file data */ #define BUF_SIZE 1024 char _pcBuf[BUF_SIZE]; void SendFile(char *szFile); void RecvFile(); void main(int nArgCnt, char *pszArg[]) { char c; printf("\nDM&P Mity SoC Serial Port File Sender (%s %s).\n\n", __DATE__, __TIME__); if(nArgCnt < 2 || nArgCnt > 3) { printf("Usage: %s <1|2> file_name\n", pszArg[0]); return; } /* Setup COM port, IRQ and I/O address */ if(nArgCnt >= 2 && pszArg[1][0] == '1') { _nComPort = COM1; _nIrq = 4; _nIoAddr = 0x3f8; } else if(nArgCnt >= 2 && pszArg[1][0] == '2') { _nComPort = COM2; _nIrq = 3; _nIoAddr = 0x2f8; } else { printf("Error COM port.\n"); return; } /* Open serial port */ if(SerPort_Open(_nComPort, 4096, _nIoAddr, _nIrq) == 0) { printf("Unable to open COM%d.\n", _nComPort + 1); return; } /* Is COM port initialized OK ? */ if(SerPort_IsOk(_nComPort) == 0) { printf("Unable to initialize COM%d.\n", _nComPort + 1); return; } /* Set baud rate 115200 bps, no parity, 8 bits, 1 stop bit */ SerPort_SetParam(_nComPort, BAUD_115200, PARITY_NO, LENGTH_8_BIT, STOPBIT_1_BIT); /* No flie name -> receiving */ if(nArgCnt == 2) RecvFile(); else if(nArgCnt == 3) SendFile(pszArg[2]); /* De-initialize COM port */ SerPort_Close(_nComPort); printf("Program terminated.\n"); } void SendFile(char *szFile) { char szBuf[13]; long lSize, lSize2; int nLen; long lBufCnt = 0; FILE *fp; /* Conver file name to upper case */ strupr(szFile); /* Open the file to be sent */ fp = fopen(szFile, "rb"); if(fp == NULL) { printf("Unable to open %s.\n", szFile); return; } /* Send file name and verify */ SerPort_SendBuf(_nComPort, szFile, 13); SerPort_RecvBuf(_nComPort, szBuf, 13); if(strcmp(szFile, szBuf) != 0) { printf("Unable to send file name.\n"); return; } /* Get file size */ fseek(fp, 0, SEEK_END); lSize = ftell(fp); fseek(fp, 0, SEEK_SET); /* Send file size and verify */ SerPort_SendBuf(_nComPort, &lSize, sizeof(lSize)); SerPort_RecvBuf(_nComPort, &lSize2, sizeof(lSize2)); if(lSize != lSize2) { printf("Unable to send file length.\n"); return; } printf("Ready to send file.\n"); while(1) { /* Read data and send them via COM port */ nLen = fread(_pcBuf, 1, BUF_SIZE, fp); SerPort_SendBuf(_nComPort, _pcBuf, nLen); lBufCnt += nLen; printf("%lu bytes sent\r", lBufCnt); /* We can read BUF_SIZE -> last block */ if(nLen < BUF_SIZE) break; /* Wait for remote response to continue */ SerPort_RecvBuf(_nComPort, &lSize2, sizeof(lSize2)); } printf("%s sent OK\t\t\t\n", szFile); fclose(fp); } void RecvFile() { char szBuf[13]; long lSize; int nLen; long lBufCnt = 0; FILE *fp; /* Receive file name from remote host */ printf("Waiting for remote ...\n"); while(SerPort_Avail(_nComPort) == 0) if(kbhit()) break; SerPort_RecvBuf(_nComPort, szBuf, 13); SerPort_SendBuf(_nComPort, szBuf, 13); /* Create file to write */ fp = fopen(szBuf, "wb"); if(fp == NULL) { printf("Unable to open %s.\n", szBuf); return; } /* Receive file size */ SerPort_RecvBuf(_nComPort, &lSize, sizeof(lSize)); SerPort_SendBuf(_nComPort, &lSize, sizeof(lSize)); lBufCnt = lSize; printf("Receiving %s (%lu bytes)...\n", szBuf, lSize); lSize = BUF_SIZE; while(1) { /* Receive data and save it until */ if(lBufCnt < BUF_SIZE) lSize = lBufCnt; SerPort_RecvBuf(_nComPort, _pcBuf, lSize); nLen = fwrite(_pcBuf, 1, lSize, fp); lBufCnt -= nLen; printf("%lu bytes remainder\r", lBufCnt); if(nLen < BUF_SIZE || lBufCnt <= 0) break; /* Send continue message */ SerPort_SendBuf(_nComPort, &lBufCnt, sizeof(lBufCnt)); } printf("%s received OK\t\t\t\n", szBuf); fclose(fp); }