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

📄 comcommand.cpp

📁 OMB COMMON CLASSES 源代码
💻 CPP
字号:
/*==============================================================================
                       OMB COMMON CLASSES IMPLEMENTATION

SERVICES:
  Provides Serial I/O Devices Communication functions.
   
DESCRIPTION:
  Implement Serial I/O read/write in (RAW|CR) mode. The class is: ComCommand.
  (1) Write data to Serial I/O by using WritePrompt function.
  (2) Read data from Serial I/O by using ReadCommand function.
  (3) Wait keyboard hit event by using WaitKbHit function.

INITIALIZATION AND SEQUENCING REQUIREMENTS:
  ComCommand::BindDev() must be called before other calls.

AUTHOR INFORMATION:
  Feng.Tie, tiefeng@mail.eastcomtd.com. Mobile: +86 10 13701013761.

Copyright (c) 2003 EASTCOMM Inc.  All Rights Reserved.
==============================================================================*/

//==============================================================================
//                    Includes and Variable Definitions
//==============================================================================

//==============================================================================
// Include Files 
//==============================================================================

#include <stdLib.h>
#include <inetLib.h>
#include <ioLib.h>
#include <tickLib.h>
#include "comcommand.h"

//------------------------------------------------------------------------------
// Constant / Defines
//------------------------------------------------------------------------------

/*==============================================================================
FUNCTION:   ComCommand::ComCommand

DESCRIPTION:
  Constructor of the class: ComCommand.
==============================================================================*/

ComCommand::ComCommand()
{
	mComHandle = ERROR ;
}

/*==============================================================================
FUNCTION:   ComCommand::~ComCommand

DESCRIPTION:
  Destructor of the class: ComCommand.
==============================================================================*/

ComCommand::~ComCommand()
{
	UnbindDev() ;
}

/*==============================================================================
FUNCTION:   ComCommand::BindDev

DESCRIPTION:
  Bind the class to a Serial I/O Device.
==============================================================================*/

STATUS ComCommand::BindDev(const char * devName)
{
	mComHandle = open(devName,O_RDWR,0) ;
	
	if ( ERROR == mComHandle )
	{
		return ERROR ;
	}
	return ioctl(mComHandle,FIOSETOPTIONS,OPT_RAW|OPT_CRMOD) ;
}

/*==============================================================================
FUNCTION:   ComCommand::UnbindDev

DESCRIPTION:
  Unbind the class from a Serial I/O Device.
==============================================================================*/

STATUS ComCommand::UnbindDev()
{
	if ( ERROR!=mComHandle )
	{
		close(mComHandle) ;
		mComHandle = ERROR ;
		return OK ;
	}
	else return ERROR ;
}

/*==============================================================================
FUNCTION:   ComCommand::WaitKbHit

DESCRIPTION:
  Waiting keyboard events in specified timeout(in seconds).
==============================================================================*/

STATUS ComCommand::WaitKbHit(const char * prompt,int timeout)
{
	static char recv_buffer[mMaxCommandChars] ;
	static char prompt_buffer[mMaxPromptChars] ;
	unsigned long start_time = tickGet() ;
	int bytes_read = 0 ;
	int show = timeout ;
	int showed = timeout ;
	char buf[16] ;

	WritePrompt(prompt) ; // Writing user prompts
	sprintf(buf,"\r\n%2d",timeout) ;
	WritePrompt(buf) ; // Writing time

	FOREVER // Checking timeout and write time to com port
	{
		show = timeout - (tickGet()-start_time)/60 ;
		if ( showed!=show )
		{
			sprintf(buf,"%2d",show) ;
			WritePrompt("\b\b") ;
			WritePrompt(buf) ;
			showed -= 1 ;
		}
		ioctl(mComHandle,FIONREAD,(int)&bytes_read) ;
		if ( bytes_read > 0 ) break ;
		if ( tickGet() - start_time > 60*timeout ) break ;
	}
	WritePrompt("\r\n") ; // Start a new line
	if ( 0 == bytes_read )
	{
		return ERROR ;
	}
	else // Receive the data and ignore it.
	{
		read(mComHandle, recv_buffer, 1) ;
		return OK ;
	}
}

/*==============================================================================
FUNCTION:   ComCommand::WritePrompt

DESCRIPTION:
  Write a prompt to specified Serial I/O Device.
==============================================================================*/

STATUS ComCommand::WritePrompt(const char * prompt)
{
	if ( strlen(prompt)>=mMaxPromptChars )
	{
		return ERROR ;
	}
	if ( ERROR == mComHandle )
	{
		return ERROR ;
	}
	write(mComHandle,(char *)prompt,strlen(prompt)) ;
	return OK ;
}

/*==============================================================================
FUNCTION:   ComCommand::ReadCommand

DESCRIPTION:
  Read a command from specified Serial I/O Device.
==============================================================================*/

STATUS ComCommand::ReadCommand(char * command,int len)
{
	int offset = 0 ;
	char buf[2] = { 0 } ;
	int echo_chars = 0 ; // Remember echo char counts
	int back_chars = 0 ; // Remember backspace char counts

	if ( len <= 0 ) return ERROR ;

	FOREVER // Receiving data from Serial I/O Device.
	{
		if ( offset == len ) return ERROR ;
		if ( offset >= mMaxCommandChars ) return ERROR ;
		int ret = read(mComHandle,command+offset,1) ;
		if ( ERROR == ret )
		{
			return ERROR ; // Can't read data
		}
		*buf = *(command+offset) ;
		switch ( *(buf) )
		{
		case '\n':
			WritePrompt("\r\n") ; // Write back the CR
			*(command + offset) = 0x00 ;
			return OK ;
		case '\b':
			if ( back_chars<echo_chars )
			{
				WritePrompt("\b \b") ;
				back_chars += 1 ;
				offset -= 2 ; // A '\b', a char
			}
			else offset -= 1 ; // Ignore the backspace
			break ;
		default:
			WritePrompt(buf) ; // Echo service
			echo_chars += 1 ;
		}
		offset += ret ;
	}
	
	return ERROR ; // Uselessness. Avoid warning.
}

// End of the file.

/*==============================================================================
                             Edit History for File
VERSION 1.0:
  2003-03-25    Initial version. (modified by tief)
==============================================================================*/

⌨️ 快捷键说明

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