/**************************************** / Serial Communication from PDA to Mega32 / ECE 476 Final Project / Diego Asturias (da65) / John Sun (jzs3) / Mar. 21, 2006 /****************************************/ //include files #include #include #include int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPWSTR lpCmdLine,int nCmdShow) { // data buffer char outBuffer[5]; // transfer six data bytes and one stop byte outBuffer[0] = 127; // start byte has value 127 outBuffer[4] = 126; // stop byte has value 126 // Extract information from text file // ---------------------------------- FILE *f; char buf[10]; f = fopen("carcontrol.txt","r"); fgets(buf, 10, f); // extract six bytes outBuffer[1] = atoi(buf); fgets(buf, 10, f); outBuffer[2] = atoi(buf); fgets(buf, 10, f); outBuffer[3] = atoi(buf); fclose(f); // close file FILE *e; // write to new file for debug e = fopen("test.txt","w"); sprintf(buf,"%d\n",outBuffer[1]); fputs(buf,e); sprintf(buf,"%d\n",outBuffer[2]); fputs(buf,e); sprintf(buf,"%d\n",outBuffer[3]); fputs(buf,e); fclose(e); // Open the serial port // -------------------- // create port variables DWORD dwError; unsigned long dwNumBytesWritten; HANDLE hPort = CreateFile(TEXT("COM1:"), // Pointer to the name of the port GENERIC_READ | GENERIC_WRITE, // Access (read-write) mode 0, // Share mode NULL, // Pointer to the security attribute OPEN_EXISTING, // How to open the serial port 0, // Port attributes NULL); // Handle to port with attribute to copy // Set up serial port // ------------------ DCB PortDCB; // Initialize the DCBlength member. PortDCB.DCBlength = sizeof (DCB); // Get the default port setting information. GetCommState (hPort, &PortDCB); // Change the DCB structure settings. PortDCB.BaudRate = 9600; // Current baud PortDCB.fBinary = TRUE; // Binary mode; no EOF check PortDCB.fParity = FALSE; // Enable parity checking PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control PortDCB.fDtrControl = DTR_CONTROL_DISABLE;// DTR flow control type PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity PortDCB.fTXContinueOnXoff = FALSE; // XOFF continues Tx PortDCB.fOutX = FALSE; // No XON/XOFF out flow control PortDCB.fInX = FALSE; // No XON/XOFF in flow control PortDCB.fErrorChar = FALSE; // Disable error replacement PortDCB.fNull = FALSE; // Disable null stripping PortDCB.fRtsControl = RTS_CONTROL_DISABLE;// RTS flow control PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on error PortDCB.ByteSize = 8; // Number of bits/byte, 4-8 PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2 // Configure the port according to the specifications of the DCB // structure. if (!SetCommState (hPort, &PortDCB)) { // Could not configure the serial port. dwError = GetLastError(); MessageBox (NULL, TEXT("Unable to configure the serial port"), TEXT("Error"), MB_OK); return FALSE; } Sleep(100); // Write to MCU // ------------ WriteFile (hPort, // Port handle &outBuffer, // Pointer to the data to write 5, // Number of bytes to write &dwNumBytesWritten, // Pointer to the number of bytes written NULL // Must be NULL for Windows CE ); Sleep(100); // Close serial port // ----------------- CloseHandle(hPort); //LPCWSTR temp = dwNumBytesWritten; //MessageBox(NULL, (const unsigned short *)buff, TEXT("Message"), MB_OK); return 0; }