В исходниках терминалки есть файлик serail.c — в нем и есть код работы с ком портом, только он уж очень корявый.
Исходники блокнота даже не смотрел — судя по всему это и не надо.
Вот код программы (проверить не могу — нет термопечи и вообще нет ком порта)
#include <windows.h>
#define MESS_SERIAL (WM_USER+1)
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL OpenPort(int port,int baud,int HwFc, HWND handle);
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPInst, LPSTR lpCmd, int nCmd)
{
WNDCLASSW wc;
HWND hWnd;
MSG msg;
wchar_t *szWndCls = L"STCLS";
memset(&wc, 0, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.hInstance = hInst;
wc.hCursor = LoadCursorW(0, IDC_ARROW);
wc.lpszClassName = szWndCls;
if (!RegisterClassW(&wc))
return 1;
hWnd = CreateWindowW(szWndCls, L"Simple Terminal", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hInst, 0);
ShowWindow(hWnd, nCmd);
UpdateWindow(hWnd);
while (GetMessage(&msg, 0, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HWND hEdit;
switch (uMsg) {
case WM_CREATE: {
CREATESTRUCT *CS;
CS = (CREATESTRUCT *)lParam;
hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", 0, WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY, 0, 0, 0, 0, hWnd, 0, CS->hInstance, 0);
SendMessage(hEdit, WM_SETFONT, (WPARAM)CreateFontA(14, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, DEFAULT_PITCH, "dejavu sans mono"), 0);
OpenPort(1, 9600, 0, hWnd);
return 0;
}
case WM_SIZE: {
SetWindowPos(hEdit, 0, 0, 0, LOWORD(lParam), HIWORD(lParam), SWP_NOZORDER);
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
case WM_CLOSE: {
PostQuitMessage(0);
return 0;
}
case MESS_SERIAL: {
int iLength;
char *szTmp;
szTmp = (char *)lParam;
szTmp[wParam] = 0;
iLength = GetWindowTextLengthW(hEdit);
SendMessage(hEdit, EM_SETSEL, iLength, iLength);
SendMessageA(hEdit, EM_REPLACESEL, 0, (LPARAM)szTmp);
return 0;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
/* From serial.c */
#include <stdio.h>
// Functions:
HANDLE StartCommThread(void);
DWORD WINAPI ThreadProc(void *p);
// Variables:
HANDLE SerialPort=NULL;
HANDLE Thread;
HWND handle=NULL;
int StopThread=0;
int FlowControl=0;
void ShowLastError(void)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox(NULL, (LPTSTR)lpMsgBuf, TEXT("Error"), MB_OK|MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
}
BOOL OpenPort(int port,int baud,int HwFc, HWND hwnd)
{
HANDLE Comport;
DCB myDCB;
COMMTIMEOUTS CTout;
char str[100];
FlowControl = HwFc;
if (port > 9)
wsprintfA(str,"\\\\.\\COM%d",port);
else
wsprintfA(str,"COM%d",port);
Comport = CreateFileA(str,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (Comport == INVALID_HANDLE_VALUE)
return FALSE;
if (!SetupComm(Comport,350,20))
return FALSE;
if (!GetCommState(Comport,&myDCB))
return FALSE;
myDCB.fInX = FALSE;
myDCB.fOutX = FALSE;
myDCB.fOutxDsrFlow = FALSE;
if (HwFc)
{
myDCB.fOutxCtsFlow = TRUE;
myDCB.fRtsControl = RTS_CONTROL_HANDSHAKE;
}
else
{
myDCB.fOutxCtsFlow = FALSE;
}
myDCB.BaudRate = baud;
myDCB.DCBlength = sizeof(DCB);
myDCB.fBinary = 1;
myDCB.fParity = 0;
myDCB.fDtrControl = DTR_CONTROL_DISABLE;
myDCB.fDsrSensitivity = 0;
myDCB.fTXContinueOnXoff = 1;
myDCB.fNull = 0;
myDCB.fRtsControl = RTS_CONTROL_DISABLE;
myDCB.fDummy2 = 0;
myDCB.wReserved = 0;
myDCB.Parity = NOPARITY;
myDCB.StopBits = ONESTOPBIT;
myDCB.wReserved1 = 0;
myDCB.ByteSize = 8;
if (!SetCommState(Comport,&myDCB)) {
ShowLastError();
return FALSE;
}
CTout.ReadIntervalTimeout = 0xffffffff;
CTout.ReadTotalTimeoutMultiplier = 0;
CTout.ReadTotalTimeoutConstant = 0;
CTout.WriteTotalTimeoutMultiplier = 0;
CTout.WriteTotalTimeoutConstant = 5000;
SetCommTimeouts(Comport,&CTout);
EscapeCommFunction(Comport,SETDTR);
PurgeComm(Comport,PURGE_TXCLEAR | PURGE_RXCLEAR);
handle = hwnd;
SerialPort = Comport;
StartCommThread();
return TRUE;
}
void CloseSerialPort(void)
{
if (!SerialPort) return;
if (Thread)
{
StopThread = TRUE;
WaitForSingleObject(Thread,2000);
CloseHandle(Thread);
Thread = NULL;
StopThread = FALSE;
}
PurgeComm(SerialPort,PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle(SerialPort);
SerialPort = NULL;
}
void PutSerialChar(int c)
{
int Cnt;
DWORD ModemStat;
DWORD ticks;
int Cts=1;
ticks = GetTickCount();
if (FlowControl)
{
while (Cts)
{
if (!GetCommModemStatus(SerialPort, &ModemStat))
{
ShowLastError();
return;
}
Cts = !(ModemStat & MS_CTS_ON);
if (GetTickCount() > ticks + 1000)
break;
}
}
WriteFile(SerialPort,&c,1,(LPDWORD)&Cnt,NULL);
}
HANDLE StartCommThread(void)
{
int ThreadID;
StopThread = FALSE;
Thread = CreateThread(NULL,4096,ThreadProc,SerialPort,0,(LPDWORD)&ThreadID);
return Thread;
}
DWORD WINAPI ThreadProc(void *p)
{
int Cnt;
char buf[256];
if (!handle)
return 0;
for(;;)
{
if (ReadFile(SerialPort,&buf,255,(LPDWORD)&Cnt,NULL) && Cnt)
{
SendMessage(handle,MESS_SERIAL,(unsigned int)Cnt,(unsigned long)buf);
}
else
Sleep(50);
if (StopThread)
break;
}
return 0;
}
int SerialPortIsOpen(void)
{
if (SerialPort)
return TRUE;
return FALSE;
}
BOOL SerialIsChar(void)
{
int Cnt,Avail,Remain;
int buf;
PeekNamedPipe(SerialPort, (LPVOID)&buf, (DWORD) 1, (LPDWORD)&Cnt,(LPDWORD)&Avail, (LPDWORD)&Remain);
if (Avail)
return TRUE;
return FALSE;
}
int SerialGetChar(void)
{
char ch;
int Cnt;
if (!SerialPort)
return EOF;
ReadFile(SerialPort,&ch,1,(LPDWORD)&Cnt,NULL);
if (!Cnt)
return EOF;
return (int) ch;
}