Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4858 - (show annotations) (download) (as text)
Fri Mar 9 15:25:48 2012 UTC (12 years, 1 month ago) by yutakapon
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 35531 byte(s)
名前付きパイプの書式チェックを追加した。
\\ServerName\pipe\PipeName

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

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