#include #include #include /////////////////////////////////////////////////////// BOOL OpenPort(); BOOL ClosePort(); BOOL SetupPort(); unsigned char ReadSByte(); BOOL SendSByte(unsigned char); BOOL WriteFByte(unsigned char); BOOL OpenIfile(); BOOL CloseIfile(); unsigned char ReadFByte(); BOOL TransferFile(); BOOL SetAddr(INT baseAddr); BOOL DownloadFile(); /////////////////////////////////////////////////////// //globals////////////////////////////////////////////// // // Port name // char gszPort[255] = "COM2"; char ifileName[255] = "c:\\temp\\data.ext"; // // Port handle // HANDLE hComm; // // Image file handle // HANDLE hFile; ////////////////////////////////////////////////// // // Main // int main(int argc, char* argv[]) { if (argc == 3){ strcpy(gszPort,argv[1]); strcpy(ifileName, argv[2]); } else if (argc == 2) { strcpy(ifileName, argv[1]); } else { printf("syntax: getdata.exe [potrnum] filename.ext\n"); return 0; } printf("running...\n"); if( OpenPort() && OpenIfile() && SetupPort()) { DownloadFile(); } ClosePort(); CloseIfile(); return 1; } //Serial Functions//////////////////////////////// // // OpenPort // BOOL OpenPort() { hComm = CreateFile( gszPort, // pointer to name of the file GENERIC_READ | GENERIC_WRITE, // access (read-write) mode 0, // share mode 0, // pointer to security attributes OPEN_EXISTING, // how to create 0, // file attributes 0); // handle to file with attributes to copy if (hComm == INVALID_HANDLE_VALUE) { printf("failed to open serial port %s \n",gszPort); return 0; } else { printf("serial port %s opened \n",gszPort); return 1; } } // // ClosePort // BOOL ClosePort() { if (CloseHandle( hComm )) { printf("Port closed\n"); return 1; } else { printf("Port close failed\n"); return 0; } } // // SetupPort // BOOL SetupPort() { DCB dcb; //printf("setting up DCB\n"); //FillMemory(&dcb, sizeof(dcb), 0); //initalize dcb //dcb.DCBlength = sizeof(dcb); printf("getting DCB\n"); if (!GetCommState(hComm,&dcb)) { printf("getDCB failed\n"); return 0; } dcb.BaudRate = 9600; dcb.fParity = FALSE; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.ByteSize = 8; dcb.fOutxCtsFlow = FALSE; dcb.fOutxDsrFlow = FALSE; dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fRtsControl = RTS_CONTROL_DISABLE; // if (!BuildCommDCB("9600,n,8,1", &dcb)) { // printf("Port configuration failed\n"); // return FALSE; // } printf("DCB ready for use\n"); if (!SetCommState(hComm, &dcb)) { printf("failed to set port state (%d)\n",GetLastError()); return 0; } else { printf("Port setup complete\n"); return 1; } } // // SendSByte // BOOL SendSByte(unsigned char byteToWrite) { DWORD dwWritten; if(WriteFile(hComm, &byteToWrite, sizeof(byteToWrite), &dwWritten, 0)) { printf("wrote byte %Xh (%c) to serial port\n", byteToWrite,byteToWrite); return 1; } else { printf("serial port write failed\n"); return 0; } } // // ReadSByte // unsigned char ReadSByte() { DWORD dwRead; unsigned char lpBuf; ReadFile(hComm, // handle of file to read &lpBuf, // address of buffer that receives data sizeof(lpBuf), // number of bytes to read &dwRead, // address of number of bytes read 0); // address of structure for data printf("Read byte %Xh from serial port\n",lpBuf); return lpBuf; } //Image file functions/////////////////////////////////////////// // //OpenIfile // BOOL OpenIfile() { hFile = CreateFile( ifileName, // pointer to name of the file GENERIC_READ | GENERIC_WRITE, // access (read-write) mode 0, // share mode 0, // pointer to security attributes CREATE_ALWAYS, // how to create 0, // file attributes 0); // handle to file with attributes to copy if (hFile == INVALID_HANDLE_VALUE) { printf("failed to open image file\n"); return 0; } else { printf("image file %s opened\n",ifileName); return 1; } } // // CloseIfile // BOOL CloseIfile() { if (CloseHandle( hFile )) { printf("File closed\n"); return 1; } else { printf("File close failed\n"); return 0; } } // // ReadFByte // unsigned char ReadFByte() { DWORD dwRead; unsigned char lpBuf; ReadFile(hFile, // handle of file to read &lpBuf, // address of buffer that receives data sizeof(lpBuf), // number of bytes to read &dwRead, // address of number of bytes read 0); // address of structure for data printf("Read byte %Xh from ifile\n",lpBuf); return lpBuf; } // // WriteFByte // BOOL WriteFByte(unsigned char c) { DWORD dwBytesToWrite; DWORD dwWritten; unsigned char buff[2]; buff[0] = c; buff[1] = ' '; dwBytesToWrite = 2*sizeof(unsigned char); WriteFile(hFile, // handle to file to write to buff, // pointer to data to write to file dwBytesToWrite, // number of bytes to write &dwWritten, // pointer to number of bytes written 0); // pointer to structure for overlapped I/O); if (dwWritten == dwBytesToWrite) return 1; else return 0; } // // TransferFile // BOOL TransferFile() { int i; int fileSize = 100; int startAddr = 0; SetAddr(startAddr); for (i=0;i 0x0) { thesteering = (((thechar & 240)>>4)+'0'-1); //steering 0 maps to 0b0001XXXX thespeed = (((thechar & 15)+'0')); //speed 0 maps to 0bXXXX0000 WriteFByte(thespeed); WriteFByte(thesteering); } return 1; } // // SetAddr // (Not yet functional: Always starts at address 0) BOOL SetAddr(INT baseAddr) { int i; SendSByte('a'); for (i=0;i<8;i++) { SendSByte('0'); } return 1; }