TNB Library
TnbRs232c.h
[詳解]
1#pragma once
16#ifdef _WIN32_WCE
17 #define _TnbRS232C_EnumExistPorts_DISABLE
18#endif
19#include "TnbCommunication.h"
20#include "TnbStrEx.h"
21#ifndef _TnbRS232C_EnumExistPorts_DISABLE
22 #include "TnbRegistryAccessor.h"
23 #include <SetupApi.h>
24 #pragma comment(lib, "setupapi.lib")
25#endif
26
27
28
29//TNB Library
30namespace TNB
31{
32
33
34
72{
73public:
74
83 class CPort
84 {
85 public:
86
88 CPort(void)
89 {
90 }
91
97 CPort(int portNo)
98 {
99 #ifndef _WIN32_WCE
100 m_portName.Format(_T("\\\\.\\COM%d"), portNo);
101 #else
102 m_portName.Format(_T("COM%d:"), portNo);
103 #endif
104 }
105
110 CPort(LPCTSTR lpszPortName) : m_portName(lpszPortName)
111 {
112 }
113
114 #ifdef __AFX_H__
115
120 CPort(const CString& portName) : m_portName(portName)
121 {
122 }
123
124 #endif //__AFX_H__
125
130 const CStr& GetPortName(void) const
131 {
132 return m_portName;
133 }
134
139 int GetNumber(void) const
140 {
141 LPCTSTR P1 = m_portName;
142 LPCTSTR P2 = m_Find(P1);
143 if ( P2 != NULL )
144 {
145 return _ttoi(P2);
146 }
147 return -1;
148 }
149
154 CStr GetPrefix(void) const
155 {
156 CStr s;
157 LPCTSTR P1 = m_portName;
158 LPCTSTR P2 = m_Find(P1);
159 if ( P2 != NULL )
160 {
161 s = m_portName.Left(P2 - P1);
162 }
163 return s;
164 }
165
171 bool IsValid(void) const
172 {
173 return ! m_portName.IsEmpty();
174 }
175
176 private:
178 LPCTSTR m_Find(LPCTSTR lpsz) const
179 {
180 while ( (*lpsz) != 0 )
181 {
182 if ( (*lpsz) >= '0' && (*lpsz) <= '9' )
183 {
184 return lpsz;
185 }
186 lpsz++;
187 }
188 return NULL;
189 }
190 CStr m_portName;
191 };
192
195 {
196 Parity_Non = NOPARITY,
197 Parity_Odd = ODDPARITY,
198 Parity_Even = EVENPARITY,
199 Parity_Mark = MARKPARITY,
200 Parity_Space = SPACEPARITY,
201 //
202 #ifndef _TnbDOXYGEN //Document作成用シンボル
203 EP_NonParity = NOPARITY,
204 EP_OddParity = ODDPARITY,
205 EP_EvenParity = EVENPARITY,
206 EP_MarkParity = MARKPARITY,
207 EP_SpaceParity = SPACEPARITY,
208 #endif // _TnbDOXYGEN
209 };
212 {
213 StopBits_1 = ONESTOPBIT,
214 StopBits_1_5 = ONE5STOPBITS,
215 StopBits_2 = TWOSTOPBITS,
216 //
217 #ifndef _TnbDOXYGEN //Document作成用シンボル
218 ES_OneStopBit = ONESTOPBIT,
219 ES_One5StopBits = ONE5STOPBITS,
220 ES_TwoStopBits = TWOSTOPBITS,
221 #endif // _TnbDOXYGEN
222 };
223
224
225 //--------------------------------
226
227
229 CRs232c(void) : m_property(_T("RS232C"), TNBVIW_KIND_RS232C), m_hComPort(INVALID_HANDLE_VALUE)
230 , m_dwBaudRate(9600), m_bByteSize(7), m_eParity(Parity_Non)
231 , m_eStopBits(StopBits_1), m_boCanBulkSend(true), m_boIsConnect(false), m_isCheckCts(false)
232 {
233 ASSERT( m_property.receiveTimeout == 0 );
234 }
235
238 {
240 }
241
251 void GetParameter(CPort& _comPort, DWORD& _dwBaudRate, BYTE& _bByteSize, EParity& _eParity, EStopBits& _eStopBits) const
252 {
253 _comPort = m_comPort;
254 _dwBaudRate = m_dwBaudRate,
255 _bByteSize = m_bByteSize;
256 _eParity = m_eParity;
257 _eStopBits = m_eStopBits;
258 }
259
274 DWORD SetParameter(CPort comPort, DWORD dwBaudRate, BYTE bByteSize, EParity eParity, EStopBits eStopBits)
275 {
276 if ( m_comPort.GetNumber() != comPort.GetNumber() )
277 {
278 Close();
279 m_comPort = comPort;
280 }
281 m_dwBaudRate = dwBaudRate,
282 m_bByteSize = bByteSize;
283 m_eParity = eParity;
284 m_eStopBits = eStopBits;
285 if ( m_hComPort != INVALID_HANDLE_VALUE )
286 {
287 return m_PortSetting(m_hComPort);
288 }
289 return 0;
290 }
291
299 virtual bool SetReceiveTimeout(DWORD to)
300 {
301 return false;
302 }
303
309 void SetSendMode(bool canBulk)
310 {
311 m_boCanBulkSend = canBulk;
312 }
313
319 void SetCheckCtsMode(bool r = true)
320 {
321 m_isCheckCts = r;
322 }
323
329 virtual const TProperty& GetProperty(void) const
330 {
331 return m_property;
332 }
333
339 HANDLE GetHandle(void) const
340 {
341 return m_hComPort;
342 }
343
349 virtual bool Open(void)
350 {
351 Close();
352 if ( m_Open() == ERROR_SUCCESS )
353 {
354 m_property.typeName = CStr::Fmt(_T("RS232C(%s%d)"), m_comPort.GetPrefix(), m_comPort.GetNumber()); // タイプ名。
355 m_property.typeId = TNBVIW_KIND_RS232C + (m_comPort.GetNumber() & 0xFFFF);
356 return true;
357 }
358 return false;
359 }
360
364 virtual void Close(void)
365 {
366 if ( m_hComPort != INVALID_HANDLE_VALUE )
367 {
368 // ポートの設定を取得
369 HANDLE h = m_hComPort;
370 m_hComPort = INVALID_HANDLE_VALUE;
371 DCB tDCB;
372 if ( ::GetCommState(h, &tDCB) )
373 {
374 tDCB.fRtsControl = RTS_CONTROL_DISABLE;
375 tDCB.fDtrControl = DTR_CONTROL_DISABLE;
376 // ポートの状態を設定
377 ::SetCommState(h, &tDCB);
378 }
379 ::CloseHandle(h);
380 m_boIsConnect = false;
381 }
382 }
383
389 virtual bool IsOpened(void) const
390 {
391 return m_hComPort != INVALID_HANDLE_VALUE;
392 }
393
400 virtual bool IsConnect(void) const
401 {
402 return m_boIsConnect;
403 }
404
410 virtual size_t GetReceivingDataSize(void) const
411 {
412 // 現在の状態を取得。
413 DWORD dwErrors;
414 COMSTAT tComStat;
415 if ( ! ::ClearCommError(m_hComPort, &dwErrors, &tComStat) )
416 {
417 _GetLastError("ClearCommError");
418 return INVALID_SIZE;
419 }
420 // 現在受信しているDataを取得
421 return tComStat.cbInQue;
422 }
423
432 virtual EState Check(void) const
433 {
434 if ( IsOpened() )
435 {
436 DWORD dwStat;
437 if ( ! ::GetCommModemStatus(m_hComPort, &dwStat) )
438 {
439 _GetLastError("GetCommModemStatus");
440 }
441 else if ( m_isCheckCts )
442 {
443 m_boIsConnect = (dwStat & MS_CTS_ON) != 0;
444 return m_boIsConnect ? CONNECTING : OPENNING;
445 }
446 else
447 {
448 m_boIsConnect = true;
449 return CONNECTING;
450 }
451 }
452 return CLOSED;
453 }
454
462 virtual size_t Send(size_t size, LPCVOID P)
463 {
464 if ( ! IsOpened() )
465 {
466 return INVALID_SIZE;
467 }
468 if ( m_boCanBulkSend )
469 {
470 //=== 一括送信
471 const BYTE* B = static_cast<const BYTE*>(P);
472 size_t r = 0;
473 size_t writtenSize = 0;
474 while ( true )
475 {
476 if ( ! WritePort(writtenSize, size, B, m_hComPort) )
477 {
478 return INVALID_SIZE;
479 }
480 if ( writtenSize == 0 )
481 {
482 break;
483 }
484 r += writtenSize;
485 if ( size <= writtenSize )
486 {
487 break;
488 }
489 size -= writtenSize;
490 B += writtenSize;
491 }
492 return r;
493 }
494 //=== 一バイトずつ送信
495 const BYTE* B = static_cast<const BYTE*>(P);
496 for ( UINT i = 0; i < size; i++ )
497 {
498 int iErrCnt = 10; // エラーカウント
499 size_t writtenSize = 0;
500 while ( writtenSize == 0 && iErrCnt > 0 )
501 {
502 if ( ! WritePort(writtenSize, 1, &B[i], m_hComPort) )
503 {
504 // WriteFile関数がエラーを返した
505 return INVALID_SIZE;
506 }
507 if ( writtenSize != 1 )
508 {
509 iErrCnt --;
510 Sleep(1);
511 }
512 }
513 if ( iErrCnt < 1 )
514 {
515 return INVALID_SIZE;
516 }
517 }
518 return size;
519 }
520
529 virtual size_t Receive(size_t size, LPVOID _P)
530 {
531 DWORD dwErrors;
532 COMSTAT tComStat;
533 if ( ! ::ClearCommError(m_hComPort, &dwErrors, &tComStat) )
534 {
535 _GetLastError("ClearCommError");
536 return INVALID_SIZE;
537 }
538 if ( size > tComStat.cbInQue )
539 {
540 size = tComStat.cbInQue;
541 }
542 if ( size == 0 )
543 {
544 return 0;
545 }
546 size_t redSize = 0;
547 if ( ReadPort(redSize, _P, size, m_hComPort) )
548 {
549 return redSize;
550 }
551 return INVALID_SIZE;
552 }
553
558 virtual void Purge(void)
559 {
560 ::PurgeComm(m_hComPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
561 }
562
570 bool PaddingReceive(DWORD dwTime)
571 {
572 if ( ! IsOpened() )
573 {
574 return false;
575 }
576 //
577 bool boResult = false;
578 DWORD dwLoopCount = 0;
579 //
580 while ( true )
581 {
582 // 現在の状態を取得。
583 BOOL boIsClearComm;
584 DWORD dwErrors;
585 COMSTAT tComStat;
586 // エラー状態リセット&受信文字数確認
587 boIsClearComm = ::ClearCommError(m_hComPort, &dwErrors, &tComStat);
588 if ( ! boIsClearComm )
589 {
590 boResult = false;
591 break;
592 }
593 //
594 // 現在受信しているDataを取得
595 DWORD dwReceivedDataCount = tComStat.cbInQue;
596 //
597 if ( dwReceivedDataCount == 0 )
598 {
599 dwLoopCount++;
600 if ( dwLoopCount * 10 > dwTime )
601 {
602 boResult = true;
603 break;
604 }
605 }
606 else
607 {
608 dwLoopCount = 0;
609 //
610 CWorkMem tmp(dwReceivedDataCount);
611 // RS232Cからの読み込み
612 size_t redSize = 0;
613 if ( ! ReadPort(redSize, tmp.Ref(), tmp.GetSize(), m_hComPort) )
614 {
615 //失敗
616 boResult = false;
617 break;
618 }
619 if ( redSize == 0 )
620 {
621 boResult = true;
622 break;
623 }
624 }
625 ::Sleep(10);
626 }
627 //
628 return boResult;
629 }
630
632 enum
633 {
634 LOW = true,
635 HI = false
636 };
637
651 bool SetDtrSignal(bool isLow)
652 {
653 return !! ::EscapeCommFunction(m_hComPort, isLow ? SETDTR : CLRDTR);
654 }
655
669 bool SetRtsSignal(bool isLow)
670 {
671 return !! ::EscapeCommFunction(m_hComPort, isLow ? SETRTS : CLRRTS);
672 }
673
686 bool IsCtsSignalLow(void) const
687 {
688 DWORD w;
689 if ( ::GetCommModemStatus(m_hComPort, &w) )
690 {
691 return (w & MS_CTS_ON) != 0;
692 }
693 return false;
694 }
695
708 bool IsDsrSignalLow(void) const
709 {
710 DWORD w;
711 if ( ::GetCommModemStatus(m_hComPort, &w) )
712 {
713 return (w & MS_DSR_ON) != 0;
714 }
715 return false;
716 }
717
725 bool IsRingSignalLow(void) const
726 {
727 DWORD w;
728 if ( ::GetCommModemStatus(m_hComPort, &w) )
729 {
730 return (w & MS_RING_ON) != 0;
731 }
732 return false;
733 }
734
742 bool IsRlsdSignalLow(void) const
743 {
744 DWORD w;
745 if ( ::GetCommModemStatus(m_hComPort, &w) )
746 {
747 return (w & MS_RLSD_ON) != 0;
748 }
749 return false;
750 }
751
758 bool IsCtsSignalOn(void) const
759 {
760 return IsCtsSignalLow();
761 }
762
769 bool IsDsrSignalOn(void) const
770 {
771 return IsDsrSignalLow();
772 }
773
780 bool IsRingSignalOn(void) const
781 {
782 return IsRingSignalLow();
783 }
784
791 bool IsRlsdSignalOn(void) const
792 {
793 return IsRlsdSignalLow();
794 }
795
802 static DWORD CanPortBeOpened(CPort comPort)
803 {
804 // COMポート名作成
805 CStr strPortName = comPort.GetPortName();
806 // ポートを開く
807 HANDLE hComPort = ::CreateFile(
808 strPortName, // ファイルの名前のアドレス
809 GENERIC_READ | GENERIC_WRITE, // アクセス (読み取りおよび書き込み) モード
810 0, // 共有モード
811 NULL, // セキュリティ記述子のアドレス
812 OPEN_EXISTING, // 作成方法
813 FILE_ATTRIBUTE_NORMAL, // ファイル属性
814 NULL // コピーする属性付きファイルのハンドル
815 );
816 // ポートを開く関数が異常を返した場合
817 if ( hComPort == INVALID_HANDLE_VALUE )
818 {
819 return _GetLastError("CreateFile");
820 }
821 ::CloseHandle(hComPort);
822 return 0;
823 }
824
825 #ifndef _TnbRS232C_EnumExistPorts_DISABLE
826
829 struct TDevice : IComparableT<TDevice>
830 {
831 WORD port;
834 WORD vid;
835 WORD pid;
837 TDevice(void) : port(0), vid(0), pid(0)
838 {
839 }
845 CStr ToString(void) const
846 {
847 if ( description.Find(manufacturer) == 0 )
848 {
849 return CStr::Fmt(_T("%s (COM%d)"), description, port);
850 }
851 return CStr::Fmt(_T("%s %s (COM%d)"), manufacturer, description, port);
852 }
860 virtual INT_PTR Compare(const TDevice& t) const
861 {
862 return port - t.port;
863 }
864 };
865
868
874 {
875 CMyFinder ff;
876 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS);
877 return ff.Execute(reg[_T("SYSTEM\\CurrentControlSet\\Enum")]);
878 }
879
885 {
886 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS);
887 CRegistryAccessor::CSection regsub = reg[_T("HARDWARE\\DEVICEMAP\\SERIALCOMM")];
888 CStrVector vstrNames = regsub.EnumKeyNames();
889 //
890 CWordVector vw;
891 loop ( i, vstrNames.GetSize() )
892 {
893 CStr str = regsub.QueryString(vstrNames[i]);
894 if ( str.GetLength() >= 4 )
895 {
896 int iPort = str.ToInt(3);
897 if ( iPort >= 0 && iPort <= 256 )
898 {
899 vw.Add(static_cast<WORD>(iPort));
900 }
901 }
902 }
903 vw.Sort();
904 return vw;
905 }
906
913 static CWordVector EnumExistPortsByUsb(WORD vid, WORD pid)
914 {
915 CWordVector vw;
917 loop ( i, vd )
918 {
919 const TDevice& d = vd[i];
920 if ( d.vid == vid && d.pid == pid )
921 {
922 vw.Add(d.port);
923 }
924 }
925 vw.Sort();
926 return vw;
927 }
928
929 #endif
930
931protected:
932
942 virtual bool OnRs232cOpening(HANDLE hComPort, DCB& _tDCB, COMMTIMEOUTS& _tCommTimeOuts)
943 {
944 return true;
945 }
946
954 virtual HANDLE CreatePort(LPCTSTR lpszPortName) const
955 {
956 DWORD desiredAccess = GENERIC_READ | GENERIC_WRITE; // アクセス (読み取りおよび書き込み) モード
957 DWORD flag = FILE_ATTRIBUTE_NORMAL;
958 HANDLE h = ::CreateFile(lpszPortName, desiredAccess, 0, NULL, OPEN_EXISTING, flag, NULL);
959 _GetLastError("CreateFile");
960 return h;
961 }
962
973 virtual bool WritePort(size_t& _writtenSize, size_t len, LPCVOID P, HANDLE h) const
974 {
975 DWORD dwWritten = 0;
976 if ( ! ::WriteFile(h, P, ToDword(len), &dwWritten, NULL) )
977 {
978 _GetLastError("WriteFile");
979 return false;
980 }
981 _writtenSize = dwWritten;
982 return true;
983 }
984
995 virtual bool ReadPort(size_t& _redSize, LPVOID _P, size_t len, HANDLE h) const
996 {
997 DWORD dwRed = 0;
998 if ( ! ::ReadFile(h, _P, ToDword(len), &dwRed, NULL) )
999 {
1000 _GetLastError("ReadFile");
1001 return false;
1002 }
1003 _redSize = dwRed;
1004 return true;
1005 }
1006
1007private:
1008
1014 DWORD m_Open(void)
1015 {
1016 if ( ! m_comPort.IsValid() )
1017 {
1018 return ERROR_INVALID_PARAMETER;
1019 }
1020 // ポートを開く
1021 HANDLE hComPort = CreatePort(m_comPort.GetPortName());
1022 // ポートを開く関数が異常を返した場合
1023 if ( hComPort == INVALID_HANDLE_VALUE )
1024 {
1025 return ::GetLastError();
1026 }
1027 ::PurgeComm(hComPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
1028 // ポートの設定を取得
1029 DWORD r = m_PortSetting(hComPort);
1030 if ( r != 0 )
1031 {
1032 ::CloseHandle(hComPort);
1033 return r;
1034 }
1035 //
1036 COMSTAT tComStat;
1037 ::ClearCommError(hComPort, &r, &tComStat);
1038 ::SetCommMask(hComPort, 0);
1039 ::PurgeComm(hComPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
1040 m_hComPort = hComPort;
1041 return 0;
1042 }
1043
1049 DWORD m_PortSetting(HANDLE hComPort)
1050 {
1051 DCB tDCB;
1052 COMMTIMEOUTS tCommTimeOuts;
1053 if ( ! ::GetCommState(hComPort, &tDCB) || ! ::GetCommTimeouts(hComPort, &tCommTimeOuts) )
1054 {
1055 return _GetLastError("GetCommState/GetCommTimeouts");
1056 }
1057 // ポートを設定
1058 tDCB.BaudRate = m_dwBaudRate;
1059 tDCB.ByteSize = m_bByteSize;
1060 tDCB.Parity = static_cast<BYTE>(m_eParity);
1061 tDCB.StopBits = static_cast<BYTE>(m_eStopBits);
1062 if ( m_eParity == EP_NonParity )
1063 {
1064 tDCB.fParity = 0;
1065 }
1066 else
1067 {
1068 tDCB.fParity = 1;
1069 }
1070 //ソフトフロー状態にする。
1071 tCommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
1072 tCommTimeOuts.ReadTotalTimeoutMultiplier = 0;
1073 tCommTimeOuts.ReadTotalTimeoutConstant = 0;
1074 tCommTimeOuts.WriteTotalTimeoutMultiplier = 1;
1075 tCommTimeOuts.WriteTotalTimeoutConstant = 1000; //1s
1076 tDCB.fRtsControl = RTS_CONTROL_DISABLE;
1077 tDCB.fDtrControl = DTR_CONTROL_DISABLE;
1078 tDCB.fOutxCtsFlow = FALSE; //ClearToSend監視しない
1079 tDCB.fOutxDsrFlow = FALSE; //DataSetReady監視しない
1080 tDCB.fDsrSensitivity = FALSE; //DSR監視しない
1081 tDCB.fTXContinueOnXoff = FALSE; //XOffCharチェックなし
1082 tDCB.fInX = FALSE; //XON/XOFFフロー制御なし
1083 tDCB.fOutX = FALSE; //XON/XOFFフロー制御なし
1084 if ( ! OnRs232cOpening(hComPort, tDCB, tCommTimeOuts) )
1085 {
1086 return ERROR_NOT_READY; //ユーザ停止
1087 }
1088 // ポートの状態を設定
1089 if ( ! ::SetCommState(hComPort, &tDCB) || ! ::SetCommTimeouts(hComPort, &tCommTimeOuts) )
1090 {
1091 return _GetLastError("SetCommState/SetCommTimeouts");
1092 }
1093 return 0;
1094 }
1095 TProperty m_property;
1096 HANDLE m_hComPort;
1097 CPort m_comPort;
1098 DWORD m_dwBaudRate;
1099 BYTE m_bByteSize;
1100 EParity m_eParity;
1101 EStopBits m_eStopBits;
1102 mutable bool m_boIsConnect;
1103 bool m_boCanBulkSend;
1104 bool m_isCheckCts;
1105
1106 #ifndef _TnbRS232C_EnumExistPorts_DISABLE
1107
1109 class CMyFinder : private CDeepAccessFinder
1110 {
1111 DEFSUPER(CDeepAccessFinder);
1112 public:
1113 // 実行
1114 TDeviceVector Execute(IAccessor::CSection& section)
1115 {
1116 TDeviceVector vd;
1117 m_foundDriver.RemoveAll();
1118 m_foundPort.RemoveAll();
1119 _super::Execute(section);
1120 size_t l = m_foundDriver.GetSize();
1121 ASSERT( l == m_foundPort.GetSize() );
1122 if ( l > 0 )
1123 {
1124 const int BUF_MAX = 200;
1125 TCHAR buf[BUF_MAX];
1126 DWORD len = 0;
1127 BYTE* B = reinterpret_cast<BYTE*>(buf);
1128 SP_DEVINFO_DATA dev = { sizeof(SP_DEVINFO_DATA) };
1129 HDEVINFO hDevInfo = ::SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES);
1130 for ( int i = 0; ::SetupDiEnumDeviceInfo(hDevInfo, i, &dev); i++ )
1131 {
1132 if ( ::SetupDiGetDeviceRegistryProperty(hDevInfo, &dev, SPDRP_DRIVER, NULL, B, BUF_MAX, &len) )
1133 {
1134 INDEX f = m_foundDriver.Find(buf);
1135 if ( f != INVALID_INDEX )
1136 {
1137 TDevice t;
1138 if ( ::SetupDiGetDeviceRegistryProperty(hDevInfo, &dev, SPDRP_DEVICEDESC, NULL, B, BUF_MAX, &len) )
1139 {
1140 t.description = buf;
1141 }
1142 if ( ::SetupDiGetDeviceRegistryProperty(hDevInfo, &dev, SPDRP_MFG, NULL, B, BUF_MAX, &len) )
1143 {
1144 t.manufacturer = buf;
1145 }
1146 if ( ::SetupDiGetDeviceRegistryProperty(hDevInfo, &dev, SPDRP_HARDWAREID, NULL, B, BUF_MAX, &len) )
1147 {
1148 CStr s = LowerString(buf); //USB\Vid_067b&Pid_2303&Rev_0300
1149 INT_PTR f1 = s.Find(_T("vid_"));
1150 if ( f1 >= 0 )
1151 {
1152 t.vid = down_cast<WORD>(STRLIB::ToInt(s.Mid(f1 + 4, 4), 16));
1153 }
1154 INT_PTR f2 = s.Find(_T("pid_"));
1155 if ( f2 >= 0 )
1156 {
1157 t.pid = down_cast<WORD>(STRLIB::ToInt(s.Mid(f2 + 4, 4), 16));
1158 }
1159 }
1160 t.port = m_foundPort[f];
1161 vd.Add(t);
1162 }
1163 }
1164 }
1165 VERIFY ( ::SetupDiDestroyDeviceInfoList(hDevInfo) );
1166 }
1167 vd.Sort();
1168 return vd;
1169 }
1170 protected:
1171 virtual EResult OnFindSection(LPCTSTR lpszSection, IAccessor::CSection& sec, int depth)
1172 {
1173 if ( STRLIB::Compare(lpszSection, _T("Device Parameters")) == 0 )
1174 {
1175 if ( sec.HasKey(_T("PortName")) )
1176 {
1177 CStr s = sec.QueryString(_T("PortName"));
1178 if ( s.Find(_T("COM")) == 0 )
1179 {
1180 m_foundPort.Add(static_cast<WORD>(s.ToInt(3)));
1181 IAccessor::CSection s = sec;
1183 m_foundDriver.Add(s.QueryString(_T("Driver")));
1184 }
1185 return Previous;
1186 }
1187 }
1188 return Next;
1189 }
1190 private:
1191 CWordVector m_foundPort;
1192 CStrVector m_foundDriver;
1193 };
1194
1195 #endif
1196
1197 friend class CRs232cTest;
1198};
1199
1200
1201
1204class CRs232cHf : public CRs232c
1205{
1206protected:
1216 virtual bool OnRs232cOpening(HANDLE hComPort, DCB& _tDCB, COMMTIMEOUTS& _tCommTimeOuts)
1217 {
1218 _tCommTimeOuts.WriteTotalTimeoutMultiplier = 1;
1219 _tCommTimeOuts.WriteTotalTimeoutConstant = 1000;
1220 //
1221// _tDCB.fOutxDsrFlow = TRUE;
1222// _tDCB.fDtrControl = DTR_CONTROL_HANDSHAKE;
1223 _tDCB.fOutxCtsFlow = TRUE;
1224 _tDCB.fRtsControl = RTS_CONTROL_HANDSHAKE;
1225 _tDCB.fDsrSensitivity = FALSE;
1226 //
1227 _tDCB.fTXContinueOnXoff = FALSE;
1228 _tDCB.fOutX = FALSE;
1229 _tDCB.fInX = FALSE;
1230 return true;
1231 }
1232};
1233
1234
1235
1236}; // TNB
通信関係のヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
レジストリアクセス関係のヘッダ
文字列処理関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
ディープ検索クラス
Definition: TnbAccessor.h:1093
レジストリアクセスクラス
ハードフロー制御タイプ
Definition: TnbRs232c.h:1205
virtual bool OnRs232cOpening(HANDLE hComPort, DCB &_tDCB, COMMTIMEOUTS &_tCommTimeOuts)
[通知] オープン通知.
Definition: TnbRs232c.h:1216
RS232C ポート管理
Definition: TnbRs232c.h:84
int GetNumber(void) const
[取得] 数値取得.
Definition: TnbRs232c.h:139
CPort(const CString &portName)
コンストラクタ.
Definition: TnbRs232c.h:120
CStr GetPrefix(void) const
[取得] プリフィックス取得
Definition: TnbRs232c.h:154
CPort(int portNo)
コンストラクタ.
Definition: TnbRs232c.h:97
bool IsValid(void) const
[確認] 有効確認.
Definition: TnbRs232c.h:171
CPort(void)
コンストラクタ
Definition: TnbRs232c.h:88
const CStr & GetPortName(void) const
[取得] ポート名取得.
Definition: TnbRs232c.h:130
CPort(LPCTSTR lpszPortName)
コンストラクタ.
Definition: TnbRs232c.h:110
RS232Cアクセスクラス
Definition: TnbRs232c.h:72
virtual EState Check(void) const
[確認] チェック.
Definition: TnbRs232c.h:432
bool IsRingSignalOn(void) const
[取得] RING 確認.
Definition: TnbRs232c.h:780
bool IsCtsSignalLow(void) const
[取得] CTS 確認.
Definition: TnbRs232c.h:686
static DWORD CanPortBeOpened(CPort comPort)
[確認] 指定ポートは開けるか
Definition: TnbRs232c.h:802
bool IsDsrSignalLow(void) const
[取得] DSR 確認.
Definition: TnbRs232c.h:708
virtual bool SetReceiveTimeout(DWORD to)
[設定] 受信待ち時間設定.
Definition: TnbRs232c.h:299
EParity
パリティ設定値
Definition: TnbRs232c.h:195
@ Parity_Non
なしパリティ
Definition: TnbRs232c.h:196
@ Parity_Mark
マークパリティ
Definition: TnbRs232c.h:199
@ Parity_Space
空白パリティ
Definition: TnbRs232c.h:200
@ Parity_Odd
奇数パリティ
Definition: TnbRs232c.h:197
@ Parity_Even
偶数パリティ
Definition: TnbRs232c.h:198
CRs232c(void)
コンストラクタ
Definition: TnbRs232c.h:229
virtual bool Open(void)
[処理] オープン.
Definition: TnbRs232c.h:349
friend class CRs232cTest
フレンドクラス宣言
Definition: TnbRs232c.h:1197
bool IsRingSignalLow(void) const
[取得] RING 確認.
Definition: TnbRs232c.h:725
DWORD SetParameter(CPort comPort, DWORD dwBaudRate, BYTE bByteSize, EParity eParity, EStopBits eStopBits)
[設定] オープン設定.
Definition: TnbRs232c.h:274
HANDLE GetHandle(void) const
[取得] ハンドル取得
Definition: TnbRs232c.h:339
virtual bool WritePort(size_t &_writtenSize, size_t len, LPCVOID P, HANDLE h) const
[通知] ポート書き込み.
Definition: TnbRs232c.h:973
virtual bool OnRs232cOpening(HANDLE hComPort, DCB &_tDCB, COMMTIMEOUTS &_tCommTimeOuts)
[通知] オープン通知.
Definition: TnbRs232c.h:942
@ LOW
LOW (ON)
Definition: TnbRs232c.h:634
@ HI
HI (OFF)
Definition: TnbRs232c.h:635
virtual bool ReadPort(size_t &_redSize, LPVOID _P, size_t len, HANDLE h) const
[通知] ポート読み込み.
Definition: TnbRs232c.h:995
static TDeviceVector EnumExistPortDeviceNames(void)
[取得] 存在するPort一覧
Definition: TnbRs232c.h:873
EStopBits
ストップビット設定値
Definition: TnbRs232c.h:212
@ StopBits_2
2bit
Definition: TnbRs232c.h:215
@ StopBits_1
1bit
Definition: TnbRs232c.h:213
@ StopBits_1_5
1.5bit
Definition: TnbRs232c.h:214
void SetSendMode(bool canBulk)
[設定] 送信設定.
Definition: TnbRs232c.h:309
bool PaddingReceive(DWORD dwTime)
[操作] パディング読み込み
Definition: TnbRs232c.h:570
CVectorT< TDevice > TDeviceVector
デバイス情報配列.
Definition: TnbRs232c.h:867
bool SetDtrSignal(bool isLow)
[設定] DTR 設定.
Definition: TnbRs232c.h:651
bool IsRlsdSignalLow(void) const
[取得] RLSD 確認.
Definition: TnbRs232c.h:742
bool IsRlsdSignalOn(void) const
[取得] RLSD 確認.
Definition: TnbRs232c.h:791
virtual HANDLE CreatePort(LPCTSTR lpszPortName) const
[通知] ポート作成.
Definition: TnbRs232c.h:954
virtual void Purge(void)
[設定] パージ.
Definition: TnbRs232c.h:558
void GetParameter(CPort &_comPort, DWORD &_dwBaudRate, BYTE &_bByteSize, EParity &_eParity, EStopBits &_eStopBits) const
[取得] オープン設定取得.
Definition: TnbRs232c.h:251
virtual bool IsConnect(void) const
[確認] 接続確認.
Definition: TnbRs232c.h:400
bool SetRtsSignal(bool isLow)
[設定] RTS 設定.
Definition: TnbRs232c.h:669
static CWordVector EnumExistPortsByUsb(WORD vid, WORD pid)
[取得] USBシリアルPort一覧.
Definition: TnbRs232c.h:913
virtual void Close(void)
[処理] クローズ.
Definition: TnbRs232c.h:364
bool IsCtsSignalOn(void) const
[取得] CTS 確認.
Definition: TnbRs232c.h:758
virtual bool IsOpened(void) const
[確認] オープン確認.
Definition: TnbRs232c.h:389
virtual size_t Receive(size_t size, LPVOID _P)
[処理] 受信.
Definition: TnbRs232c.h:529
~CRs232c(void)
デストラクタ
Definition: TnbRs232c.h:237
void SetCheckCtsMode(bool r=true)
[設定] CTSチェック設定.
Definition: TnbRs232c.h:319
static CWordVector EnumExistPorts(void)
[取得] 存在するPort一覧
Definition: TnbRs232c.h:884
virtual const TProperty & GetProperty(void) const
[取得] 性能取得
Definition: TnbRs232c.h:329
virtual size_t GetReceivingDataSize(void) const
[確認] 受信データ確認.
Definition: TnbRs232c.h:410
bool IsDsrSignalOn(void) const
[取得] DSR 確認.
Definition: TnbRs232c.h:769
virtual size_t Send(size_t size, LPCVOID P)
[処理] 送信.
Definition: TnbRs232c.h:462
bool IsEmpty(void) const
[確認] 空チェック
Definition: TnbStr.h:528
CStrT Left(size_t iSize) const
[作成] 範囲取得.
Definition: TnbStr.h:801
size_t GetLength(void) const
[取得] 文字列長
Definition: TnbStr.h:518
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
Definition: TnbStr.h:1206
INT_PTR Find(TYP t, INDEX iFromIndex=0) const
[確認] 検索.
Definition: TnbStr.h:540
void Format(const TYP *lpszFormat,...)
[代入] 書式付き文字列代入.
Definition: TnbStr.h:359
CStrT Mid(INDEX iOffset, size_t iSize=INVALID_SIZE) const
[作成] 範囲取得.
Definition: TnbStr.h:766
int ToInt(INDEX iOffset=0, int iBase=10) const
[取得] 数値(int)へ変換
Definition: TnbStr.h:842
配列型情報管理テンプレート
Definition: TnbVector.h:75
virtual size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbVector.h:368
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
Definition: TnbVector.h:383
size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbDef.h:665
const TYP * Ref(void) const
[取得] ポインタ取得
Definition: TnbDef.h:712
セクション情報アクセスクラス
Definition: TnbAccessor.h:294
CStrVector EnumKeyNames(void) const
[取得] キー名一覧取得
Definition: TnbAccessor.h:419
CStr QueryString(LPCTSTR lpszKey, LPCTSTR lpszDefault=NULL) const
[取得] 文字列情報取得
Definition: TnbAccessor.h:506
bool HasKey(LPCTSTR lpszKey) const
[確認] 情報存在確認
Definition: TnbAccessor.h:450
void PreviousSubSection(void)
[設定] 対象セクション変更.
Definition: TnbAccessor.h:384
int Compare(LPCSTR P1, LPCSTR P2, INT_PTR len=-1, DWORD dwCmpFlags=0)
[比較] 文字列比較(ASCII/SJIS用)
Definition: TnbStrLib.h:135
DWORD ToDword(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:395
CAscii LowerString(LPCSTR lpsz)
[変換] 小文字変換(ASCII/SJIS用)
Definition: TnbStrEx.h:221
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:367
TNB Library
Definition: TnbDoxyTitle.txt:2
デバイス情報
Definition: TnbRs232c.h:830
TDevice(void)
コンストラクタ
Definition: TnbRs232c.h:837
virtual INT_PTR Compare(const TDevice &t) const
[確認] 比較
Definition: TnbRs232c.h:860
CStr description
説明
Definition: TnbRs232c.h:832
WORD pid
ProductID (USB)
Definition: TnbRs232c.h:835
WORD port
ポートNo
Definition: TnbRs232c.h:831
CStr manufacturer
製造業者
Definition: TnbRs232c.h:833
WORD vid
VendorID (USB)
Definition: TnbRs232c.h:834
CStr ToString(void) const
[取得] 文字列化.
Definition: TnbRs232c.h:845
bool Sort(const IComparatorT< TYP > &comparator, bool boIsReverse=false, bool boIsBubble=false)
[操作] ソート.
性能プロパティ型.
DWORD receiveTimeout
受信待ち時間。 INFINITE ならブロックされる。
通信インターフェース
@ CONNECTING
オープン&接続状態
@ CLOSED
クローズ状態
@ OPENNING
オープン状態
比較機能インターフェース.
Definition: TnbComparable.h:54