Subj : Problems with API WriteFile To : borland.public.cpp.borlandcpp From : Jeff Baker Date : Tue Apr 27 2004 07:16 am Can someone please point out why this will not work correctly. I think the WriteFile function is called incorrectly but I can't figure out why. I thank you in advance. - Jeff #include #include #include void ProgramError(void) { LPVOID lpMsgBuf; textcolor(LIGHTRED); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,NULL ,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0 ,NULL); cprintf("Program Error: %s",lpMsgBuf); cprintf("Press any key to terminate program: "); getch(); exit(EXIT_FAILURE); } int main(int argc, char *argv[]) { DCB dcb; HANDLE hCom; BOOL fSuccess; int send_stream[]={4,0,0,0,4,0}; DWORD nNumberOfBytesToWrite; LPDWORD lpNumberOfBytesWritten; int x; char *pcCommPort; for (x = 0; x < argc; x++) { if (strcmpi("COM2",argv[x]) == 0) pcCommPort = "COM2"; else if (strcmpi("COM3",argv[x]) == 0) pcCommPort = "COM3"; else if (strcmpi("COM4",argv[x]) == 0) pcCommPort = "COM4"; else pcCommPort = "COM1"; } hCom = CreateFile(pcCommPort,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL ); if (hCom == INVALID_HANDLE_VALUE) ProgramError(); fSuccess = GetCommState(hCom,&dcb); if (!fSuccess) ProgramError(); dcb.BaudRate = CBR_19200; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; fSuccess = SetCommState(hCom,&dcb); if (!fSuccess) ProgramError(); textcolor(LIGHTGREEN); cprintf("Serial port %s successfully reconfigured\r\n",pcCommPort); nNumberOfBytesToWrite = 6; lpNumberOfBytesWritten = 0; fSuccess = WriteFile(hCom,send_stream,nNumberOfBytesToWrite,lpNumberOfBytesWritten,NULL ); if (fSuccess == 0) ProgramError(); if (nNumberOfBytesToWrite != *lpNumberOfBytesWritten) ProgramError(); cprintf("Data stream was send successfully to serial port %s\r\n",pcCommPort); getch(); exit(EXIT_SUCCESS); } .