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 4857 - (show annotations) (download) (as text)
Thu Mar 8 15:50:26 2012 UTC (12 years, 1 month ago) by yutakapon
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 34793 byte(s)
名前付きパイプをサポートした。
VMware Player 3.1.5 + Fedora 16 で動作確認済み。

* 未サポート
  - 接続ダイアログからの設定
  - ブレーク送信
  - 他

* コマンドライン
  書式 /NAMEDPIPE 名前付きパイプ名
  例   /NAMEDPIPE \\.\pipe\vmware-serial-port

* teraterm.ini
  Port=namedpipe 追加


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 strncpy_s(P, sizeof(P), ts->HostName, _TRUNCATE);
558 cv->ComID =
559 PCreateFile(P,GENERIC_READ | GENERIC_WRITE,
560 0,NULL,OPEN_EXISTING,
561 0, // �u���b�L���O���[�h������(FILE_FLAG_OVERLAPPED ���w��������)
562 NULL);
563 if (cv->ComID == INVALID_HANDLE_VALUE ) {
564 get_lang_msg("MSG_CANTOEPN_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Cannot open %s(%d)", ts->UILanguageFile);
565 _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, ts->UIMsg, &P[0], GetLastError());
566
567 if (cv->NoMsg==0) {
568 get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
569 MessageBox(cv->HWin,ErrMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
570 }
571 InvalidHost = TRUE;
572 }
573 else {
574 cv->Open = TRUE;
575 PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
576 InvalidHost = FALSE;
577 }
578 break; /* end of "case IdNamedPipe:" */
579
580 } /* end of "switch" */
581
582 #ifndef NO_INET6
583 BreakSC:
584 #endif /* NO_INET6 */
585 if (InvalidHost) {
586 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
587 if ( (ts->PortType==IdTCPIP) && cv->Open ) {
588 if ( cv->s!=INVALID_SOCKET ) {
589 Pclosesocket(cv->s);
590 cv->s = INVALID_SOCKET; /* �\�P�b�g�����������t�����B(2010.8.6 yutaka) */
591 }
592 FreeWinsock();
593 }
594 return;
595 }
596 }
597
598 // ���O�t���p�C�v�p�X���b�h
599 void NamedPipeThread(void *arg)
600 {
601 PComVar cv = (PComVar)arg;
602 DWORD DErr;
603 HANDLE REnd;
604 char Temp[20];
605 char Buffer[1]; // 1byte
606 DWORD BytesRead, TotalBytesAvail, BytesLeftThisMessage;
607
608 _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
609 REnd = OpenEvent(EVENT_ALL_ACCESS,FALSE, Temp);
610 while (TRUE) {
611 BytesRead = 0;
612 // ���O�t���p�C�v���C�x���g���������������������d�l�������A�L���[�����g��
613 // �`���������������AReadFile() ���������������f�����B
614 if (PeekNamedPipe(cv->ComID, Buffer, sizeof(Buffer), &BytesRead, &TotalBytesAvail, &BytesLeftThisMessage)) {
615 if (! cv->Ready) {
616 _endthread();
617 }
618 if (BytesRead == 0) { // �����������A�����������B
619 Sleep(1);
620 continue;
621 }
622 if (! cv->RRQ) {
623 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_READ);
624 }
625 // ReadFile() ���I�������������B
626 WaitForSingleObject(REnd,INFINITE);
627 }
628 else {
629 DErr = GetLastError(); // this returns 995 (operation aborted) if a USB com port is removed
630 if (! cv->Ready || ERROR_OPERATION_ABORTED == DErr) {
631 _endthread();
632 }
633 }
634 }
635 }
636
637 void CommThread(void *arg)
638 {
639 DWORD Evt;
640 PComVar cv = (PComVar)arg;
641 DWORD DErr;
642 HANDLE REnd;
643 char Temp[20];
644
645 _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
646 REnd = OpenEvent(EVENT_ALL_ACCESS,FALSE, Temp);
647 while (TRUE) {
648 if (WaitCommEvent(cv->ComID,&Evt,NULL)) {
649 if (! cv->Ready) {
650 _endthread();
651 }
652 if (! cv->RRQ) {
653 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_READ);
654 }
655 WaitForSingleObject(REnd,INFINITE);
656 }
657 else {
658 DErr = GetLastError(); // this returns 995 (operation aborted) if a USB com port is removed
659 if (! cv->Ready || ERROR_OPERATION_ABORTED == DErr) {
660 _endthread();
661 }
662 ClearCommError(cv->ComID,&DErr,NULL);
663 }
664 }
665 }
666
667 void CommStart(PComVar cv, LONG lParam, PTTSet ts)
668 {
669 char ErrMsg[31];
670 char Temp[20];
671 char uimsg[MAX_UIMSG];
672
673 if (! cv->Open ) {
674 return;
675 }
676 if ( cv->Ready ) {
677 return;
678 }
679
680 // �L�����Z���^�C�}�����������������B�������A�������_�� WM_TIMER �����������������\���������B
681 if (*cv->ConnetingTimeout > 0) {
682 KillTimer(cv->HWin, IdCancelConnectTimer);
683 }
684
685 switch (cv->PortType) {
686 case IdTCPIP:
687 ErrMsg[0] = 0;
688 switch (HIWORD(lParam)) {
689 case WSAECONNREFUSED:
690 get_lang_msg("MSG_COMM_REFUSE_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Connection refused", ts->UILanguageFile);
691 _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, "%s", ts->UIMsg);
692 break;
693 case WSAENETUNREACH:
694 get_lang_msg("MSG_COMM_REACH_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Network cannot be reached", ts->UILanguageFile);
695 _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, "%s", ts->UIMsg);
696 break;
697 case WSAETIMEDOUT:
698 get_lang_msg("MSG_COMM_CONNECT_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Connection timed out", ts->UILanguageFile);
699 _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, "%s", ts->UIMsg);
700 break;
701 default:
702 get_lang_msg("MSG_COMM_TIMEOUT_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Cannot connect the host", ts->UILanguageFile);
703 _snprintf_s(ErrMsg, sizeof(ErrMsg), _TRUNCATE, "%s", ts->UIMsg);
704 }
705 if (HIWORD(lParam)>0) {
706 #ifndef NO_INET6
707 /* connect() failed */
708 if (cv->res->ai_next != NULL) {
709 /* try to connect with other protocol */
710 CloseSocket(cv->s);
711 for (cv->res = cv->res->ai_next; cv->res; cv->res = cv->res->ai_next) {
712 cv->s = OpenSocket(cv);
713 if (cv->s == INVALID_SOCKET) {
714 CloseSocket(cv->s);
715 continue;
716 }
717 AsyncConnect(cv);
718 cv->Ready = FALSE;
719 cv->RetryWithOtherProtocol = TRUE; /* retry with other procotol */
720 return;
721 }
722 } else {
723 /* trying with all protocol family are failed */
724 if (cv->NoMsg==0)
725 {
726 get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
727 MessageBox(cv->HWin,ErrMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
728 }
729 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
730 cv->RetryWithOtherProtocol = FALSE;
731 return;
732 }
733 #else
734 if (cv->NoMsg==0) {
735 get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
736 MessageBox(cv->HWin, ErrMsg, uimsg, MB_TASKMODAL | MB_ICONEXCLAMATION);
737 }
738 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
739 return;
740 #endif /* NO_INET6 */
741 }
742
743 #ifndef NO_INET6
744 /* here is connection established */
745 cv->RetryWithOtherProtocol = FALSE;
746 #endif /* NO_INET6 */
747 PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMNOTIFY, FD_READ | FD_OOB | FD_CLOSE);
748 TCPIPClosed = FALSE;
749 break;
750
751 case IdSerial:
752 _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
753 ReadEnd = CreateEvent(NULL,FALSE,FALSE,Temp);
754 _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", WRITENAME, cv->ComPort);
755 memset(&wol,0,sizeof(OVERLAPPED));
756 wol.hEvent = CreateEvent(NULL,TRUE,TRUE,Temp);
757 _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READNAME, cv->ComPort);
758 memset(&rol,0,sizeof(OVERLAPPED));
759 rol.hEvent = CreateEvent(NULL,TRUE,FALSE,Temp);
760
761 /* create the receiver thread */
762 if (_beginthread(CommThread,0,cv) == -1) {
763 get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
764 get_lang_msg("MSG_TT_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Can't create thread", ts->UILanguageFile);
765 MessageBox(cv->HWin,ts->UIMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
766 }
767 break;
768
769 case IdFile:
770 cv->RRQ = TRUE;
771 break;
772
773 case IdNamedPipe:
774 cv->ComPort = 0;
775 _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
776 ReadEnd = CreateEvent(NULL,FALSE,FALSE,Temp);
777 _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", WRITENAME, cv->ComPort);
778 memset(&wol,0,sizeof(OVERLAPPED));
779 wol.hEvent = CreateEvent(NULL,TRUE,TRUE,Temp);
780 _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READNAME, cv->ComPort);
781 memset(&rol,0,sizeof(OVERLAPPED));
782 rol.hEvent = CreateEvent(NULL,TRUE,FALSE,Temp);
783
784 /* create the receiver thread */
785 if (_beginthread(NamedPipeThread,0,cv) == -1) {
786 get_lang_msg("MSG_TT_ERROR", uimsg, sizeof(uimsg), "Tera Term: Error", ts->UILanguageFile);
787 get_lang_msg("MSG_TT_ERROR", ts->UIMsg, sizeof(ts->UIMsg), "Can't create thread", ts->UILanguageFile);
788 MessageBox(cv->HWin,ts->UIMsg,uimsg,MB_TASKMODAL | MB_ICONEXCLAMATION);
789 }
790 break;
791 }
792 cv->Ready = TRUE;
793 }
794
795 BOOL CommCanClose(PComVar cv)
796 { // check if data remains in buffer
797 if (! cv->Open) {
798 return TRUE;
799 }
800 if (cv->InBuffCount>0) {
801 return FALSE;
802 }
803 if ((cv->HLogBuf!=NULL) &&
804 ((cv->LCount>0) ||
805 (cv->DCount>0))) {
806 return FALSE;
807 }
808 if ((cv->HBinBuf!=NULL) &&
809 (cv->BCount>0)) {
810 return FALSE;
811 }
812 return TRUE;
813 }
814
815 void CommClose(PComVar cv)
816 {
817 if ( ! cv->Open ) {
818 return;
819 }
820 cv->Open = FALSE;
821
822 /* disable event message posting & flush buffer */
823 cv->RRQ = FALSE;
824 cv->Ready = FALSE;
825 cv->InPtr = 0;
826 cv->InBuffCount = 0;
827 cv->OutPtr = 0;
828 cv->OutBuffCount = 0;
829 cv->LineModeBuffCount = 0;
830 cv->FlushLen = 0;
831 cv->Flush = FALSE;
832
833 /* close port & release resources */
834 switch (cv->PortType) {
835 case IdTCPIP:
836 if (HAsync!=0) {
837 PWSACancelAsyncRequest(HAsync);
838 }
839 HAsync = 0;
840 #ifndef NO_INET6
841 Pfreeaddrinfo(cv->res0);
842 #endif /* NO_INET6 */
843 if ( cv->s!=INVALID_SOCKET ) {
844 Pclosesocket(cv->s);
845 }
846 cv->s = INVALID_SOCKET;
847 TTXCloseTCP(); /* TTPLUG */
848 FreeWinsock();
849 break;
850 case IdSerial:
851 if ( cv->ComID != INVALID_HANDLE_VALUE ) {
852 CloseHandle(ReadEnd);
853 CloseHandle(wol.hEvent);
854 CloseHandle(rol.hEvent);
855 PurgeComm(cv->ComID, PURGE_TXABORT | PURGE_RXABORT |
856 PURGE_TXCLEAR | PURGE_RXCLEAR);
857 EscapeCommFunction(cv->ComID,CLRDTR);
858 SetCommMask(cv->ComID,0);
859 PCloseFile(cv->ComID);
860 ClearCOMFlag(cv->ComPort);
861 }
862 TTXCloseFile(); /* TTPLUG */
863 break;
864 case IdFile:
865 if (cv->ComID != INVALID_HANDLE_VALUE) {
866 PCloseFile(cv->ComID);
867 }
868 TTXCloseFile(); /* TTPLUG */
869 break;
870
871 case IdNamedPipe:
872 if ( cv->ComID != INVALID_HANDLE_VALUE ) {
873 CloseHandle(ReadEnd);
874 CloseHandle(wol.hEvent);
875 CloseHandle(rol.hEvent);
876 PCloseFile(cv->ComID);
877 }
878 TTXCloseFile(); /* TTPLUG */
879 break;
880 }
881 cv->ComID = INVALID_HANDLE_VALUE;
882 cv->PortType = 0;
883
884 _free_locale(cv->locale);
885 }
886
887 void CommProcRRQ(PComVar cv)
888 {
889 if ( ! cv->Ready ) {
890 return;
891 }
892 /* disable receive request */
893 switch (cv->PortType) {
894 case IdTCPIP:
895 if (! TCPIPClosed) {
896 PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMNOTIFY, FD_OOB | FD_CLOSE);
897 }
898 break;
899 case IdSerial:
900 break;
901 }
902 cv->RRQ = TRUE;
903 CommReceive(cv);
904 }
905
906 void CommReceive(PComVar cv)
907 {
908 DWORD C;
909 DWORD DErr;
910
911 if (! cv->Ready || ! cv->RRQ ||
912 (cv->InBuffCount>=InBuffSize)) {
913 return;
914 }
915
916 /* Compact buffer */
917 if ((cv->InBuffCount>0) && (cv->InPtr>0)) {
918 memmove(cv->InBuff,&(cv->InBuff[cv->InPtr]),cv->InBuffCount);
919 cv->InPtr = 0;
920 }
921
922 if (cv->InBuffCount<InBuffSize) {
923 switch (cv->PortType) {
924 case IdTCPIP:
925 C = Precv(cv->s, &(cv->InBuff[cv->InBuffCount]),
926 InBuffSize-cv->InBuffCount, 0);
927 if (C == SOCKET_ERROR) {
928 C = 0;
929 PWSAGetLastError();
930 }
931 cv->InBuffCount = cv->InBuffCount + C;
932 break;
933 case IdSerial:
934 do {
935 ClearCommError(cv->ComID,&DErr,NULL);
936 if (! PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
937 InBuffSize-cv->InBuffCount,&C,&rol)) {
938 if (GetLastError() == ERROR_IO_PENDING) {
939 if (WaitForSingleObject(rol.hEvent, 1000) != WAIT_OBJECT_0) {
940 C = 0;
941 }
942 else {
943 GetOverlappedResult(cv->ComID,&rol,&C,FALSE);
944 }
945 }
946 else {
947 C = 0;
948 }
949 }
950 cv->InBuffCount = cv->InBuffCount + C;
951 } while ((C!=0) && (cv->InBuffCount<InBuffSize));
952 ClearCommError(cv->ComID,&DErr,NULL);
953 break;
954 case IdFile:
955 if (PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
956 InBuffSize-cv->InBuffCount,&C,NULL)) {
957 if (C == 0) {
958 DErr = ERROR_HANDLE_EOF;
959 }
960 else {
961 cv->InBuffCount = cv->InBuffCount + C;
962 }
963 }
964 else {
965 DErr = GetLastError();
966 }
967 break;
968
969 case IdNamedPipe:
970 // �L���[����������1�o�C�g�������f�[�^�������������������m�F���������������A
971 // ReadFile() ���u���b�N�������������������A�������������B
972 if (PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
973 InBuffSize-cv->InBuffCount,&C,NULL)) {
974 if (C == 0) {
975 DErr = ERROR_HANDLE_EOF;
976 }
977 else {
978 cv->InBuffCount = cv->InBuffCount + C;
979 }
980 }
981 else {
982 DErr = GetLastError();
983 }
984
985 // 1�o�C�g�������������A�C�x���g���N�����A�X���b�h�����J�������B
986 if (cv->InBuffCount > 0) {
987 cv->RRQ = FALSE;
988 SetEvent(ReadEnd);
989 }
990 break;
991 }
992 }
993
994 if (cv->InBuffCount==0) {
995 switch (cv->PortType) {
996 case IdTCPIP:
997 if (! TCPIPClosed) {
998 PWSAAsyncSelect(cv->s,cv->HWin, WM_USER_COMMNOTIFY,
999 FD_READ | FD_OOB | FD_CLOSE);
1000 }
1001 break;
1002 case IdSerial:
1003 cv->RRQ = FALSE;
1004 SetEvent(ReadEnd);
1005 return;
1006 case IdFile:
1007 if (DErr != ERROR_IO_PENDING) {
1008 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
1009 cv->RRQ = FALSE;
1010 }
1011 else {
1012 cv->RRQ = TRUE;
1013 }
1014 return;
1015 case IdNamedPipe:
1016 // TODO: �������A���������������������B
1017 if (DErr != ERROR_IO_PENDING) {
1018 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
1019 cv->RRQ = FALSE;
1020 }
1021 else {
1022 cv->RRQ = TRUE;
1023 }
1024 SetEvent(ReadEnd);
1025 return;
1026 }
1027 cv->RRQ = FALSE;
1028 }
1029 }
1030
1031 void CommSend(PComVar cv)
1032 {
1033 int delay;
1034 COMSTAT Stat;
1035 BYTE LineEnd;
1036 int C, D, Max;
1037 DWORD DErr;
1038
1039 if ((! cv->Open) || (! cv->Ready)) {
1040 cv->OutBuffCount = 0;
1041 return;
1042 }
1043
1044 if ((cv->OutBuffCount == 0) || (! cv->CanSend)) {
1045 return;
1046 }
1047
1048 /* Max num of bytes to be written */
1049 switch (cv->PortType) {
1050 case IdTCPIP:
1051 if (TCPIPClosed) {
1052 cv->OutBuffCount = 0;
1053 }
1054 Max = cv->OutBuffCount;
1055 break;
1056 case IdSerial:
1057 ClearCommError(cv->ComID,&DErr,&Stat);
1058 Max = OutBuffSize - Stat.cbOutQue;
1059 break;
1060 case IdFile:
1061 Max = cv->OutBuffCount;
1062 break;
1063 case IdNamedPipe:
1064 Max = cv->OutBuffCount;
1065 break;
1066 }
1067
1068 if ( Max<=0 ) {
1069 return;
1070 }
1071 if ( Max > cv->OutBuffCount ) {
1072 Max = cv->OutBuffCount;
1073 }
1074
1075 if (cv->PortType == IdTCPIP && cv->TelFlag) {
1076 cv->LastSendTime = time(NULL);
1077 }
1078
1079 C = Max;
1080 delay = 0;
1081
1082 if ( cv->DelayFlag && (cv->PortType==IdSerial) ) {
1083 if ( cv->DelayPerLine > 0 ) {
1084 if ( cv->CRSend==IdCR ) {
1085 LineEnd = 0x0d;
1086 }
1087 else {
1088 LineEnd = 0x0a;
1089 }
1090 C = 1;
1091 if ( cv->DelayPerChar==0 ) {
1092 while ((C<Max) && (cv->OutBuff[cv->OutPtr+C-1]!=LineEnd)) {
1093 C++;
1094 }
1095 }
1096 if ( cv->OutBuff[cv->OutPtr+C-1]==LineEnd ) {
1097 delay = cv->DelayPerLine;
1098 }
1099 else {
1100 delay = cv->DelayPerChar;
1101 }
1102 }
1103 else if ( cv->DelayPerChar > 0 ) {
1104 C = 1;
1105 delay = cv->DelayPerChar;
1106 }
1107 }
1108
1109 /* Write to comm driver/Winsock */
1110 switch (cv->PortType) {
1111 case IdTCPIP:
1112 D = Psend(cv->s, &(cv->OutBuff[cv->OutPtr]), C, 0);
1113 if ( D==SOCKET_ERROR ) { /* if error occurs */
1114 PWSAGetLastError(); /* Clear error */
1115 D = 0;
1116 }
1117 break;
1118
1119 case IdSerial:
1120 if (! PWriteFile(cv->ComID,&(cv->OutBuff[cv->OutPtr]),C,(LPDWORD)&D,&wol)) {
1121 if (GetLastError() == ERROR_IO_PENDING) {
1122 if (WaitForSingleObject(wol.hEvent,1000) != WAIT_OBJECT_0) {
1123 D = C; /* Time out, ignore data */
1124 }
1125 else {
1126 GetOverlappedResult(cv->ComID,&wol,(LPDWORD)&D,FALSE);
1127 }
1128 }
1129 else { /* I/O error */
1130 D = C; /* ignore error */
1131 }
1132 }
1133 ClearCommError(cv->ComID,&DErr,&Stat);
1134 break;
1135
1136 case IdFile:
1137 if (! PWriteFile(cv->ComID, &(cv->OutBuff[cv->OutPtr]), C, (LPDWORD)&D, NULL)) {
1138 if (! GetLastError() == ERROR_IO_PENDING) {
1139 D = C; /* ignore data */
1140 }
1141 }
1142 break;
1143
1144 case IdNamedPipe:
1145 if (! PWriteFile(cv->ComID, &(cv->OutBuff[cv->OutPtr]), C, (LPDWORD)&D, NULL)) {
1146 if (! GetLastError() == ERROR_IO_PENDING) {
1147 D = C; /* ignore data */
1148 }
1149 }
1150 break;
1151 }
1152
1153 cv->OutBuffCount = cv->OutBuffCount - D;
1154 if ( cv->OutBuffCount==0 ) {
1155 cv->OutPtr = 0;
1156 }
1157 else {
1158 cv->OutPtr = cv->OutPtr + D;
1159 }
1160
1161 if ( (C==D) && (delay>0) ) {
1162 cv->CanSend = FALSE;
1163 SetTimer(cv->HWin, IdDelayTimer, delay, NULL);
1164 }
1165 }
1166
1167 void CommSendBreak(PComVar cv)
1168 /* for only serial ports */
1169 {
1170 MSG DummyMsg;
1171
1172 if ( ! cv->Ready ) {
1173 return;
1174 }
1175
1176 switch (cv->PortType) {
1177 case IdSerial:
1178 /* Set com port into a break state */
1179 SetCommBreak(cv->ComID);
1180
1181 /* pause for 1 sec */
1182 if (SetTimer(cv->HWin, IdBreakTimer, 1000, NULL) != 0) {
1183 GetMessage(&DummyMsg,cv->HWin,WM_TIMER,WM_TIMER);
1184 }
1185
1186 /* Set com port into the nonbreak state */
1187 ClearCommBreak(cv->ComID);
1188 break;
1189 }
1190 }
1191
1192 void CommLock(PTTSet ts, PComVar cv, BOOL Lock)
1193 {
1194 BYTE b;
1195 DWORD Func;
1196
1197 if (! cv->Ready) {
1198 return;
1199 }
1200 if ((cv->PortType==IdTCPIP) ||
1201 (cv->PortType==IdSerial) &&
1202 (ts->Flow!=IdFlowHard)) {
1203 if (Lock) {
1204 b = XOFF;
1205 }
1206 else {
1207 b = XON;
1208 }
1209 CommBinaryOut(cv,&b,1);
1210 }
1211 else if ((cv->PortType==IdSerial) &&
1212 (ts->Flow==IdFlowHard)) {
1213 if (Lock) {
1214 Func = CLRRTS;
1215 }
1216 else {
1217 Func = SETRTS;
1218 }
1219 EscapeCommFunction(cv->ComID,Func);
1220 }
1221 }
1222
1223 BOOL PrnOpen(PCHAR DevName)
1224 {
1225 char Temp[MAXPATHLEN], *c;
1226 DCB dcb;
1227 DWORD DErr;
1228 COMMTIMEOUTS ctmo;
1229 OSVERSIONINFO osvi;
1230
1231 strncpy_s(Temp, sizeof(Temp),DevName, _TRUNCATE);
1232 c = Temp;
1233 while (*c != '\0' && *c != ':') {
1234 c++;
1235 }
1236 *c = '\0';
1237 LPTFlag = (Temp[0]=='L') ||
1238 (Temp[0]=='l');
1239
1240 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1241 GetVersionEx(&osvi);
1242 if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
1243 // �l�b�g���[�N���L���}�b�v�������f�o�C�X�������������A�������������������������� (2011.01.25 maya)
1244 // http://logmett.com/forum/viewtopic.php?f=2&t=1383
1245 // http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx#5
1246 PrnID = CreateFile(Temp,GENERIC_WRITE | FILE_READ_ATTRIBUTES,
1247 FILE_SHARE_READ,NULL,CREATE_ALWAYS,
1248 0,NULL);
1249 }
1250 else {
1251 // 9x �������L���R�[�h���������������������]������������
1252 PrnID = CreateFile(Temp,GENERIC_WRITE,
1253 0,NULL,OPEN_EXISTING,
1254 0,NULL);
1255 }
1256
1257 if (PrnID == INVALID_HANDLE_VALUE) {
1258 return FALSE;
1259 }
1260
1261 if (GetCommState(PrnID,&dcb)) {
1262 BuildCommDCB(DevName,&dcb);
1263 SetCommState(PrnID,&dcb);
1264 }
1265 ClearCommError(PrnID,&DErr,NULL);
1266 if (! LPTFlag) {
1267 SetupComm(PrnID,0,CommOutQueSize);
1268 }
1269 /* flush output buffer */
1270 PurgeComm(PrnID, PURGE_TXABORT | PURGE_TXCLEAR);
1271 memset(&ctmo,0,sizeof(ctmo));
1272 ctmo.WriteTotalTimeoutConstant = 1000;
1273 SetCommTimeouts(PrnID,&ctmo);
1274 if (! LPTFlag) {
1275 EscapeCommFunction(PrnID,SETDTR);
1276 }
1277 return TRUE;
1278 }
1279
1280 int PrnWrite(PCHAR b, int c)
1281 {
1282 int d;
1283 DWORD DErr;
1284 COMSTAT Stat;
1285
1286 if (PrnID == INVALID_HANDLE_VALUE ) {
1287 return c;
1288 }
1289
1290 ClearCommError(PrnID,&DErr,&Stat);
1291 if (! LPTFlag &&
1292 (OutBuffSize - (int)Stat.cbOutQue < c)) {
1293 c = OutBuffSize - Stat.cbOutQue;
1294 }
1295 if (c<=0) {
1296 return 0;
1297 }
1298 if (! WriteFile(PrnID,b,c,(LPDWORD)&d,NULL)) {
1299 d = 0;
1300 }
1301 ClearCommError(PrnID,&DErr,NULL);
1302 return d;
1303 }
1304
1305 void PrnCancel()
1306 {
1307 PurgeComm(PrnID, PURGE_TXABORT | PURGE_TXCLEAR);
1308 PrnClose();
1309 }
1310
1311 void PrnClose()
1312 {
1313 if (PrnID != INVALID_HANDLE_VALUE) {
1314 if (!LPTFlag) {
1315 EscapeCommFunction(PrnID,CLRDTR);
1316 }
1317 CloseHandle(PrnID);
1318 }
1319 PrnID = INVALID_HANDLE_VALUE;
1320 }

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