// Disconnects port void commDisconnect() { // disconnects with serial port if connected HMENU hMenu; // Check if we're connected if (CONNECTED(TTYInfo)) { // Menu hMenu = GetMenu(ghWndMain); // get the menu handle EnableMenuItem(hMenu, ID_FILE_CONNECT, MF_ENABLED); // enable the connect menu EnableMenuItem(hMenu, ID_FILE_DISCONNECT, MF_GRAYED | MF_DISABLED); // disable the connect menu EnableMenuItem(hMenu, ID_EDIT_SETTINGS, MF_ENABLED); // enable the TTY menu // lower DTR EscapeCommFunction(COMDEV(TTYInfo), CLRDTR); // Purge reads/writes, and I/O buffer PurgeComm(COMDEV(TTYInfo), PURGE_FLAGS); CloseHandle(COMDEV(TTYInfo)); // release the handle TTYInfo.fThreadDone = TRUE; // we want the thread to end CONNECTED(TTYInfo) = FALSE; // we are now disconnected } }
int commSendChar(char c) { DWORD numBytesWritten; // records number of character successfully written (not used, but necessary for WriteFile) OVERLAPPED overlapped = {0}; // overlap for preventing simulatious access to serial port if (!TTYInfo.fConnected) // make sure port is actually active return FALSE; // not connected :( //WriteFile(wMem->hComm, (LPBYTE)&wParam, 1, &wMem->numofBWriten, &overlaped if (!WriteFile(COMDEV(TTYInfo), &c, 1, &numBytesWritten, &overlapped)) // write byte to port return FALSE; // write failed :( CloseHandle(overlapped.hEvent); return TRUE; // done }
/*-----------------------------------------------------------------------------*/ void InitTTYInfo() { // Initialize general TTY info COMDEV(TTYInfo) = NULL; CONNECTED(TTYInfo) = FALSE; PORT(TTYInfo) = '0'; // setting to 0 since we know nothing about // the current COM port configuration BAUDRATE(TTYInfo) = CBR_9600; BYTESIZE(TTYInfo) = 8; PARITY(TTYInfo) = NOPARITY; STOPBITS(TTYInfo) = ONESTOPBIT; InitFont(); ClearTTYContents(); }
// Connects to, sets up and configures (if virgin) port. void commConnect() { DCB dcb; HMENU hMenu; // Make sure we're not connected if (!CONNECTED(TTYInfo)) { // Menu hMenu = GetMenu(ghWndMain); // get the menu handle EnableMenuItem(hMenu, ID_FILE_CONNECT, MF_GRAYED | MF_DISABLED); // disable the connect menu EnableMenuItem(hMenu, ID_FILE_DISCONNECT, MF_ENABLED); // enable the disconnect menu EnableMenuItem(hMenu, ID_EDIT_SETTINGS, MF_GRAYED | MF_DISABLED); // disable the TTY menu COMDEV(TTYInfo) = CreateFile(gszPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); FillMemory(&dcb, sizeof(dcb), 0); dcb.BaudRate = BAUDRATE(TTYInfo); // set the baud rate dcb.ByteSize = BYTESIZE(TTYInfo); // set the data bits dcb.fParity = PARITY(TTYInfo); // set the parity dcb.StopBits = STOPBITS(TTYInfo); // set the stop bits SetCommState(COMDEV(TTYInfo), &dcb); // update the COM port configuration CONNECTED(TTYInfo) = TRUE; // we are now connected (hopefully) } // Start reading thread TTYInfo.hThread = CreateThread( NULL, // security attributes 0, // inital thread stack size, in bytes commReadThread, // address of thread function ghWndTerm, // argument for new thread 0, // creation flags &TTYInfo.hThread ); // address of returned thread ID TTYInfo.fConnected = TRUE; }
/*----------------------------------------------------------------------------- FUNCTION: WriterGeneric(char *, DWORD) PURPOSE: Handles sending all types of data PARAMETER: lpBuf - pointer to data buffer dwToWrite - size of buffer HISTORY: Date: Author: Comment: 10/27/95 AllenD Wrote it -----------------------------------------------------------------------------*/ void WriterGeneric(char * lpBuf, DWORD dwToWrite) { OVERLAPPED osWrite = {0}; HANDLE hArray[2]; DWORD dwWritten; DWORD dwRes; // // If no writing is allowed, then just return // if (NOWRITING(TTYInfo)) return ; // // create this writes overlapped structure hEvent // osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (osWrite.hEvent == NULL) ErrorInComm("CreateEvent (overlapped write hEvent)"); hArray[0] = osWrite.hEvent; hArray[1] = ghThreadExitEvent; // // issue write // if (!WriteFile(COMDEV(TTYInfo), lpBuf, dwToWrite, &dwWritten, &osWrite)) { if (GetLastError() == ERROR_IO_PENDING) { // // write is delayed // dwRes = WaitForMultipleObjects(2, hArray, FALSE, INFINITE); switch(dwRes) { // // write event set // case WAIT_OBJECT_0: SetLastError(ERROR_SUCCESS); if (!GetOverlappedResult(COMDEV(TTYInfo), &osWrite, &dwWritten, FALSE)) { if (GetLastError() == ERROR_OPERATION_ABORTED) UpdateStatus("Write aborted\r\n"); else ErrorInComm("GetOverlappedResult(in Writer)"); } if (dwWritten != dwToWrite) { if ((GetLastError() == ERROR_SUCCESS) && SHOWTIMEOUTS(TTYInfo)) UpdateStatus("Write timed out. (overlapped)\r\n"); else ErrorReporter("Error writing data to port (overlapped)"); } break; // // thread exit event set // case WAIT_OBJECT_0 + 1: break; // // wait timed out // case WAIT_TIMEOUT: UpdateStatus("Wait Timeout in WriterGeneric.\r\n"); break; case WAIT_FAILED: default: ErrorInComm("WaitForMultipleObjects (WriterGeneric)"); break; } } else // // writefile failed, but it isn't delayed // ErrorInComm("WriteFile (in Writer)"); } else { // // writefile returned immediately // if (dwWritten != dwToWrite) UpdateStatus("Write timed out. (immediate)\r\n"); } CloseHandle(osWrite.hEvent); return; }