⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dsiolib1.cpp

📁 lpt to i2c converter source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			outlen--;
			TempByte = outbuf[i++];

			// reset the write event 
			ResetEvent(osWrite[portnum].hEvent);

			// write the byte
			fWriteStat = WriteFile(ComID[portnum], (LPSTR) &TempByte, 
                1, &dwBytesWritten, &osWrite[portnum] );

			// check for an error
			if (!fWriteStat)
				ler = GetLastError();

			// if not done writing then wait 
			if (!fWriteStat && ler == ERROR_IO_PENDING)
			{                                         
				WaitForSingleObject(osWrite[portnum].hEvent,to);

				// verify all is written correctly
				fWriteStat = GetOverlappedResult(ComID[portnum], &osWrite[portnum], 
                   &dwBytesWritten, FALSE); 
			} 

			// check results of write
			if (!fWriteStat || (dwBytesWritten != 1))
				return 0;
			//else
			//	return 1;

			msDelay(3);	// wait for 5 ms

			if (outlen == 0)
				return 1;
		} // end of while
   }
*/
	return 0;
}

//--------------------------------------------------------------------------
// Read an array of bytes to the COM port, verify that it was
// sent out.  Assume that baud rate has been set.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//               OpenCOM to indicate the port number.
// 'inlen'     - number of bytes to read from COM port
// 'inbuf'     - pointer to a buffer to hold the incomming bytes
//
// Returns: number of characters read
//
int ReadCOM(int portnum, int inlen, UCHAR *inbuf)
{
   DWORD dwLength=0; 
   BOOL fReadStat;
   DWORD ler=0,to;

   // calculate a timeout
   to = 20 * inlen + 60;

   // reset the read event 
   ResetEvent(osRead[portnum].hEvent);

   // read
   fReadStat = ReadFile(ComID[portnum], (LPSTR) &inbuf[0],
                      inlen, &dwLength, &osRead[portnum]) ;
   
   // check for an error
   if (!fReadStat)
      ler = GetLastError();

   // if not done writing then wait 
   if (!fReadStat && ler == ERROR_IO_PENDING)
   {
      // wait until everything is read
      WaitForSingleObject(osRead[portnum].hEvent,to);

      // verify all is read correctly
      fReadStat = GetOverlappedResult(ComID[portnum], &osRead[portnum], 
                   &dwLength, FALSE); 
   }

   // check results
   if (fReadStat)
      return dwLength;
   else
      return 0;
}

//--------------------------------------------------------------------------
// Send a break on the com port for at least 2 ms
// 
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number was provided to
//              OpenCOM to indicate the port number.
//
void BreakCOM(int portnum)
{
   // start the reset pulse
   SetCommBreak(ComID[portnum]);

   // sleep 
   Sleep(2); 
   
   // clear the break
   ClearCommBreak(ComID[portnum]);
}

//--------------------------------------------------------------------------
// Set the baud rate on the com port.  
//
// 'portnum'   - number 0 to MAX_PORTNUM-1.  This number was provided to
//               OpenCOM to indicate the port number.
// 'new_baud'  - new baud rate defined as
//                PARMSET_9600     0x00
//                PARMSET_19200    0x02
//                PARMSET_57600    0x04
//                PARMSET_115200   0x06
// 
void SetBaudCOM(int portnum, UCHAR new_baud)
{
   DCB dcb;

   // get the current com port state
   GetCommState(ComID[portnum], &dcb);

   // change just the baud rate
   switch (new_baud)
   {
      case PARMSET_115200:
         dcb.BaudRate = CBR_115200;
         break;
      case PARMSET_57600:
         dcb.BaudRate = CBR_57600;
         break;
      case PARMSET_19200:
         dcb.BaudRate = CBR_19200;
         break;
      case PARMSET_9600:
      default:
         dcb.BaudRate = CBR_9600;
         break;
   }
   
   // restore to set the new baud rate
   SetCommState(ComID[portnum], &dcb);
}

//--------------------------------------------------------------------------
//  Description:
//     Delay for at least 'len' ms
// 
//void msDelay(int len)
//{
//   Sleep(len);
//}

//--------------------------------------------------------------------------
// Get the current millisecond tick count.  Does not have to represent
// an actual time, it just needs to be an incrementing timer.
//
//long msGettick(void)
//{
//   return GetTickCount();
//}




//***************************************************************************
//***************************************************************************
//***************************************************************************
//---------------------------------------------------------------------------
//
//  owSesU.C - Acquire and release a Session on the 1-Wire Net.
//
//  Version: 2.00
//
//  History: 1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for 
//                         multiple ports.  

//#include <stdio.h>
//#include "ownet.h"

// external function prototypes
int  OpenCOM(int,char *);
void CloseCOM(int);
//extern int  DS2480Detect(int);

// local function prototypes
extern bool  AcquireCOMPort(int,char *,char *);
extern void ReleaseCOMPort(int,char *);

// keep port name for later message when closing
char portname[MAX_PORTNUM][128];

//---------------------------------------------------------------------------
// Attempt to acquire a 1-Wire net using a com port and a DS2480 based
// adapter.  
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
//                OpenCOM to indicate the port number.
// 'port_zstr'  - zero terminated port name.  For this platform
//                use format COMX where X is the port number.
// 'return_msg' - zero terminated return message. 
//
// Returns: TRUE - success, COM port opened
//
bool AcquireCOMPort(int portnum, char *port_zstr, char *return_msg)
{
   int cnt=0;
   portname[portnum][0] = 0;

   // attempt to open the communications port
   if (OpenCOM(portnum,port_zstr))
      cnt += sprintf(&return_msg[cnt],"%s opened\n",port_zstr);
   else
   {
      cnt += sprintf(&return_msg[cnt],"Could not open port %s,"
              " aborting.\nClosing port %s.\n",port_zstr,port_zstr);
      return FALSE;
   }

   // detect DS2480
/*   if (DS2480Detect(portnum))
      cnt += sprintf(&return_msg[cnt],"DS2480-based adapter detected\n");
   else
   {
      cnt += sprintf(&return_msg[cnt],"DS2480-based adapter not detected, aborting program\n");
      cnt += sprintf(&return_msg[cnt],"Closing port %s.\n",port_zstr);
      CloseCOM(portnum);
      return FALSE;
   }      
*/
   // success
   sprintf(portname[portnum],"%s",port_zstr);
   return TRUE;
}

//---------------------------------------------------------------------------
// Release the previously acquired a 1-Wire net.
//
// 'portnum'    - number 0 to MAX_PORTNUM-1.  This number was provided to
//                OpenCOM to indicate the port number.
// 'return_msg' - zero terminated return message. 
//
void ReleaseCOMPort(int portnum, char *return_msg)
{
   // close the communications port
   sprintf(return_msg,"Closing port %s.\n",portname[portnum]);
   CloseCOM(portnum);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -