//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: Comm485.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of Comm485.cpp //---------------------------------------------------------------------------- #include #include #include "..\lgs\comm485.h" #include "..\LgLib\version.h" const BYTE CComm485::c_acPort[] = {0x0C, 0x0B, 0x0C, 0x0B}; const BYTE CComm485::c_acMask[] = {0xEF, 0xF7, 0xEF, 0xF7}; CComm485::CComm485( PISRPROC pIsrProc ) : CIsr( pIsrProc ) { m_pBaseAddress = 0; m_nBase = 0; m_pOldIsrProc = NULL; #ifdef COMM_RS485_PREVENT_ECHO m_bWrite = FALSE; #endif m_nBaudRate = 0; m_nBitCount = 0; m_eParity = _NOPARITY; #ifdef DEBUG_SHOW_COMMUNICATION m_bShowCommunication = FALSE; m_nShowCommunicationWrite = 0; m_nShowCommunicationRead = 0; m_bShowCommunicationWritten = FALSE; #endif } CComm485::~CComm485() { Destroy(); } BOOL CComm485::SetParameters( LONG nBaudRate, INT nBitCount, PARITY eParity ) { if( !m_pBaseAddress ) return FALSE; if( m_nBaudRate == nBaudRate && m_nBitCount == nBitCount && m_eParity == eParity ) return TRUE; // finish transmition!!! Flush(); // prepare configuration int config; switch( nBitCount ) { case 5: config = LCR_SERIALDB5; break; case 6: config = LCR_SERIALDB6; break; case 7: config = LCR_SERIALDB7; break; case 8: config = LCR_SERIALDB8; break; default: return FALSE; } // add parity config |= (BYTE)eParity; // Setup baud rate; outportb( m_pBaseAddress + LCR_REGISTER, LCR_DLAB ); // remember baudrate for future use m_nBaudRate = nBaudRate; m_nBitCount = nBitCount; m_eParity = eParity; // initialize baudrate generator outportb( m_pBaseAddress + DLL_REGISTER, static_cast (115200L / nBaudRate) ); outportb( m_pBaseAddress + DLM_REGISTER, 0 ); // Setup configuration outportb( m_pBaseAddress + LCR_REGISTER, config ); return TRUE; } BOOL CComm485::Create( PORTS nPort, LONG nBaudRate, INT nBitCount, PARITY eParity ) { Destroy(); if( !m_InputStream.Create() ) return FALSE; if( !m_OutputStream.Create() ) return FALSE; m_nBase = nPort; m_pBaseAddress = *((short far*)MK_FP(0x0040, 2 * m_nBase )); if(!m_pBaseAddress) { printf( "Comm %d is inaccessable\n", nPort ); return FALSE; } /* int config; switch( nBitCount ) { case 5: config = LCR_SERIALDB5; break; case 6: config = LCR_SERIALDB6; break; case 7: config = LCR_SERIALDB7; break; case 8: config = LCR_SERIALDB8; break; default: return FALSE; } // Setup baud rate; outportb( m_pBaseAddress + LCR_REGISTER, LCR_DLAB ); // remember baudrate for future use m_nBaudRate = nBaudRate; outportb(m_pBaseAddress + DLL_REGISTER, static_cast (115200L / nBaudRate)); outportb( m_pBaseAddress + DLM_REGISTER, 0 ); // Setup configuration outportb( m_pBaseAddress + LCR_REGISTER, config ); */ if( !SetParameters( nBaudRate, nBitCount, eParity ) ) return FALSE; // Use FIFO outportb(m_pBaseAddress + FCR_REGISTER, FCR_ITL14 | FCR_RESETTF | FCR_RESETRF | FCR_FIFO_ENABLE ); #ifndef COMM_WITHOUT_INTERRUPTS // First thing is to store the old interrupt m_pOldIsrProc = _dos_getvect( c_acPort[m_nBase] ); _dos_setvect( c_acPort[m_nBase], m_pIsrProc ); // Set OUT2 bit to enable interrupts outportb(m_pBaseAddress + MCR_REGISTER, MCR_IE); // Enable the interrupts we want outportb(m_pBaseAddress + IER_REGISTER, IER_EDSSI | IER_ELSI | IER_ERBFI ); // Last thing to do is unmask the int in the ICU outportb(IMR_REGISTER, c_acMask[m_nBase] & inp(IMR_REGISTER)); #else // Disable all interrupts outportb(m_pBaseAddress + IER_REGISTER, 0 ); #endif SetRTS( FALSE ); // clear RTS // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER ); // if( MCR_Status & MCR_RTSC ) // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC ); return TRUE; } void CComm485::Flush() { #ifdef COMM_WITHOUT_INTERRUPTS while( !Invalidate() ); #else // output remaining bytes while( m_OutputStream.GetCount() ); // wait until output shift register become empty while( !(inportb(m_pBaseAddress + LSR_REGISTER) & LSR_TEMPT) ); #endif } void CComm485::Destroy() { if( m_pBaseAddress ) { // finish transmition!!! Flush(); // Disable FIFO outportb(m_pBaseAddress + FCR_REGISTER, 0); #ifndef COMM_WITHOUT_INTERRUPTS // Disable interrupt outportb(m_pBaseAddress + MCR_REGISTER, 0); outportb(m_pBaseAddress + IER_REGISTER, 0); // disable in interrupt controller outportb(IMR_REGISTER, ~c_acMask[m_nBase] | inp(IMR_REGISTER)); // return old vector _dos_setvect(c_acPort[m_nBase], m_pOldIsrProc ); #endif m_pBaseAddress = 0; #ifdef COMM_RS485_PREVENT_ECHO m_bWrite = FALSE; #endif m_nBase = 0; } m_nBaudRate = 0; m_nBitCount = 0; m_eParity = _NOPARITY; } void CComm485::OnISR() { // First thing to do in interrupt routines is tell // the ICU about the service being done. This avoids lock-up outportb( ICR_REGISTER, END_OF_INTERRUPT ); #ifndef COMM_WITHOUT_INTERRUPTS while( TRUE ) { BYTE byte; int LSR_Status; int IIR_Status = inportb( m_pBaseAddress + IIR_REGISTER ); if( IIR_Status & IIR_PENDING ) break; IIR_Status &= IIR_INTERRUPT_MASK; // Equal 0x0F switch( IIR_Status ) { case IIR_BREAK_OR_ERROR: // In order of priority // It's just important to read the LSR_REGISTER LSR_Status = inportb(m_pBaseAddress + LSR_REGISTER); if( LSR_Status & LSR_ERROR_MASK ) { // read error byte from receiver buffer inportb(m_pBaseAddress + RBR_REGISTER); } break; case IIR_RECEIVER_READY: case IIR_TIMEOUT: while( TRUE ) { // read byte from receiver buffer byte = inportb(m_pBaseAddress + RBR_REGISTER); #ifdef COMM_RS485_PREVENT_ECHO // check for echo if( !m_bWrite ) #endif m_InputStream.Put( byte ); // Check when receiver data ready LSR_Status = inportb(m_pBaseAddress + LSR_REGISTER); if(!(LSR_Status & LSR_DR)) break; } break; case IIR_TRANSMITER_EMPTY: // if there are not bytes for transmit if( !m_OutputStream.GetCount() ) { #ifdef COMM_RS485_PREVENT_ECHO // check for echo m_bWrite = FALSE; #endif SetRTS( FALSE ); // clear RTS // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER ); // if( MCR_Status & MCR_RTSC ) // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC ); // enable receiver and disable transmitter outportb(m_pBaseAddress + IER_REGISTER, IER_EDSSI | IER_ELSI | IER_ERBFI ); break; } #ifdef COMM_RS485_PREVENT_ECHO // check for echo m_bWrite = TRUE; #endif // while there are bytes in output stream while(1) { // check for transmitter buffer overflow LSR_Status = inportb(m_pBaseAddress + LSR_REGISTER); if(!(LSR_Status & LSR_THRE)) break; // get new byte for transmit if( !m_OutputStream.Get( &byte ) ) break; // write byte into FIFO outportb(m_pBaseAddress + THR_REGISTER, byte ); } break; case IIR_MODEM_CHANGE_STATUS: // Must read the MSR_REGISTER or will lock up the system! inportb(m_pBaseAddress + MSR_REGISTER); break; } } #endif // COMM_WITHOUT_INTERRUPTS } #ifdef COMM_WITHOUT_INTERRUPTS BOOL CComm485::Invalidate() { BYTE byte; if( !m_pBaseAddress ) return TRUE; // check errors // while( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_ERROR_MASK ) while( inportb(m_pBaseAddress + LSR_REGISTER) & ( LSR_FIFOE | LSR_FE | LSR_PE | LSR_OE ) ) { // read error byte from receiver buffer inportb(m_pBaseAddress + RBR_REGISTER); } // check receiver while( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_DR ) { // read byte from receiver buffer byte = inportb(m_pBaseAddress + RBR_REGISTER); #ifdef COMM_RS485_PREVENT_ECHO // check for echo if( !m_bWrite ) #endif m_InputStream.Put( byte ); } // if there are not bytes for transmit if( m_OutputStream.GetCount() ) { // set RTS SetRTS( TRUE ); // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER ); // if( !(MCR_Status & MCR_RTSC) ) // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_RTSC ); #ifdef COMM_RS485_PREVENT_ECHO // check for echo m_bWrite = FALSE; #endif // if FIFO empty if( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_THRE ) { // while there are bytes in output stream for( INT i = 0; i < 16; i++ ) { // get new byte for transmit if( !m_OutputStream.Get( &byte ) ) break; // write byte into FIFO outportb(m_pBaseAddress + THR_REGISTER, byte ); } } } // check for transmit end if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_THRE ) ) return FALSE; if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_TEMPT ) ) return FALSE; SetRTS( FALSE ); // clear RTS // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER ); // if( MCR_Status & MCR_RTSC ) // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC ); #ifdef COMM_RS485_PREVENT_ECHO // check for echo m_bWrite = FALSE; #endif return TRUE; } #endif DWORD CComm485::Write( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout ) { // unused parameter dwTimeout; INT nWritten = 0; if ( dwSize > 0 ) { // write bytes to transmit stream // Sending 0xFF after Data pBuffer[dwSize]=0xFF; dwSize++; // Remove previews FF from queue m_OutputStream.RemoveLast(); // write bytes to transmit stream nWritten = m_OutputStream.Put( pBuffer, dwSize ); nWritten -- ; // decrease because +FF }; #ifdef DEBUG_SHOW_COMMUNICATION if( m_bShowCommunication ) { printf( "\nW%d: ", m_nShowCommunicationWrite++ ); for( DWORD i = 0; i < nWritten; i++ ) printf( "%02X", (BYTE)pBuffer[i] ); m_bShowCommunicationWritten = TRUE; } #endif #ifndef COMM_WITHOUT_INTERRUPTS SetRTS( TRUE ); // set RTS // BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER ); // if( !(MCR_Status & MCR_RTSC) ) // outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_RTSC ); // enable transmitter and disable receiver outportb(m_pBaseAddress + IER_REGISTER, IER_EDSSI | IER_ELSI | IER_ETBEI | IER_ERBFI ); #endif return nWritten; } DWORD CComm485::Read( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout ) { // unused parameter dwTimeout; DWORD dwRead = m_InputStream.Get( pBuffer, dwSize ); #ifdef DEBUG_SHOW_COMMUNICATION if( m_bShowCommunication ) { if( dwRead && m_bShowCommunicationWritten ) { printf( "\nR%d: ", m_nShowCommunicationRead++ ); m_bShowCommunicationWritten = FALSE; } for( DWORD i = 0; i < dwRead; i++ ) printf( "%02X", (BYTE)pBuffer[i] ); } #endif // read bytes from receive stream return dwRead; } BOOL CComm485::SetRTS( BOOL bOn ) { // set/clear RTS BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER ); if( bOn ) { if( !(MCR_Status & MCR_RTSC) ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_RTSC ); } else { if( MCR_Status & MCR_RTSC ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC ); } } BOOL CComm485::SetDTR( BOOL bOn ) { // set/clear RTS BYTE MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER ); if( bOn ) { if( !(MCR_Status & MCR_DTRC) ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_DTRC ); } else { if( MCR_Status & MCR_DTRC ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_DTRC ); } }