📄 timeout.c
字号:
/************************************************************************
TimeOut.c
-- Main module for read & write timeout setting example program.
Description:
1.Select "setting..." menu item to set com port option.
2.Select "Open" menu item to open com port.
After selected "Open" from menu, you can select
"Timeout Demo" to test timeout status.
Polling Write:
sio_write() will return immediately. The data will be
copied to driver,but do not really wrtie to com port. This
operation will maybe let sio_write() return 0, means that
not enough buffer to write. Be careful this polling operation
will 'eat' a large amount of system resource, including
memory, processor time.
Block Write:
sio_write() will block until the all data has be write to
com port (But maybe some data is still in com port output
buffer when sio_write() return). You can check the sio_write()
elapsed time. This operation is suitable to using another
thread to write data. Then you can call sio_AbortWrite()
to abort write operation in main thread when you want to
stop writing.
Block Write (Timeout):
This operation is the same as "Block Write". The difference
is that, sio_write() will block until the tiemout is arrived
or the all data has be write to com port. You can decrease
the timeout value to check the difference.
Polling Read:
sio_read() will return immediately. sio_read() just checks
input buffer, gets all buffer data (maybe less than or equal
to that sio_read() want to read), then returns. If no data in
buffer, sio_read() return 0. Be careful this polling operation
will 'eat' a large amount of system resource, including memory,
processor time.
Block Read:
sio_read() will block until the input buffer data length is
equal to that sio_read() want to read. This operation is
suitable to using another thread to read data. Then you can
call sio_AbortRead() to abort read operation in main thread
when you want to stop reading.
Block Read (Total Timeout):
This operation is the saem as "Block Read". The difference
is that, sio_read() will block until timeout is arrived or
the input buffer data length is equal to that sio_read()
want to read.You can decrease the timeout value to check
the difference.
In this example, you can connect to terminal. Then you can
try 2 cases from terminal:
send 10240 bytes,
or wait the timeout is arrived.
Check the sio_read() elapsed time.
Block Read (Interval Timeout):
sio_read() will wait the first byte arrived, then begin
interval timeout checking.sio_read will block until the
interval timeout is arrived or the input buffer data length
is equal to that sio_read() want to read.
In this example, you can connect to terminal. Then you can
try 2 cases from terminal:
send 1 or 2 byte,
send 10240 bytes,
Check the sio_read() elapsed time.
Block Read ( Total+Interval Timeout ):
sio_read() will block until the timeout is arrived or the
input buffer data length is equdal to that sio_read() want
to read.
In this example, you can connect to terminal. Then you can
try 3 cases from terminal :
send 1 or 2 byte,
send 10240 bytes,
send > 10240 bytes
Check the sio_read() elapsed time.
3.Select "Close" menu item to close com port.
This program demo:
How to set write timeout(sio_SetWriteTimeouts());
How to set read timeout(sio_SetReadTimeouts());
How to abort write process(sio_AbortWrite());
How to abort read process(sio_AbortRead());
Use function:
sio_open, sio_close, sio_ioctl,
sio_flowctrl, sio_DTR, sio_RTS,
sio_read, sio_write,
sio_SetWriteTimesout, sio_SetReadTimeouts,
sio_AbortWrite, sio_AbortRead;
History: Date Author Comment
3/1/98 Casper Wrote it.
*************************************************************************/
#include <windows.h>
#include <windowsx.h>
#include "PComm.h"
#include "mxtool.h"
#include "resource.h"
#include "comm.h"
#define BUFLEN (10*1024)
HINSTANCE GhInst;
COMMDATA GCommData;
BOOL GbOpen;
HANDLE GhExit;
DWORD GDifTime;
DWORD GCount;
DWORD GCallCount;
char GszAppName[] = "TimeOut Setting Demo";
static char _GBuf[BUFLEN];
static HANDLE hWriteThread;
static HANDLE hReadThread;
static HWND GhWnd;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);
BOOL CALLBACK PortDlgProc(HWND hDlg,UINT iMsg,WPARAM wParam,LPARAM lParam);
BOOL CALLBACK AboutDlgProc(HWND hDlg,UINT iMsg,WPARAM wParam,LPARAM lParam);
BOOL CALLBACK WStatDlgProc(HWND hDlg,UINT iMsg,WPARAM wParam,LPARAM lParam);
BOOL CALLBACK RStatDlgProc(HWND hDlg,UINT iMsg,WPARAM wParam,LPARAM lParam);
static void SwitchMenu(HWND hwnd);
static BOOL OpenPort(void);
static BOOL ClosePort(void);
static BOOL PortSet(void);
static void ShowStatus(void);
UINT WriteProc( LPVOID pParam );
UINT ReadProc( LPVOID pParam );
void DemoWriteTimeout(int port,UINT testitem);
void DemoReadTimeout(int port,UINT testitem);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
PSTR szCmdLine,int iCmdShow)
{
WNDCLASSEX wndclass;
HWND hwnd;
MSG msg;
GhInst = hInstance;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = MAKEINTRESOURCE(IDM_TIMEOUT);
wndclass.lpszClassName = GszAppName;
wndclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd = CreateWindow(GszAppName,
GszAppName,
WS_OVERLAPPEDWINDOW ,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,NULL,
hInstance,
NULL);
GhWnd = hwnd;
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
switch(iMsg){
case WM_COMMAND:
switch(LOWORD(wParam)){
case IDM_PORT_OPEN:
OpenPort();
return 0;
case IDM_PORT_CLOSE:
ClosePort();
return 0;
case IDM_PORT_SETTING:{
COMMDATA bakdata = GCommData;
if(DialogBox(GhInst,MAKEINTRESOURCE(IDD_OPEN),hwnd,PortDlgProc)==IDCANCEL)
return 0;
if(GbOpen)
if (!PortSet())
GCommData = bakdata;
return 0;
}
case IDM_WRITE_POLL:
case IDM_WRITE_BLOCK:
case IDM_WRITE_TIMEOUT:
DemoWriteTimeout(GCommData.Port,LOWORD(wParam));
return 0;
case IDM_READ_POLL:
case IDM_READ_BLOCK:
case IDM_READ_BLOCK_T:
case IDM_READ_BLOCK_I:
case IDM_READ_BLOCK_TI:
DemoReadTimeout(GCommData.Port,LOWORD(wParam));
return 0;
case IDM_HELP_ABOUT:
DialogBox(GhInst,MAKEINTRESOURCE(IDD_ABOUT),hwnd,AboutDlgProc);
return 0;
case IDM_PORT_EXIT:
SendMessage(hwnd,WM_CLOSE,0,0L);
return 0;
}
break;
case WM_CREATE:
GCommData.Port = 1;
GCommData.ibaudrate = 14;
GCommData.iparity = 0;
GCommData.ibytesize = 3;
GCommData.istopbits = 0;
GCommData.BaudRate = B38400;
GCommData.Parity = P_NONE;
GCommData.ByteSize = BIT_8;
GCommData.StopBits = STOP_1;
GCommData.Hw = FALSE;
GCommData.Sw = FALSE;
GCommData.Dtr = TRUE;
GCommData.Rts = TRUE;
GbOpen = FALSE;
SwitchMenu(hwnd);
return 0;
case WM_CLOSE:
if(GbOpen)
SendMessage(hwnd,WM_COMMAND,IDM_PORT_CLOSE,0);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}
static void SwitchMenu(HWND hwnd)
{
HMENU hMenu;
int i;
hMenu = GetMenu(hwnd) ;
if(GbOpen){
EnableMenuItem( hMenu, IDM_PORT_OPEN,
MF_GRAYED | MF_DISABLED | MF_BYCOMMAND ) ;
EnableMenuItem( hMenu, IDM_PORT_CLOSE, MF_ENABLED | MF_BYCOMMAND ) ;
for(i=0;i<8;i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -