CSampleInfo Class
The CSampleInfo Class includes a set of reader/writers to negotiate with HWAccess I/O access to the hardware ports. The class has all the logic required to perform calculations to the data retrieved, data formatting, buffer storage and the necessary error handling. The class encapsulates the low-level access functionality of the library.
// Header definition for the CSampleInfo Class
class CSampleInfo
{
// Construction
public:
CSampleInfo();
virtual ~CSampleInfo();
// Attributes private: // Buffer to store the data formatted CHAR m_achInfo[MAX_INFO_BUFFER]; UINT m_nBufferSize;
public: // Indicates if I/O functions should be used BOOL m_bIO; // Indicates if Memory functions should be used BOOL m_bMemory;
// Implementation
public:
VOID GetSampleInfo();
PBYTE GetBufferPtr();
UINT GetBufferSize();
// Operations public: unsigned __int8 ReadPort( unsigned __int8 btPort ); VOID WritePort( unsigned __int8 btPort, unsigned __int8 btData ); BOOL ReadMemory( PVOID pBuffer, DWORD dwOffset, DWORD dwSize, unsigned __int8 btType ); BOOL WriteMemory( PVOID pBuffer, DWORD dwOffset, DWORD dwSize, unsigned __int8 btType ); }; typedef CSampleInfo * PCSAMPLEINFO; // CSampleInfo Class Implementation CSampleInfo::CSampleInfo()
{
// Initialize information buffer
memset(m_achInfo, 0, MAX_INFO_BUFFER );
// Initialize Buffer Size to 0
m_nBufferSize = 0;
// Initialize I/O & Memory. Set to TRUE to process them
m_bIO = TRUE;
m_bMemory = TRUE;
}
CSampleInfo::~CSampleInfo()
{
}
// This is the low level function that retrieves
// Custom Hardware Information through the EXE file
// It uses the Read/Write Memory and I/O messages
// It also constructs the memory buffer that will eventually
// passed to the EXE file to display.
VOID CSampleInfo::GetSampleInfo()
{
m_nBufferSize = 0;
// If I/O is enabled
if( m_bIO )
{
BYTE btSeconds;
BYTE btMinutes;
BYTE btHours;
// Lets Read the time from CMOS // Read seconds from offset 0 WritePort( 0x70, 0); btSeconds = ReadPort( 0x71 ); // Read minuutes from offset 2 WritePort( 0x70, 2); btMinutes = ReadPort( 0x71 ); // Read hours from offset 4 WritePort( 0x70, 4); btHours = ReadPort( 0x71 ); // Format and store the time to our buffer wsprintf( &m_achInfo[m_nBufferSize], "\nCMOS Time is %.2X:%.2X:%.2X", btHours, btMinutes, btSeconds ); m_nBufferSize += strlen(&m_achInfo[m_nBufferSize]); }
// If memory is enabled if( m_bMemory ) { // Lets Read the first 2 Legacy Serial Ports mapped // in the BIOS data area. These are at offsets 0x400-0x403 // 1. Construct a Read Buffer unsigned __int16 awPorts[4]; // 2. Read Memory to buffer ReadMemory( awPorts, 0x400, 4, 1); // 3. Update the View Buffer wsprintf( &m_achInfo[m_nBufferSize], "\nSerial Port 1 is at 0x%.4X\nSerial Port 2 is at 0x%.4X", awPorts[0], awPorts[1] );
m_nBufferSize += strlen(&m_achInfo[m_nBufferSize]); }
// If both memory and I/O are disabled show we have an empty buffer if( !m_bIO && !m_bMemory ) { wsprintf( m_achInfo, "\nEmpty Buffer. \nSelect Memory and/or I/O from the Dialog Options" ); m_nBufferSize = strlen(m_achInfo); } } // End of GetSampleInfo
PBYTE CSampleInfo::GetBufferPtr()
{
return (PBYTE)m_achInfo;
}
// End of GetBufferPtr
UINT CSampleInfo::GetBufferSize()
{
return m_nBufferSize;
}
// End of GetBufferSize
//
// Reads 8bit data from a specified port unsigned __int8 CSampleInfo::ReadPort( unsigned __int8 btPort ) { SPORTACCESS stcIO = {0}; PSAMPLEAPP pApp = (PSAMPLEAPP)AfxGetApp(); stcIO.btType = 1; stcIO.dwPort = btPort; // Request information from main application pApp->DispatchDLLMsg( LM_PORT_READ, (LPARAM)&stcIO ); return (unsigned __int8)stcIO.dwData; } // End of ReadPort
// Writes 8bit data to a specified port VOID CSampleInfo::WritePort( unsigned __int8 btPort, unsigned __int8 btData ) { SPORTACCESS stcIO = {0}; PSAMPLEAPP pApp = (PSAMPLEAPP)AfxGetApp(); stcIO.btType = 1; stcIO.dwPort = btPort; stcIO.dwData = btData; // Request information from main application pApp->DispatchDLLMsg( LM_PORT_WRITE, (LPARAM)&stcIO ); } // End of WritePort
// Read Memory BOOL CSampleInfo::ReadMemory( PVOID pBuffer, DWORD dwOffset, DWORD dwSize, unsigned __int8 btType ) { SMEMORYACCESS stcMemory = {0}; PSAMPLEAPP pApp = (PSAMPLEAPP)AfxGetApp(); stcMemory.btType = btType; stcMemory.qwAddress = dwOffset; stcMemory.dwSize = dwSize; stcMemory.pBuffer = (unsigned __int8 *)pBuffer; // Request information from main application return pApp->DispatchDLLMsg( LM_MEMORY_READ, (LPARAM)&stcMemory ); } // End of ReadMemory
// Write Memory BOOL CSampleInfo::WriteMemory( PVOID pBuffer, DWORD dwOffset, DWORD dwSize, unsigned __int8 btType ) { SMEMORYACCESS stcMemory = {0}; PSAMPLEAPP pApp = (PSAMPLEAPP)AfxGetApp(); stcMemory.btType = btType; stcMemory.qwAddress = dwOffset; stcMemory.dwSize = dwSize; stcMemory.pBuffer = (unsigned __int8 *)pBuffer; // Request information from main application return pApp->DispatchDLLMsg( LM_MEMORY_WRITE, (LPARAM)&stcMemory ); } // End of WriteMemory
The main function of this class is the GetSampleInfo. It should be invoked from a secondary thread. The function calls the ReadPort and ReadMemory to acquire the H/W information. It then formats and stores this information to an internal buffer. Notice the access functions format data and setup the interface structures to communicate with HWAccess the DispatchDLLMessage is handled by the common library.

