Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /branches/ttcomtester/teraterm/teraterm/commlib.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6893 - (hide annotations) (download) (as text)
Wed Aug 9 13:08:44 2017 UTC (6 years, 8 months ago) by doda
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 35080 byte(s)
シリアルポート/パイプが開けなかった時のメッセージ強化。 Ticket: #36630

以下のエラーは起き易いので、専用のメッセージを表示する。

・ERROR_FILE_NOT_FOUND(0x02)
・ERROR_ACCESS_DENIED(0x05)

他のエラーはエラーコードを表示する。
他にも頻発するようなエラーがあった場合は別途専用メッセージを追加する。
1 doda 6806 /*
2     * Copyright (C) 1994-1998 T. Teranishi
3     * (C) 2005-2017 TeraTerm Project
4     * All rights reserved.
5     *
6 doda 6841 * Redistribution and use in source and binary forms, with or without
7     * modification, are permitted provided that the following conditions
8     * are met:
9 doda 6806 *
10 doda 6841 * 1. Redistributions of source code must retain the above copyright
11     * notice, this list of conditions and the following disclaimer.
12     * 2. Redistributions in binary form must reproduce the above copyright
13     * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the distribution.
15     * 3. The name of the author may not be used to endorse or promote products
16     * derived from this software without specific prior written permission.
17 doda 6806 *
18 doda 6841 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21     * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 doda 6806 */
29 maya 3857 /* IPv6 modification is Copyright (C) 2000, 2001 Jun-ya KATO <kato@win6.jp> */
30    
31     /* TERATERM.EXE, Communication routines */
32     #include "teraterm.h"
33     #include "tttypes.h"
34     #include "tt_res.h"
35     #include <process.h>
36    
37     #include "ttcommon.h"
38     #include "ttwsk.h"
39     #include "ttlib.h"
40     #include "ttfileio.h"
41     #include "ttplug.h" /* TTPLUG */
42    
43     #include "commlib.h"
44     #include <winsock2.h>
45     #include <ws2tcpip.h>
46     #include <stdio.h> /* for _snprintf() */
47     #include <time.h>
48 doda 3932 #include <locale.h>
49 maya 3857
50     static SOCKET OpenSocket(PComVar);
51     static void AsyncConnect(PComVar);
52     static int CloseSocket(SOCKET);
53    
54     /* create socket */
55     static SOCKET OpenSocket(PComVar cv)
56     {
57     cv->s = cv->res->ai_family;
58     cv->s = Psocket(cv->res->ai_family, cv->res->ai_socktype, cv->res->ai_protocol);
59     return cv->s;
60     }
61    
62     /* connect with asynchronous mode */
63     static void AsyncConnect(PComVar cv)
64     {
65     int Err;
66     BOOL BBuf;
67     BBuf = TRUE;
68     /* set synchronous mode */
69     PWSAAsyncSelect(cv->s,cv->HWin,0,0);
70 doda 6801 Psetsockopt(cv->s,(int)SOL_SOCKET,SO_OOBINLINE,(char *)&BBuf,sizeof(BBuf));
71 maya 3857 /* set asynchronous mode */
72     PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMOPEN, FD_CONNECT);
73    
74     // �z�X�g���������������������������A�����I���\�P�b�g���N���[�Y�����A
75     // �����������L�����Z���������B�l��0�������������������B
76     // (2007.1.11 yutaka)
77     if (*cv->ConnetingTimeout > 0) {
78     SetTimer(cv->HWin, IdCancelConnectTimer, *cv->ConnetingTimeout * 1000, NULL);
79     }
80    
81     /* WM_USER_COMMOPEN occurs, CommOpen is called, then CommStart is called */
82     Err = Pconnect(cv->s, cv->res->ai_addr, cv->res->ai_addrlen);
83     if (Err != 0) {
84     Err = PWSAGetLastError();
85     if (Err == WSAEWOULDBLOCK) {
86     /* Do nothing */
87     } else if (Err!=0 ) {
88     PostMessage(cv->HWin, WM_USER_COMMOPEN,0,
89     MAKELONG(FD_CONNECT,Err));
90     }
91     }
92     }
93    
94     /* close socket */
95     static int CloseSocket(SOCKET s)
96     {
97     return Pclosesocket(s);
98     }
99    
100     #define CommInQueSize 8192
101     #define CommOutQueSize 2048
102     #define CommXonLim 2048
103     #define CommXoffLim 2048
104    
105     #define READENDNAME "ReadEnd"
106     #define WRITENAME "Write"
107     #define READNAME "Read"
108     #define PRNWRITENAME "PrnWrite"
109    
110     static HANDLE ReadEnd;
111     static OVERLAPPED wol, rol;
112    
113     // Winsock async operation handle
114     static HANDLE HAsync=0;
115    
116     BOOL TCPIPClosed = TRUE;
117    
118     /* Printer port handle for
119     direct pass-thru printing */
120     static HANDLE PrnID = INVALID_HANDLE_VALUE;
121     static BOOL LPTFlag;
122    
123     // Initialize ComVar.
124     // This routine is called only once
125     // by the initialization procedure of Tera Term.
126     void CommInit(PComVar cv)
127     {
128     cv->Open = FALSE;
129     cv->Ready = FALSE;
130    
131     // log-buffer variables
132     cv->HLogBuf = 0;
133     cv->HBinBuf = 0;
134     cv->LogBuf = NULL;
135     cv->BinBuf = NULL;
136     cv->LogPtr = 0;
137     cv->LStart = 0;
138     cv->LCount = 0;
139     cv->BinPtr = 0;
140     cv->BStart = 0;
141     cv->BCount = 0;
142     cv->DStart = 0;
143     cv->DCount = 0;
144     cv->BinSkip = 0;
145     cv->FilePause = 0;
146     cv->ProtoFlag = FALSE;
147     /* message flag */
148     cv->NoMsg = 0;
149 doda 6662
150 doda 6788 cv->isSSH = 0;
151     cv->TitleRemote[0] = '\0';
152    
153 doda 6662 cv->NotifyIcon = NULL;
154 maya 3857 }
155    
156     /* reset a serial port which is already open */
157     void CommResetSerial(PTTSet ts, PComVar cv, BOOL ClearBuff)
158     {
159     DCB dcb;
160     DWORD DErr;
161     COMMTIMEOUTS ctmo;
162    
163     if (! cv->Open ||
164     (cv->PortType != IdSerial)) {
165     return;
166     }
167    
168     ClearCommError(cv->ComID,&DErr,NULL);
169     SetupComm(cv->ComID,CommInQueSize,CommOutQueSize);
170     /* flush input and output buffers */
171     if (ClearBuff) {
172     PurgeComm(cv->ComID, PURGE_TXABORT | PURGE_RXABORT |
173     PURGE_TXCLEAR | PURGE_RXCLEAR);
174     }
175    
176     memset(&ctmo,0,sizeof(ctmo));
177     ctmo.ReadIntervalTimeout = MAXDWORD;
178     ctmo.WriteTotalTimeoutConstant = 500;
179     SetCommTimeouts(cv->ComID,&ctmo);
180     cv->InBuffCount = 0;
181     cv->InPtr = 0;
182     cv->OutBuffCount = 0;
183     cv->OutPtr = 0;
184    
185     cv->DelayPerChar = ts->DelayPerChar;
186     cv->DelayPerLine = ts->DelayPerLine;
187    
188     memset(&dcb,0,sizeof(DCB));
189     dcb.DCBlength = sizeof(DCB);
190 maya 3874 dcb.BaudRate = ts->Baud;
191 maya 3857 dcb.fBinary = TRUE;
192     switch (ts->Parity) {
193 doda 4849 case IdParityNone:
194     dcb.Parity = NOPARITY;
195     break;
196     case IdParityOdd:
197     dcb.fParity = TRUE;
198     dcb.Parity = ODDPARITY;
199     break;
200 maya 3857 case IdParityEven:
201     dcb.fParity = TRUE;
202     dcb.Parity = EVENPARITY;
203     break;
204 doda 4849 case IdParityMark:
205 maya 3857 dcb.fParity = TRUE;
206 doda 4849 dcb.Parity = MARKPARITY;
207 maya 3857 break;
208 doda 4849 case IdParitySpace:
209     dcb.fParity = TRUE;
210     dcb.Parity = SPACEPARITY;
211 maya 3857 break;
212     }
213    
214     dcb.fDtrControl = DTR_CONTROL_ENABLE;
215     dcb.fRtsControl = RTS_CONTROL_ENABLE;
216     switch (ts->Flow) {
217     case IdFlowX:
218     dcb.fOutX = TRUE;
219     dcb.fInX = TRUE;
220     dcb.XonLim = CommXonLim;
221     dcb.XoffLim = CommXoffLim;
222     dcb.XonChar = XON;
223     dcb.XoffChar = XOFF;
224     break;
225     case IdFlowHard:
226     dcb.fOutxCtsFlow = TRUE;
227     dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
228     break;
229     }
230    
231     switch (ts->DataBit) {
232     case IdDataBit7:
233     dcb.ByteSize = 7;
234     break;
235     case IdDataBit8:
236     dcb.ByteSize = 8;
237     break;
238     }
239     switch (ts->StopBit) {
240     case IdStopBit1:
241     dcb.StopBits = ONESTOPBIT;
242     break;
243 doda 4849 case IdStopBit15:
244     dcb.StopBits = ONE5STOPBITS;
245     break;
246 maya 3857 case IdStopBit2:
247     dcb.StopBits = TWOSTOPBITS;
248     break;
249     }
250    
251     SetCommState(cv->ComID, &dcb);
252    
253     /* enable receive request */
254     SetCommMask(cv->ComID,0);
255     SetCommMask(cv->ComID,EV_RXCHAR);
256     }
257    
258 yutakapon 4860 // ���O�t���p�C�v�����������������`�F�b�N�����B
259     // \\ServerName\pipe\PipeName
260     //
261     // return 0: ������
262     // -1: �s��
263     // (2012.3.10 yutaka)
264     int CheckNamedPipeFormat(char *p, int size)
265     {
266     int ret = -1;
267     char *s;
268    
269     if (size <= 8)
270     goto error;
271    
272     if (p[0] == '\\' && p[1] == '\\') {
273     s = strchr(&p[2], '\\');
274     if (s && _strnicmp(s+1, "pipe\\", 5) == 0) {
275     ret = 0;
276     }
277     }
278    
279     error:
280     return (ret);
281     }
282    
283 maya 3857 void CommOpen(HWND HW, PTTSet ts, PComVar cv)
284     {
285 yutakapon 4857 char ErrMsg[21+256];
286     char P[50+256];
287 maya 3857
288     MSG Msg;
289     ADDRINFO hints;
290     char pname[NI_MAXSERV];
291    
292     BOOL InvalidHost;
293    
294     char uimsg[MAX_UIMSG];
295    
296 yutakapon 4860 // �z�X�g�������O�t���p�C�v�����������������B
297     if (ts->PortType == IdTCPIP) {
298     if (CheckNamedPipeFormat(ts->HostName, strlen(ts->HostName)) == 0) {
299     ts->PortType = IdNamedPipe;
300     }
301     }
302    
303 maya 3857 /* initialize ComVar */
304     cv->InBuffCount = 0;
305     cv->InPtr = 0;
306     cv->OutBuffCount = 0;
307     cv->OutPtr = 0;
308     cv->HWin = HW;
309     cv->Ready = FALSE;
310     cv->Open = FALSE;
311     cv->PortType = ts->PortType;
312     cv->ComPort = 0;
313     cv->RetryCount = 0;
314     cv->RetryWithOtherProtocol = TRUE;
315     cv->s = INVALID_SOCKET;
316     cv->ComID = INVALID_HANDLE_VALUE;
317     cv->CanSend = TRUE;
318     cv->RRQ = FALSE;
319     cv->SendKanjiFlag = FALSE;
320     cv->SendCode = IdASCII;
321     cv->EchoKanjiFlag = FALSE;
322     cv->EchoCode = IdASCII;
323     cv->Language = ts->Language;
324     cv->CRSend = ts->CRSend;
325     cv->KanjiCodeEcho = ts->KanjiCode;
326     cv->JIS7KatakanaEcho = ts->JIS7Katakana;
327     cv->KanjiCodeSend = ts->KanjiCodeSend;
328     cv->JIS7KatakanaSend = ts->JIS7KatakanaSend;
329     cv->KanjiIn = ts->KanjiIn;
330     cv->KanjiOut = ts->KanjiOut;
331     cv->RussHost = ts->RussHost;
332     cv->RussClient = ts->RussClient;
333     cv->DelayFlag = TRUE;
334     cv->DelayPerChar = ts->DelayPerChar;
335     cv->DelayPerLine = ts->DelayPerLine;
336     cv->TelBinRecv = FALSE;
337     cv->TelBinSend = FALSE;
338     cv->TelFlag = FALSE;
339     cv->TelMode = FALSE;
340     cv->IACFlag = FALSE;
341     cv->TelCRFlag = FALSE;
342     cv->TelCRSend = FALSE;
343     cv->TelCRSendEcho = FALSE;
344     cv->TelAutoDetect = ts->TelAutoDetect; /* TTPLUG */
345     cv->Locale = ts->Locale;
346 doda 3932 cv->locale = _create_locale(LC_ALL, cv->Locale);
347 maya 3857 cv->CodePage = &ts->CodePage;
348     cv->ConnetingTimeout = &ts->ConnectingTimeout;
349     cv->LastSendTime = time(NULL);
350     cv->LineModeBuffCount = 0;
351     cv->Flush = FALSE;
352     cv->FlushLen = 0;
353     cv->TelLineMode = FALSE;
354    
355     if ((ts->PortType!=IdSerial) && (strlen(ts->HostName)==0))
356     {
357     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
358     return;
359     }
360    
361     switch (ts->PortType) {
362     case IdTCPIP:
363     cv->TelFlag = (ts->Telnet > 0);
364     if (ts->EnableLineMode) {
365     cv->TelLineMode = TRUE;
366     }
367     if (! LoadWinsock()) {
368     if (cv->NoMsg==0) {
369     get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
370     get_lang_msg("MSG_WINSOCK_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Cannot use winsock", ts->UILanguageFile);
371     MessageBox(cv->HWin,ts->UIMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
372     }
373     InvalidHost = TRUE;
374     }
375     else {
376     TTXOpenTCP(); /* TTPLUG */
377     cv->Open = TRUE;
378     /* resolving address */
379     memset(&hints, 0, sizeof(hints));
380     hints.ai_family = ts->ProtocolFamily;
381     hints.ai_socktype = SOCK_STREAM;
382     hints.ai_protocol = IPPROTO_TCP;
383     _snprintf_s(pname, sizeof(pname), _TRUNCATE, "%d", ts->TCPPort);
384    
385 doda 4089 HAsync = PWSAAsyncGetAddrInfo(HW, WM_USER_GETHOST,
386 maya 3857 ts->HostName, pname, &hints, &cv->res0);
387     if (HAsync == 0)
388     InvalidHost = TRUE;
389     else {
390     cv->ComPort = 1; // set "getting host" flag
391     // (see CVTWindow::OnSysCommand())
392     do {
393     if (GetMessage(&Msg,0,0,0)) {
394     if ((Msg.hwnd==HW) &&
395     ((Msg.message == WM_SYSCOMMAND) &&
396     ((Msg.wParam & 0xfff0) == SC_CLOSE) ||
397     (Msg.message == WM_COMMAND) &&
398     (LOWORD(Msg.wParam) == ID_FILE_EXIT) ||
399     (Msg.message == WM_CLOSE))) { /* Exit when the user closes Tera Term */
400     PWSACancelAsyncRequest(HAsync);
401     CloseHandle(HAsync);
402     HAsync = 0;
403     cv->ComPort = 0; // clear "getting host" flag
404     PostMessage(HW,Msg.message,Msg.wParam,Msg.lParam);
405     return;
406     }
407     if (Msg.message != WM_USER_GETHOST) { /* Prosess messages */
408     TranslateMessage(&Msg);
409     DispatchMessage(&Msg);
410     }
411     }
412     else {
413     return;
414     }
415     } while (Msg.message!=WM_USER_GETHOST);
416     cv->ComPort = 0; // clear "getting host" flag
417     CloseHandle(HAsync);
418     HAsync = 0;
419     InvalidHost = WSAGETASYNCERROR(Msg.lParam) != 0;
420     }
421     } /* if (!LoadWinsock()) */
422    
423     if (InvalidHost) {
424     if (cv->NoMsg==0) {
425     get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
426     get_lang_msg("MSG_INVALID_HOST_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Invalid host", ts->UILanguageFile);
427     MessageBox(cv->HWin,ts->UIMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
428     }
429     goto BreakSC;
430     }
431     for (cv->res = cv->res0; cv->res; cv->res = cv->res->ai_next) {
432     cv->s = OpenSocket(cv);
433     if (cv->s == INVALID_SOCKET) {
434     CloseSocket(cv->s);
435     continue;
436     }
437     /* start asynchronous connect */
438     AsyncConnect(cv);
439     break; /* break for-loop immediately */
440     }
441     break;
442    
443     case IdSerial:
444     InitFileIO(IdSerial); /* TTPLUG */
445     TTXOpenFile(); /* TTPLUG */
446     _snprintf_s(P, sizeof(P), _TRUNCATE, "COM%d", ts->ComPort);
447     strncpy_s(ErrMsg, sizeof(ErrMsg),P, _TRUNCATE);
448     strncpy_s(P, sizeof(P),"\\\\.\\", _TRUNCATE);
449     strncat_s(P, sizeof(P),ErrMsg, _TRUNCATE);
450 doda 6893 cv->ComID = PCreateFile(P, GENERIC_READ | GENERIC_WRITE, 0, NULL,
451     OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
452 maya 3857 if (cv->ComID == INVALID_HANDLE_VALUE ) {
453 doda 6893 DWORD err = GetLastError();
454 maya 3857
455 doda 6893 switch (err) {
456     case ERROR_FILE_NOT_FOUND:
457     get_lang_msg("MSG_CANTOPEN_ERROR_NOTFOUND", ts->UIMsg, sizeof(ts->UIMsg), "Cannot open %s. Not found.", ts->UILanguageFile);
458     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, ts->UIMsg, &P[4]);
459     break;
460     case ERROR_ACCESS_DENIED:
461     get_lang_msg("MSG_CANTOPEN_ERROR_DENIED", ts->UIMsg, sizeof(ts->UIMsg), "Cannot open %s. Access denied.", ts->UILanguageFile);
462     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, ts->UIMsg, &P[4]);
463     break;
464     default:
465     get_lang_msg("MSG_CANTOPEN_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Cannot open %s. (0x%08x)", ts->UILanguageFile);
466     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, ts->UIMsg, &P[4], err);
467     break;
468     }
469    
470 maya 3857 if (cv->NoMsg==0) {
471     get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
472     MessageBox(cv->HWin,ErrMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
473     }
474     InvalidHost = TRUE;
475     }
476     else {
477     cv->Open = TRUE;
478     cv->ComPort = ts->ComPort;
479     CommResetSerial(ts, cv, ts->ClearComBuffOnOpen);
480     if (!ts->ClearComBuffOnOpen) {
481     cv->RRQ = TRUE;
482     }
483    
484     /* notify to VT window that Comm Port is open */
485     PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
486     InvalidHost = FALSE;
487    
488     SetCOMFlag(ts->ComPort);
489     }
490     break; /* end of "case IdSerial:" */
491    
492     case IdFile:
493     InitFileIO(IdFile); /* TTPLUG */
494     TTXOpenFile(); /* TTPLUG */
495 doda 6893 cv->ComID = PCreateFile(ts->HostName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
496 maya 3857 InvalidHost = (cv->ComID == INVALID_HANDLE_VALUE);
497     if (InvalidHost) {
498     if (cv->NoMsg==0) {
499     get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
500 doda 6892 get_lang_msg("MSG_CANTOPEN_FILE_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Cannot open file", ts->UILanguageFile);
501 maya 3857 MessageBox(cv->HWin,ts->UIMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
502     }
503     }
504     else {
505     cv->Open = TRUE;
506     PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
507     }
508     break;
509 yutakapon 4857
510     case IdNamedPipe:
511     InitFileIO(IdNamedPipe); /* TTPLUG */
512     TTXOpenFile(); /* TTPLUG */
513 doda 6435
514 yutakapon 4858 memset(P, 0, sizeof(P));
515 yutakapon 4857 strncpy_s(P, sizeof(P), ts->HostName, _TRUNCATE);
516 yutakapon 4858
517     // ���O�t���p�C�v�����������������`�F�b�N�����B
518 yutakapon 4860 if (CheckNamedPipeFormat(P, strlen(P)) < 0) {
519     InvalidHost = TRUE;
520    
521 doda 6435 _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE,
522 yutakapon 5921 "Invalid pipe name (%d)\n\n"
523 yutakapon 4858 "A valid pipe name has the form\n"
524 doda 6435 "\"\\\\<ServerName>\\pipe\\<PipeName>\"",
525 yutakapon 4860 GetLastError());
526 yutakapon 4858 get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
527     MessageBox(cv->HWin,ErrMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
528     break;
529     }
530    
531 doda 6893 cv->ComID = PCreateFile(P, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
532     0, // �u���b�L���O���[�h������(FILE_FLAG_OVERLAPPED ���w��������)
533     NULL);
534 yutakapon 4857 if (cv->ComID == INVALID_HANDLE_VALUE ) {
535 doda 6893 DWORD err = GetLastError();
536 yutakapon 4857
537 doda 6893 switch (err) {
538     case ERROR_FILE_NOT_FOUND:
539     get_lang_msg("MSG_CANTOPEN_ERROR_NOTFOUND", ts->UIMsg, sizeof(ts->UIMsg), "Cannot open %s. Not found.", ts->UILanguageFile);
540     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, ts->UIMsg, &P[4]);
541     break;
542     case ERROR_ACCESS_DENIED:
543     get_lang_msg("MSG_CANTOPEN_ERROR_DENIED", ts->UIMsg, sizeof(ts->UIMsg), "Cannot open %s. Access denied.", ts->UILanguageFile);
544     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, ts->UIMsg, &P[4]);
545     break;
546     default:
547     get_lang_msg("MSG_CANTOPEN_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Cannot open %s. (0x%08x)", ts->UILanguageFile);
548     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, ts->UIMsg, &P[4], err);
549     break;
550     }
551    
552 yutakapon 4857 if (cv->NoMsg==0) {
553     get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
554     MessageBox(cv->HWin,ErrMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
555     }
556     InvalidHost = TRUE;
557     }
558     else {
559     cv->Open = TRUE;
560     PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
561     InvalidHost = FALSE;
562     }
563     break; /* end of "case IdNamedPipe:" */
564    
565 maya 3857 } /* end of "switch" */
566    
567     BreakSC:
568     if (InvalidHost) {
569     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
570     if ( (ts->PortType==IdTCPIP) && cv->Open ) {
571     if ( cv->s!=INVALID_SOCKET ) {
572     Pclosesocket(cv->s);
573 yutakapon 3968 cv->s = INVALID_SOCKET; /* �\�P�b�g�����������t�����B(2010.8.6 yutaka) */
574 maya 3857 }
575     FreeWinsock();
576     }
577     return;
578     }
579     }
580    
581 yutakapon 4857 // ���O�t���p�C�v�p�X���b�h
582     void NamedPipeThread(void *arg)
583     {
584     PComVar cv = (PComVar)arg;
585     DWORD DErr;
586     HANDLE REnd;
587     char Temp[20];
588     char Buffer[1]; // 1byte
589     DWORD BytesRead, TotalBytesAvail, BytesLeftThisMessage;
590    
591     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
592     REnd = OpenEvent(EVENT_ALL_ACCESS,FALSE, Temp);
593     while (TRUE) {
594     BytesRead = 0;
595     // ���O�t���p�C�v���C�x���g���������������������d�l�������A�L���[�����g��
596     // �`���������������AReadFile() ���������������f�����B
597     if (PeekNamedPipe(cv->ComID, Buffer, sizeof(Buffer), &BytesRead, &TotalBytesAvail, &BytesLeftThisMessage)) {
598     if (! cv->Ready) {
599     _endthread();
600     }
601     if (BytesRead == 0) { // �����������A�����������B
602     Sleep(1);
603     continue;
604     }
605     if (! cv->RRQ) {
606     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_READ);
607     }
608     // ReadFile() ���I�������������B
609     WaitForSingleObject(REnd,INFINITE);
610     }
611     else {
612 doda 6435 DErr = GetLastError();
613 yutakapon 4962 // [VMware] this returns 109 (broken pipe) if a named pipe is removed.
614     // [Virtual Box] this returns 233 (pipe not connected) if a named pipe is removed.
615     if (! cv->Ready || ERROR_BROKEN_PIPE == DErr || ERROR_PIPE_NOT_CONNECTED == DErr) {
616 yutakapon 4863 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
617 yutakapon 4857 _endthread();
618     }
619     }
620     }
621     }
622    
623 maya 3857 void CommThread(void *arg)
624     {
625     DWORD Evt;
626     PComVar cv = (PComVar)arg;
627     DWORD DErr;
628     HANDLE REnd;
629     char Temp[20];
630    
631     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
632     REnd = OpenEvent(EVENT_ALL_ACCESS,FALSE, Temp);
633     while (TRUE) {
634     if (WaitCommEvent(cv->ComID,&Evt,NULL)) {
635     if (! cv->Ready) {
636     _endthread();
637     }
638     if (! cv->RRQ) {
639     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_READ);
640     }
641     WaitForSingleObject(REnd,INFINITE);
642     }
643     else {
644     DErr = GetLastError(); // this returns 995 (operation aborted) if a USB com port is removed
645     if (! cv->Ready || ERROR_OPERATION_ABORTED == DErr) {
646     _endthread();
647     }
648     ClearCommError(cv->ComID,&DErr,NULL);
649     }
650     }
651     }
652    
653     void CommStart(PComVar cv, LONG lParam, PTTSet ts)
654     {
655     char ErrMsg[31];
656     char Temp[20];
657     char uimsg[MAX_UIMSG];
658    
659     if (! cv->Open ) {
660     return;
661     }
662     if ( cv->Ready ) {
663     return;
664     }
665    
666     // �L�����Z���^�C�}�����������������B�������A�������_�� WM_TIMER �����������������\���������B
667     if (*cv->ConnetingTimeout > 0) {
668     KillTimer(cv->HWin, IdCancelConnectTimer);
669     }
670    
671     switch (cv->PortType) {
672     case IdTCPIP:
673     ErrMsg[0] = 0;
674     switch (HIWORD(lParam)) {
675     case WSAECONNREFUSED:
676     get_lang_msg("MSG_COMM_REFUSE_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Connection refused", ts->UILanguageFile);
677     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, "%s", ts->UIMsg);
678     break;
679     case WSAENETUNREACH:
680     get_lang_msg("MSG_COMM_REACH_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Network cannot be reached", ts->UILanguageFile);
681     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, "%s", ts->UIMsg);
682     break;
683     case WSAETIMEDOUT:
684     get_lang_msg("MSG_COMM_CONNECT_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Connection timed out", ts->UILanguageFile);
685     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, "%s", ts->UIMsg);
686     break;
687     default:
688     get_lang_msg("MSG_COMM_TIMEOUT_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Cannot connect the host", ts->UILanguageFile);
689     _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, "%s", ts->UIMsg);
690     }
691     if (HIWORD(lParam)>0) {
692     /* connect() failed */
693     if (cv->res->ai_next != NULL) {
694     /* try to connect with other protocol */
695     CloseSocket(cv->s);
696     for (cv->res = cv->res->ai_next; cv->res; cv->res = cv->res->ai_next) {
697     cv->s = OpenSocket(cv);
698     if (cv->s == INVALID_SOCKET) {
699     CloseSocket(cv->s);
700     continue;
701     }
702     AsyncConnect(cv);
703     cv->Ready = FALSE;
704     cv->RetryWithOtherProtocol = TRUE; /* retry with other procotol */
705     return;
706     }
707     } else {
708     /* trying with all protocol family are failed */
709     if (cv->NoMsg==0)
710     {
711     get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
712     MessageBox(cv->HWin,ErrMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
713     }
714     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
715     cv->RetryWithOtherProtocol = FALSE;
716     return;
717     }
718     }
719    
720     /* here is connection established */
721     cv->RetryWithOtherProtocol = FALSE;
722     PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMNOTIFY, FD_READ | FD_OOB | FD_CLOSE);
723     TCPIPClosed = FALSE;
724     break;
725    
726     case IdSerial:
727     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
728     ReadEnd = CreateEvent(NULL,FALSE,FALSE,Temp);
729     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", WRITENAME, cv->ComPort);
730     memset(&wol,0,sizeof(OVERLAPPED));
731     wol.hEvent = CreateEvent(NULL,TRUE,TRUE,Temp);
732     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READNAME, cv->ComPort);
733     memset(&rol,0,sizeof(OVERLAPPED));
734     rol.hEvent = CreateEvent(NULL,TRUE,FALSE,Temp);
735    
736     /* create the receiver thread */
737     if (_beginthread(CommThread,0,cv) == -1) {
738     get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
739     get_lang_msg("MSG_TT_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Can't create thread", ts->UILanguageFile);
740     MessageBox(cv->HWin,ts->UIMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
741     }
742     break;
743    
744     case IdFile:
745     cv->RRQ = TRUE;
746     break;
747 yutakapon 4857
748     case IdNamedPipe:
749     cv->ComPort = 0;
750     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
751     ReadEnd = CreateEvent(NULL,FALSE,FALSE,Temp);
752     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", WRITENAME, cv->ComPort);
753     memset(&wol,0,sizeof(OVERLAPPED));
754     wol.hEvent = CreateEvent(NULL,TRUE,TRUE,Temp);
755     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READNAME, cv->ComPort);
756     memset(&rol,0,sizeof(OVERLAPPED));
757     rol.hEvent = CreateEvent(NULL,TRUE,FALSE,Temp);
758    
759     /* create the receiver thread */
760     if (_beginthread(NamedPipeThread,0,cv) == -1) {
761     get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
762     get_lang_msg("MSG_TT_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Can't create thread", ts->UILanguageFile);
763     MessageBox(cv->HWin,ts->UIMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
764     }
765     break;
766 maya 3857 }
767     cv->Ready = TRUE;
768     }
769    
770     BOOL CommCanClose(PComVar cv)
771     { // check if data remains in buffer
772     if (! cv->Open) {
773     return TRUE;
774     }
775     if (cv->InBuffCount>0) {
776     return FALSE;
777     }
778     if ((cv->HLogBuf!=NULL) &&
779     ((cv->LCount>0) ||
780     (cv->DCount>0))) {
781     return FALSE;
782     }
783     if ((cv->HBinBuf!=NULL) &&
784     (cv->BCount>0)) {
785     return FALSE;
786     }
787     return TRUE;
788     }
789    
790     void CommClose(PComVar cv)
791     {
792     if ( ! cv->Open ) {
793     return;
794     }
795     cv->Open = FALSE;
796    
797     /* disable event message posting & flush buffer */
798     cv->RRQ = FALSE;
799     cv->Ready = FALSE;
800     cv->InPtr = 0;
801     cv->InBuffCount = 0;
802     cv->OutPtr = 0;
803     cv->OutBuffCount = 0;
804     cv->LineModeBuffCount = 0;
805     cv->FlushLen = 0;
806     cv->Flush = FALSE;
807    
808     /* close port & release resources */
809     switch (cv->PortType) {
810     case IdTCPIP:
811     if (HAsync!=0) {
812     PWSACancelAsyncRequest(HAsync);
813     }
814     HAsync = 0;
815 doda 4165 Pfreeaddrinfo(cv->res0);
816 maya 3857 if ( cv->s!=INVALID_SOCKET ) {
817     Pclosesocket(cv->s);
818     }
819     cv->s = INVALID_SOCKET;
820     TTXCloseTCP(); /* TTPLUG */
821     FreeWinsock();
822     break;
823     case IdSerial:
824     if ( cv->ComID != INVALID_HANDLE_VALUE ) {
825     CloseHandle(ReadEnd);
826     CloseHandle(wol.hEvent);
827     CloseHandle(rol.hEvent);
828     PurgeComm(cv->ComID, PURGE_TXABORT | PURGE_RXABORT |
829     PURGE_TXCLEAR | PURGE_RXCLEAR);
830     EscapeCommFunction(cv->ComID,CLRDTR);
831     SetCommMask(cv->ComID,0);
832     PCloseFile(cv->ComID);
833     ClearCOMFlag(cv->ComPort);
834     }
835     TTXCloseFile(); /* TTPLUG */
836     break;
837     case IdFile:
838     if (cv->ComID != INVALID_HANDLE_VALUE) {
839     PCloseFile(cv->ComID);
840     }
841     TTXCloseFile(); /* TTPLUG */
842     break;
843 yutakapon 4857
844     case IdNamedPipe:
845     if ( cv->ComID != INVALID_HANDLE_VALUE ) {
846     CloseHandle(ReadEnd);
847     CloseHandle(wol.hEvent);
848     CloseHandle(rol.hEvent);
849     PCloseFile(cv->ComID);
850     }
851     TTXCloseFile(); /* TTPLUG */
852     break;
853 maya 3857 }
854     cv->ComID = INVALID_HANDLE_VALUE;
855     cv->PortType = 0;
856 doda 3932
857     _free_locale(cv->locale);
858 maya 3857 }
859    
860     void CommProcRRQ(PComVar cv)
861     {
862     if ( ! cv->Ready ) {
863     return;
864     }
865     /* disable receive request */
866     switch (cv->PortType) {
867     case IdTCPIP:
868     if (! TCPIPClosed) {
869     PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMNOTIFY, FD_OOB | FD_CLOSE);
870     }
871     break;
872     case IdSerial:
873     break;
874     }
875     cv->RRQ = TRUE;
876     CommReceive(cv);
877     }
878    
879     void CommReceive(PComVar cv)
880     {
881     DWORD C;
882     DWORD DErr;
883    
884     if (! cv->Ready || ! cv->RRQ ||
885     (cv->InBuffCount>=InBuffSize)) {
886     return;
887     }
888    
889     /* Compact buffer */
890     if ((cv->InBuffCount>0) && (cv->InPtr>0)) {
891     memmove(cv->InBuff,&(cv->InBuff[cv->InPtr]),cv->InBuffCount);
892     cv->InPtr = 0;
893     }
894    
895     if (cv->InBuffCount<InBuffSize) {
896     switch (cv->PortType) {
897     case IdTCPIP:
898     C = Precv(cv->s, &(cv->InBuff[cv->InBuffCount]),
899     InBuffSize-cv->InBuffCount, 0);
900     if (C == SOCKET_ERROR) {
901     C = 0;
902     PWSAGetLastError();
903     }
904     cv->InBuffCount = cv->InBuffCount + C;
905     break;
906     case IdSerial:
907     do {
908     ClearCommError(cv->ComID,&DErr,NULL);
909     if (! PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
910     InBuffSize-cv->InBuffCount,&C,&rol)) {
911     if (GetLastError() == ERROR_IO_PENDING) {
912     if (WaitForSingleObject(rol.hEvent, 1000) != WAIT_OBJECT_0) {
913     C = 0;
914     }
915     else {
916     GetOverlappedResult(cv->ComID,&rol,&C,FALSE);
917     }
918     }
919     else {
920     C = 0;
921     }
922     }
923     cv->InBuffCount = cv->InBuffCount + C;
924     } while ((C!=0) && (cv->InBuffCount<InBuffSize));
925     ClearCommError(cv->ComID,&DErr,NULL);
926     break;
927     case IdFile:
928     if (PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
929     InBuffSize-cv->InBuffCount,&C,NULL)) {
930     if (C == 0) {
931     DErr = ERROR_HANDLE_EOF;
932     }
933     else {
934     cv->InBuffCount = cv->InBuffCount + C;
935     }
936     }
937     else {
938     DErr = GetLastError();
939     }
940     break;
941 yutakapon 4857
942     case IdNamedPipe:
943     // �L���[����������1�o�C�g�������f�[�^�������������������m�F���������������A
944     // ReadFile() ���u���b�N�������������������A�������������B
945     if (PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
946     InBuffSize-cv->InBuffCount,&C,NULL)) {
947     if (C == 0) {
948     DErr = ERROR_HANDLE_EOF;
949     }
950     else {
951     cv->InBuffCount = cv->InBuffCount + C;
952     }
953     }
954     else {
955     DErr = GetLastError();
956     }
957    
958     // 1�o�C�g�������������A�C�x���g���N�����A�X���b�h�����J�������B
959     if (cv->InBuffCount > 0) {
960     cv->RRQ = FALSE;
961     SetEvent(ReadEnd);
962     }
963     break;
964 maya 3857 }
965     }
966    
967     if (cv->InBuffCount==0) {
968     switch (cv->PortType) {
969     case IdTCPIP:
970     if (! TCPIPClosed) {
971     PWSAAsyncSelect(cv->s,cv->HWin, WM_USER_COMMNOTIFY,
972     FD_READ | FD_OOB | FD_CLOSE);
973     }
974     break;
975     case IdSerial:
976     cv->RRQ = FALSE;
977     SetEvent(ReadEnd);
978     return;
979     case IdFile:
980     if (DErr != ERROR_IO_PENDING) {
981     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
982     cv->RRQ = FALSE;
983     }
984     else {
985     cv->RRQ = TRUE;
986     }
987     return;
988 yutakapon 4857 case IdNamedPipe:
989     // TODO: �������A���������������������B
990     if (DErr != ERROR_IO_PENDING) {
991     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
992     cv->RRQ = FALSE;
993     }
994     else {
995     cv->RRQ = TRUE;
996     }
997     SetEvent(ReadEnd);
998     return;
999 maya 3857 }
1000     cv->RRQ = FALSE;
1001     }
1002     }
1003    
1004     void CommSend(PComVar cv)
1005     {
1006     int delay;
1007     COMSTAT Stat;
1008     BYTE LineEnd;
1009     int C, D, Max;
1010     DWORD DErr;
1011    
1012     if ((! cv->Open) || (! cv->Ready)) {
1013 doda 6435 cv->OutBuffCount = 0;
1014 maya 3857 return;
1015     }
1016    
1017     if ((cv->OutBuffCount == 0) || (! cv->CanSend)) {
1018     return;
1019     }
1020    
1021     /* Max num of bytes to be written */
1022     switch (cv->PortType) {
1023     case IdTCPIP:
1024     if (TCPIPClosed) {
1025     cv->OutBuffCount = 0;
1026     }
1027     Max = cv->OutBuffCount;
1028     break;
1029     case IdSerial:
1030     ClearCommError(cv->ComID,&DErr,&Stat);
1031     Max = OutBuffSize - Stat.cbOutQue;
1032     break;
1033     case IdFile:
1034     Max = cv->OutBuffCount;
1035     break;
1036 yutakapon 4857 case IdNamedPipe:
1037     Max = cv->OutBuffCount;
1038     break;
1039 maya 3857 }
1040    
1041     if ( Max<=0 ) {
1042     return;
1043     }
1044     if ( Max > cv->OutBuffCount ) {
1045     Max = cv->OutBuffCount;
1046     }
1047    
1048     if (cv->PortType == IdTCPIP && cv->TelFlag) {
1049     cv->LastSendTime = time(NULL);
1050     }
1051    
1052     C = Max;
1053     delay = 0;
1054    
1055     if ( cv->DelayFlag && (cv->PortType==IdSerial) ) {
1056     if ( cv->DelayPerLine > 0 ) {
1057     if ( cv->CRSend==IdCR ) {
1058     LineEnd = 0x0d;
1059     }
1060 maya 6479 else { // CRLF or LF
1061 maya 3857 LineEnd = 0x0a;
1062     }
1063     C = 1;
1064     if ( cv->DelayPerChar==0 ) {
1065     while ((C<Max) && (cv->OutBuff[cv->OutPtr+C-1]!=LineEnd)) {
1066     C++;
1067     }
1068     }
1069     if ( cv->OutBuff[cv->OutPtr+C-1]==LineEnd ) {
1070     delay = cv->DelayPerLine;
1071     }
1072     else {
1073     delay = cv->DelayPerChar;
1074     }
1075     }
1076     else if ( cv->DelayPerChar > 0 ) {
1077     C = 1;
1078     delay = cv->DelayPerChar;
1079     }
1080     }
1081    
1082     /* Write to comm driver/Winsock */
1083     switch (cv->PortType) {
1084     case IdTCPIP:
1085     D = Psend(cv->s, &(cv->OutBuff[cv->OutPtr]), C, 0);
1086     if ( D==SOCKET_ERROR ) { /* if error occurs */
1087     PWSAGetLastError(); /* Clear error */
1088     D = 0;
1089     }
1090     break;
1091    
1092     case IdSerial:
1093     if (! PWriteFile(cv->ComID,&(cv->OutBuff[cv->OutPtr]),C,(LPDWORD)&D,&wol)) {
1094     if (GetLastError() == ERROR_IO_PENDING) {
1095     if (WaitForSingleObject(wol.hEvent,1000) != WAIT_OBJECT_0) {
1096     D = C; /* Time out, ignore data */
1097     }
1098     else {
1099     GetOverlappedResult(cv->ComID,&wol,(LPDWORD)&D,FALSE);
1100     }
1101     }
1102     else { /* I/O error */
1103     D = C; /* ignore error */
1104     }
1105     }
1106     ClearCommError(cv->ComID,&DErr,&Stat);
1107     break;
1108    
1109     case IdFile:
1110     if (! PWriteFile(cv->ComID, &(cv->OutBuff[cv->OutPtr]), C, (LPDWORD)&D, NULL)) {
1111     if (! GetLastError() == ERROR_IO_PENDING) {
1112     D = C; /* ignore data */
1113     }
1114     }
1115     break;
1116 yutakapon 4857
1117     case IdNamedPipe:
1118     if (! PWriteFile(cv->ComID, &(cv->OutBuff[cv->OutPtr]), C, (LPDWORD)&D, NULL)) {
1119 yutakapon 4863 // ERROR_IO_PENDING ���O���G���[���������A�p�C�v���N���[�Y�������������������������A
1120     // ���M�����������������B
1121     if (! (GetLastError() == ERROR_IO_PENDING)) {
1122 yutakapon 4857 D = C; /* ignore data */
1123     }
1124     }
1125     break;
1126 maya 3857 }
1127    
1128     cv->OutBuffCount = cv->OutBuffCount - D;
1129     if ( cv->OutBuffCount==0 ) {
1130     cv->OutPtr = 0;
1131     }
1132     else {
1133     cv->OutPtr = cv->OutPtr + D;
1134     }
1135    
1136     if ( (C==D) && (delay>0) ) {
1137     cv->CanSend = FALSE;
1138     SetTimer(cv->HWin, IdDelayTimer, delay, NULL);
1139     }
1140     }
1141    
1142 maya 5694 void CommSendBreak(PComVar cv, int msec)
1143 maya 3857 /* for only serial ports */
1144     {
1145     MSG DummyMsg;
1146    
1147     if ( ! cv->Ready ) {
1148     return;
1149     }
1150    
1151     switch (cv->PortType) {
1152     case IdSerial:
1153     /* Set com port into a break state */
1154     SetCommBreak(cv->ComID);
1155    
1156     /* pause for 1 sec */
1157 maya 5694 if (SetTimer(cv->HWin, IdBreakTimer, msec, NULL) != 0) {
1158 maya 3857 GetMessage(&DummyMsg,cv->HWin,WM_TIMER,WM_TIMER);
1159     }
1160    
1161     /* Set com port into the nonbreak state */
1162     ClearCommBreak(cv->ComID);
1163     break;
1164     }
1165     }
1166    
1167     void CommLock(PTTSet ts, PComVar cv, BOOL Lock)
1168     {
1169     BYTE b;
1170     DWORD Func;
1171    
1172     if (! cv->Ready) {
1173     return;
1174     }
1175     if ((cv->PortType==IdTCPIP) ||
1176     (cv->PortType==IdSerial) &&
1177     (ts->Flow!=IdFlowHard)) {
1178     if (Lock) {
1179     b = XOFF;
1180     }
1181     else {
1182     b = XON;
1183     }
1184     CommBinaryOut(cv,&b,1);
1185     }
1186     else if ((cv->PortType==IdSerial) &&
1187     (ts->Flow==IdFlowHard)) {
1188     if (Lock) {
1189     Func = CLRRTS;
1190     }
1191     else {
1192     Func = SETRTS;
1193     }
1194     EscapeCommFunction(cv->ComID,Func);
1195     }
1196     }
1197    
1198     BOOL PrnOpen(PCHAR DevName)
1199     {
1200 maya 4283 char Temp[MAXPATHLEN], *c;
1201 maya 3857 DCB dcb;
1202     DWORD DErr;
1203     COMMTIMEOUTS ctmo;
1204    
1205     strncpy_s(Temp, sizeof(Temp),DevName, _TRUNCATE);
1206 maya 4283 c = Temp;
1207     while (*c != '\0' && *c != ':') {
1208     c++;
1209     }
1210     *c = '\0';
1211 maya 3857 LPTFlag = (Temp[0]=='L') ||
1212     (Temp[0]=='l');
1213 maya 4284
1214 yutakapon 6286 if (IsWindowsNTKernel()) {
1215 maya 4284 // �l�b�g���[�N���L���}�b�v�������f�o�C�X�������������A�������������������������� (2011.01.25 maya)
1216     // http://logmett.com/forum/viewtopic.php?f=2&t=1383
1217     // http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx#5
1218     PrnID = CreateFile(Temp,GENERIC_WRITE | FILE_READ_ATTRIBUTES,
1219     FILE_SHARE_READ,NULL,CREATE_ALWAYS,
1220     0,NULL);
1221     }
1222     else {
1223     // 9x �������L���R�[�h���������������������]������������
1224     PrnID = CreateFile(Temp,GENERIC_WRITE,
1225     0,NULL,OPEN_EXISTING,
1226     0,NULL);
1227     }
1228    
1229 maya 3857 if (PrnID == INVALID_HANDLE_VALUE) {
1230     return FALSE;
1231     }
1232    
1233     if (GetCommState(PrnID,&dcb)) {
1234     BuildCommDCB(DevName,&dcb);
1235     SetCommState(PrnID,&dcb);
1236     }
1237     ClearCommError(PrnID,&DErr,NULL);
1238     if (! LPTFlag) {
1239     SetupComm(PrnID,0,CommOutQueSize);
1240     }
1241     /* flush output buffer */
1242     PurgeComm(PrnID, PURGE_TXABORT | PURGE_TXCLEAR);
1243     memset(&ctmo,0,sizeof(ctmo));
1244     ctmo.WriteTotalTimeoutConstant = 1000;
1245     SetCommTimeouts(PrnID,&ctmo);
1246     if (! LPTFlag) {
1247     EscapeCommFunction(PrnID,SETDTR);
1248     }
1249     return TRUE;
1250     }
1251    
1252     int PrnWrite(PCHAR b, int c)
1253     {
1254     int d;
1255     DWORD DErr;
1256     COMSTAT Stat;
1257    
1258     if (PrnID == INVALID_HANDLE_VALUE ) {
1259     return c;
1260     }
1261    
1262     ClearCommError(PrnID,&DErr,&Stat);
1263     if (! LPTFlag &&
1264     (OutBuffSize - (int)Stat.cbOutQue < c)) {
1265     c = OutBuffSize - Stat.cbOutQue;
1266     }
1267     if (c<=0) {
1268     return 0;
1269     }
1270     if (! WriteFile(PrnID,b,c,(LPDWORD)&d,NULL)) {
1271     d = 0;
1272     }
1273     ClearCommError(PrnID,&DErr,NULL);
1274     return d;
1275     }
1276    
1277     void PrnCancel()
1278     {
1279     PurgeComm(PrnID, PURGE_TXABORT | PURGE_TXCLEAR);
1280     PrnClose();
1281     }
1282    
1283     void PrnClose()
1284     {
1285     if (PrnID != INVALID_HANDLE_VALUE) {
1286     if (!LPTFlag) {
1287     EscapeCommFunction(PrnID,CLRDTR);
1288     }
1289     CloseHandle(PrnID);
1290     }
1291     PrnID = INVALID_HANDLE_VALUE;
1292     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26