hWnd = (HWND)pArg;
hwndSText = GetDlgItem (hWnd, ID_SENDTEXT);
while (1) {
rc = WaitForSingleObject (g_hSendEvent, INFINITE);
if (rc == WAIT_OBJECT_0) {
if (!fContinue)
break;
// Disable send button while sending.
EnableWindow (GetDlgItem (hWnd, ID_SENDBTN), FALSE);
// Get the text, terminate the line and convert to ansi
GetWindowText (hwndSText, szText, dim(szText));
StringCchCat (szText, dim(szText), TEXT ("\r\n"));
wcstombs_s (&siz, szAnsi, sizeof (szAnsi), szText, _TRUNCATE);
// Write to the serial port
rc = WriteFile (hComPort, szText,
lstrlen (szText)*sizeof (TCHAR),&cBytes, 0);
if (rc) {
// Copy sent text to output window.
SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0,
(LPARAM)TEXT (" >"));
SetWindowText (hwndSText, TEXT ("")); // Clear text box
} else {
// Else, print error message.
wsprintf (szText, TEXT ("Send failed rc=%d\r\n"),
GetLastError());
DWORD dwErr = 0;
COMSTAT Stat;
if (ClearCommError (hComPort, &dwErr, &Stat)) {
printf ("fail\n");
}
}
// Put text in receive text box.
SendDlgItemMessage (hWnd, ID_RCVTEXT, EM_REPLACESEL, 0,
(LPARAM)szText);
EnableWindow (GetDlgItem (hWnd, ID_SENDBTN), TRUE);
} else
break;
}
return 0;
}