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 10586 - (hide annotations) (download) (as text)
Mon Feb 6 13:26:02 2023 UTC (14 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 35871 byte(s)
シリアルのハードフローが動作しない対策,パラメータ設定

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

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