17 #define _TnbRS232C_EnumExistPorts_DISABLE
21#ifndef _TnbRS232C_EnumExistPorts_DISABLE
24 #pragma comment(lib, "setupapi.lib")
100 m_portName.
Format(_T(
"\\\\.\\COM%d"), portNo);
102 m_portName.
Format(_T(
"COM%d:"), portNo);
110 CPort(LPCTSTR lpszPortName) : m_portName(lpszPortName)
120 CPort(
const CString& portName) : m_portName(portName)
141 LPCTSTR P1 = m_portName;
142 LPCTSTR P2 = m_Find(P1);
157 LPCTSTR P1 = m_portName;
158 LPCTSTR P2 = m_Find(P1);
161 s = m_portName.
Left(P2 - P1);
178 LPCTSTR m_Find(LPCTSTR lpsz)
const
180 while ( (*lpsz) != 0 )
182 if ( (*lpsz) >=
'0' && (*lpsz) <=
'9' )
203 EP_NonParity = NOPARITY,
204 EP_OddParity = ODDPARITY,
205 EP_EvenParity = EVENPARITY,
206 EP_MarkParity = MARKPARITY,
207 EP_SpaceParity = SPACEPARITY,
218 ES_OneStopBit = ONESTOPBIT,
219 ES_One5StopBits = ONE5STOPBITS,
220 ES_TwoStopBits = TWOSTOPBITS,
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)
253 _comPort = m_comPort;
254 _dwBaudRate = m_dwBaudRate,
255 _bByteSize = m_bByteSize;
256 _eParity = m_eParity;
257 _eStopBits = m_eStopBits;
281 m_dwBaudRate = dwBaudRate,
282 m_bByteSize = bByteSize;
284 m_eStopBits = eStopBits;
285 if ( m_hComPort != INVALID_HANDLE_VALUE )
287 return m_PortSetting(m_hComPort);
311 m_boCanBulkSend = canBulk;
352 if ( m_Open() == ERROR_SUCCESS )
355 m_property.
typeId = TNBVIW_KIND_RS232C + (m_comPort.
GetNumber() & 0xFFFF);
366 if ( m_hComPort != INVALID_HANDLE_VALUE )
369 HANDLE h = m_hComPort;
370 m_hComPort = INVALID_HANDLE_VALUE;
372 if ( ::GetCommState(h, &tDCB) )
374 tDCB.fRtsControl = RTS_CONTROL_DISABLE;
375 tDCB.fDtrControl = DTR_CONTROL_DISABLE;
377 ::SetCommState(h, &tDCB);
380 m_boIsConnect =
false;
391 return m_hComPort != INVALID_HANDLE_VALUE;
402 return m_boIsConnect;
415 if ( ! ::ClearCommError(m_hComPort, &dwErrors, &tComStat) )
417 _GetLastError(
"ClearCommError");
421 return tComStat.cbInQue;
437 if ( ! ::GetCommModemStatus(m_hComPort, &dwStat) )
439 _GetLastError(
"GetCommModemStatus");
441 else if ( m_isCheckCts )
443 m_boIsConnect = (dwStat & MS_CTS_ON) != 0;
448 m_boIsConnect =
true;
462 virtual size_t Send(
size_t size, LPCVOID P)
468 if ( m_boCanBulkSend )
471 const BYTE* B =
static_cast<const BYTE*
>(P);
473 size_t writtenSize = 0;
476 if ( !
WritePort(writtenSize, size, B, m_hComPort) )
480 if ( writtenSize == 0 )
485 if ( size <= writtenSize )
495 const BYTE* B =
static_cast<const BYTE*
>(P);
496 for ( UINT i = 0; i < size; i++ )
499 size_t writtenSize = 0;
500 while ( writtenSize == 0 && iErrCnt > 0 )
502 if ( !
WritePort(writtenSize, 1, &B[i], m_hComPort) )
507 if ( writtenSize != 1 )
529 virtual size_t Receive(
size_t size, LPVOID _P)
533 if ( ! ::ClearCommError(m_hComPort, &dwErrors, &tComStat) )
535 _GetLastError(
"ClearCommError");
538 if ( size > tComStat.cbInQue )
540 size = tComStat.cbInQue;
547 if (
ReadPort(redSize, _P, size, m_hComPort) )
560 ::PurgeComm(m_hComPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
577 bool boResult =
false;
578 DWORD dwLoopCount = 0;
587 boIsClearComm = ::ClearCommError(m_hComPort, &dwErrors, &tComStat);
588 if ( ! boIsClearComm )
595 DWORD dwReceivedDataCount = tComStat.cbInQue;
597 if ( dwReceivedDataCount == 0 )
600 if ( dwLoopCount * 10 > dwTime )
653 return !! ::EscapeCommFunction(m_hComPort, isLow ? SETDTR : CLRDTR);
671 return !! ::EscapeCommFunction(m_hComPort, isLow ? SETRTS : CLRRTS);
689 if ( ::GetCommModemStatus(m_hComPort, &w) )
691 return (w & MS_CTS_ON) != 0;
711 if ( ::GetCommModemStatus(m_hComPort, &w) )
713 return (w & MS_DSR_ON) != 0;
728 if ( ::GetCommModemStatus(m_hComPort, &w) )
730 return (w & MS_RING_ON) != 0;
745 if ( ::GetCommModemStatus(m_hComPort, &w) )
747 return (w & MS_RLSD_ON) != 0;
807 HANDLE hComPort = ::CreateFile(
809 GENERIC_READ | GENERIC_WRITE,
813 FILE_ATTRIBUTE_NORMAL,
817 if ( hComPort == INVALID_HANDLE_VALUE )
819 return _GetLastError(
"CreateFile");
821 ::CloseHandle(hComPort);
825 #ifndef _TnbRS232C_EnumExistPorts_DISABLE
876 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS);
877 return ff.Execute(reg[_T(
"SYSTEM\\CurrentControlSet\\Enum")]);
886 CRegistryAccessor reg(HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS);
896 int iPort = str.
ToInt(3);
897 if ( iPort >= 0 && iPort <= 256 )
899 vw.
Add(
static_cast<WORD
>(iPort));
920 if ( d.
vid == vid && d.
pid == pid )
942 virtual bool OnRs232cOpening(HANDLE hComPort, DCB& _tDCB, COMMTIMEOUTS& _tCommTimeOuts)
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");
973 virtual bool WritePort(
size_t& _writtenSize,
size_t len, LPCVOID P, HANDLE h)
const
976 if ( ! ::WriteFile(h, P,
ToDword(len), &dwWritten, NULL) )
978 _GetLastError(
"WriteFile");
981 _writtenSize = dwWritten;
995 virtual bool ReadPort(
size_t& _redSize, LPVOID _P,
size_t len, HANDLE h)
const
998 if ( ! ::ReadFile(h, _P,
ToDword(len), &dwRed, NULL) )
1000 _GetLastError(
"ReadFile");
1018 return ERROR_INVALID_PARAMETER;
1023 if ( hComPort == INVALID_HANDLE_VALUE )
1025 return ::GetLastError();
1027 ::PurgeComm(hComPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
1029 DWORD r = m_PortSetting(hComPort);
1032 ::CloseHandle(hComPort);
1037 ::ClearCommError(hComPort, &r, &tComStat);
1038 ::SetCommMask(hComPort, 0);
1039 ::PurgeComm(hComPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
1040 m_hComPort = hComPort;
1049 DWORD m_PortSetting(HANDLE hComPort)
1052 COMMTIMEOUTS tCommTimeOuts;
1053 if ( ! ::GetCommState(hComPort, &tDCB) || ! ::GetCommTimeouts(hComPort, &tCommTimeOuts) )
1055 return _GetLastError(
"GetCommState/GetCommTimeouts");
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 )
1071 tCommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
1072 tCommTimeOuts.ReadTotalTimeoutMultiplier = 0;
1073 tCommTimeOuts.ReadTotalTimeoutConstant = 0;
1074 tCommTimeOuts.WriteTotalTimeoutMultiplier = 1;
1075 tCommTimeOuts.WriteTotalTimeoutConstant = 1000;
1076 tDCB.fRtsControl = RTS_CONTROL_DISABLE;
1077 tDCB.fDtrControl = DTR_CONTROL_DISABLE;
1078 tDCB.fOutxCtsFlow = FALSE;
1079 tDCB.fOutxDsrFlow = FALSE;
1080 tDCB.fDsrSensitivity = FALSE;
1081 tDCB.fTXContinueOnXoff = FALSE;
1086 return ERROR_NOT_READY;
1089 if ( ! ::SetCommState(hComPort, &tDCB) || ! ::SetCommTimeouts(hComPort, &tCommTimeOuts) )
1091 return _GetLastError(
"SetCommState/SetCommTimeouts");
1095 TProperty m_property;
1102 mutable bool m_boIsConnect;
1103 bool m_boCanBulkSend;
1106 #ifndef _TnbRS232C_EnumExistPorts_DISABLE
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() );
1124 const int BUF_MAX = 200;
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++ )
1132 if ( ::SetupDiGetDeviceRegistryProperty(hDevInfo, &dev, SPDRP_DRIVER, NULL, B, BUF_MAX, &len) )
1134 INDEX f = m_foundDriver.Find(buf);
1135 if ( f != INVALID_INDEX )
1138 if ( ::SetupDiGetDeviceRegistryProperty(hDevInfo, &dev, SPDRP_DEVICEDESC, NULL, B, BUF_MAX, &len) )
1140 t.description = buf;
1142 if ( ::SetupDiGetDeviceRegistryProperty(hDevInfo, &dev, SPDRP_MFG, NULL, B, BUF_MAX, &len) )
1144 t.manufacturer = buf;
1146 if ( ::SetupDiGetDeviceRegistryProperty(hDevInfo, &dev, SPDRP_HARDWAREID, NULL, B, BUF_MAX, &len) )
1149 INT_PTR f1 = s.
Find(_T(
"vid_"));
1154 INT_PTR f2 = s.
Find(_T(
"pid_"));
1160 t.port = m_foundPort[f];
1165 VERIFY ( ::SetupDiDestroyDeviceInfoList(hDevInfo) );
1175 if ( sec.
HasKey(_T(
"PortName")) )
1178 if ( s.
Find(_T(
"COM")) == 0 )
1180 m_foundPort.Add(
static_cast<WORD
>(s.
ToInt(3)));
1218 _tCommTimeOuts.WriteTotalTimeoutMultiplier = 1;
1219 _tCommTimeOuts.WriteTotalTimeoutConstant = 1000;
1223 _tDCB.fOutxCtsFlow = TRUE;
1224 _tDCB.fRtsControl = RTS_CONTROL_HANDSHAKE;
1225 _tDCB.fDsrSensitivity = FALSE;
1227 _tDCB.fTXContinueOnXoff = FALSE;
1228 _tDCB.fOutX = FALSE;
#define loop(VAR, CNT)
loop構文.
virtual bool OnRs232cOpening(HANDLE hComPort, DCB &_tDCB, COMMTIMEOUTS &_tCommTimeOuts)
[通知] オープン通知.
int GetNumber(void) const
[取得] 数値取得.
CPort(const CString &portName)
コンストラクタ.
CStr GetPrefix(void) const
[取得] プリフィックス取得
CPort(int portNo)
コンストラクタ.
bool IsValid(void) const
[確認] 有効確認.
const CStr & GetPortName(void) const
[取得] ポート名取得.
CPort(LPCTSTR lpszPortName)
コンストラクタ.
virtual EState Check(void) const
[確認] チェック.
bool IsRingSignalOn(void) const
[取得] RING 確認.
bool IsCtsSignalLow(void) const
[取得] CTS 確認.
static DWORD CanPortBeOpened(CPort comPort)
[確認] 指定ポートは開けるか
bool IsDsrSignalLow(void) const
[取得] DSR 確認.
virtual bool SetReceiveTimeout(DWORD to)
[設定] 受信待ち時間設定.
virtual bool Open(void)
[処理] オープン.
friend class CRs232cTest
フレンドクラス宣言
bool IsRingSignalLow(void) const
[取得] RING 確認.
DWORD SetParameter(CPort comPort, DWORD dwBaudRate, BYTE bByteSize, EParity eParity, EStopBits eStopBits)
[設定] オープン設定.
HANDLE GetHandle(void) const
[取得] ハンドル取得
virtual bool WritePort(size_t &_writtenSize, size_t len, LPCVOID P, HANDLE h) const
[通知] ポート書き込み.
virtual bool OnRs232cOpening(HANDLE hComPort, DCB &_tDCB, COMMTIMEOUTS &_tCommTimeOuts)
[通知] オープン通知.
virtual bool ReadPort(size_t &_redSize, LPVOID _P, size_t len, HANDLE h) const
[通知] ポート読み込み.
static TDeviceVector EnumExistPortDeviceNames(void)
[取得] 存在するPort一覧
void SetSendMode(bool canBulk)
[設定] 送信設定.
bool PaddingReceive(DWORD dwTime)
[操作] パディング読み込み
CVectorT< TDevice > TDeviceVector
デバイス情報配列.
bool SetDtrSignal(bool isLow)
[設定] DTR 設定.
bool IsRlsdSignalLow(void) const
[取得] RLSD 確認.
bool IsRlsdSignalOn(void) const
[取得] RLSD 確認.
virtual HANDLE CreatePort(LPCTSTR lpszPortName) const
[通知] ポート作成.
virtual void Purge(void)
[設定] パージ.
void GetParameter(CPort &_comPort, DWORD &_dwBaudRate, BYTE &_bByteSize, EParity &_eParity, EStopBits &_eStopBits) const
[取得] オープン設定取得.
virtual bool IsConnect(void) const
[確認] 接続確認.
bool SetRtsSignal(bool isLow)
[設定] RTS 設定.
static CWordVector EnumExistPortsByUsb(WORD vid, WORD pid)
[取得] USBシリアルPort一覧.
virtual void Close(void)
[処理] クローズ.
bool IsCtsSignalOn(void) const
[取得] CTS 確認.
virtual bool IsOpened(void) const
[確認] オープン確認.
virtual size_t Receive(size_t size, LPVOID _P)
[処理] 受信.
void SetCheckCtsMode(bool r=true)
[設定] CTSチェック設定.
static CWordVector EnumExistPorts(void)
[取得] 存在するPort一覧
virtual const TProperty & GetProperty(void) const
[取得] 性能取得
virtual size_t GetReceivingDataSize(void) const
[確認] 受信データ確認.
bool IsDsrSignalOn(void) const
[取得] DSR 確認.
virtual size_t Send(size_t size, LPCVOID P)
[処理] 送信.
bool IsEmpty(void) const
[確認] 空チェック
CStrT Left(size_t iSize) const
[作成] 範囲取得.
size_t GetLength(void) const
[取得] 文字列長
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
INT_PTR Find(TYP t, INDEX iFromIndex=0) const
[確認] 検索.
void Format(const TYP *lpszFormat,...)
[代入] 書式付き文字列代入.
CStrT Mid(INDEX iOffset, size_t iSize=INVALID_SIZE) const
[作成] 範囲取得.
int ToInt(INDEX iOffset=0, int iBase=10) const
[取得] 数値(int)へ変換
virtual size_t GetSize(void) const
[取得] サイズ取得
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
size_t GetSize(void) const
[取得] サイズ取得
const TYP * Ref(void) const
[取得] ポインタ取得
CStrVector EnumKeyNames(void) const
[取得] キー名一覧取得
CStr QueryString(LPCTSTR lpszKey, LPCTSTR lpszDefault=NULL) const
[取得] 文字列情報取得
bool HasKey(LPCTSTR lpszKey) const
[確認] 情報存在確認
void PreviousSubSection(void)
[設定] 対象セクション変更.
int Compare(LPCSTR P1, LPCSTR P2, INT_PTR len=-1, DWORD dwCmpFlags=0)
[比較] 文字列比較(ASCII/SJIS用)
DWORD ToDword(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
CAscii LowerString(LPCSTR lpsz)
[変換] 小文字変換(ASCII/SJIS用)
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
virtual INT_PTR Compare(const TDevice &t) const
[確認] 比較
CStr ToString(void) const
[取得] 文字列化.
bool Sort(const IComparatorT< TYP > &comparator, bool boIsReverse=false, bool boIsBubble=false)
[操作] ソート.
DWORD receiveTimeout
受信待ち時間。 INFINITE ならブロックされる。