type<br> TCOMMINFO = Record<br><br> hComHandle : Thandle; // Comm handle<br> {m_hSuperWnd : Thandle; // Handle of Windows<br> nMessageID : Cardinal; // Message ID}<br> hRevEvent : TEvent; // Event<br> nComPort : longword; // COM1 COM2...<br> dwBaudRate : dword;<br> dwEvtMask : dword; // Event mask<br> byParity : Byte; // Parity<br> byDataSize : Byte; // Data size<br> byStopSize : Byte; // Stop size<br> byFlowControl : Byte; // Flow control 1 : none<br> // 2 : Hardware<br> // 3 : Software<br> dwInBufferSize : dword;<br> dwOutBufferSize : dword;<br> ReadOv, WriteOv : TOverLapped;<br> end;<br><br>function TComm.OpenCom({m_hWnd : THandle;nMessage : Cardinal}) : Cardinal;<br>var<br> bSuccessCode : Boolean;<br> dcb : TDCB;<br> CommTimeOuts : TCOMMTIMEOUTS;<br> CommProp : TCommProp;<br> cpPort : string;<br>begin<br> if bIsComOpen then begin result := 2; exit; end; //Alerady Open Com<br><br> /////////////////////////////////////////////<br> if (CommInfo.nComPort = 0) or (CommInfo.nComPort > $10) then<br> begin result := 0; exit; end;<br><br> cpPort:=format('COM%d',[CommInfo.nComPort]);<br> CommInfo.hComHandle:= CreateFile(pChar(cpPort),<br> GENERIC_READ+GENERIC_WRITE,<br> 0,<br> nil,<br> OPEN_EXISTING,<br> FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED,<br> 0);<br> if CommInfo.hComHandle =INVALID_HANDLE_VALUE then begin result := 0; exit; end;<br><br> bSuccessCode:=SetCommMask(CommInfo.hComHandle,CommInfo.dwEvtMask);<br> if not bSuccessCode then<br> begin CloseHandle(CommInfo.hComHandle); result := 0; exit; end;<br><br> bSuccessCode:=SetupComm(CommInfo.hComHandle,CommInfo.dwInBufferSize,CommInfo.dwOutBufferSize);<br> if not bSuccessCode then<br> begin CloseHandle(CommInfo.hComHandle); result := 0; exit; end;<br><br> bSuccessCode:=PurgeComm(CommInfo.hComHandle,<br> PURGE_TXABORT or PURGE_TXCLEAR or PURGE_RXABORT or PURGE_RXCLEAR);<br> if not bSuccessCode then<br> begin CloseHandle(CommInfo.hComHandle); result := 0; exit; end;<br><br> bSuccessCode:=GetCommTimeouts(CommInfo.hComHandle,CommTimeOuts);<br> if not bSuccessCode then<br> begin CloseHandle(CommInfo.hComHandle); result := 0; exit; end;<br><br> CommTimeOuts.ReadIntervalTimeout:=$ffffffff;<br> CommTimeOuts.ReadTotalTimeoutMultiplier:=0;<br> CommTimeOuts.ReadTotalTimeoutConstant:=1000;<br> CommTimeOuts.WriteTotalTimeoutMultiplier:=50;<br> CommTimeOuts.WriteTotalTimeoutConstant:=0;<br> CommTimeOuts.WriteTotalTimeoutConstant := 2000;<br><br> bSuccessCode:=SetCommTimeouts(CommInfo.hComHandle,CommTimeOuts);<br> if not bSuccessCode then<br> begin CloseHandle(CommInfo.hComHandle); result := 0; exit; end;<br><br> CommProp.wPacketLength:=sizeof(COMMPROP);<br><br> GetCommProperties(CommInfo.hComHandle,CommProp);<br> if ((CommProp.dwSettableBaud and CommInfo.dwBaudRate) <> 0) and<br> ((CommInfo.byParity = 0)or<br> ((CommProp.wSettableStopParity and CommInfo.byParity) <> 0)) then<br> begin<br> FillChar(dcb, sizeof(dcb), 0);<br> dcb.DCBlength := sizeof(dcb);<br><br> bSuccessCode:=GetCommState(CommInfo.hComHandle,dcb);<br> if not bSuccessCode then<br> begin CloseHandle(CommInfo.hComHandle); result := 0; exit; end;<br><br> dcb.BaudRate:=(CommInfo).dwBaudRate;<br> dcb.Flags := $00000003;<br> dcb.ByteSize := 8;<br> dcb.Parity := NOPARITY;<br> dcb.StopBits := OneStopBit;<br> dcb.XonLim := 50;<br> dcb.XoffLim := 50;<br> dcb.XonChar := char($11); // XON<br> dcb.XoffChar := char($13); // XOFF}<br><br> bSuccessCode:=SetCommState((CommInfo).hComHandle,dcb);<br> if not bSuccessCode then<br> begin CloseHandle(CommInfo.hComHandle); result := 0; exit; end;<br> end;<br> bSuccessCode:=EscapeCommFunction(CommInfo.hComHandle,SETDTR);<br> if not bSuccessCode then<br> begin CloseHandle(CommInfo.hComHandle); result := 0; exit; end;<br> bSuccessCode:=EscapeCommFunction(CommInfo.hComHandle,SETRTS);<br> if not bSuccessCode then<br> begin CloseHandle(CommInfo.hComHandle); result := 0; exit; end;<br><br> bIsComOpen:=TRUE;<br><br> /////////////////////////////////////////////<br> CommInfo.hRevEvent:=TEvent.Create(nil,True,True,'');<br> if CommInfo.hRevEvent.Handle = THandle(nil) then<br> begin CloseCom(); result := 0; exit; end;<br><br> WatchCommThread := TWatchCommThread.Create(True);<br> WatchCommThread.Resume;<br><br> result := 1; //Success<br>end;<br>