uartreadwrite.c

来自「一个在Linux下开发的IP摄像头的驱动程序及控制软件」· C语言 代码 · 共 77 行

C
77
字号
#include "Utility.h"

/*!
***************************************************************************
 * \brief
 *	read/write through COM port

 * \param tServerInfo 
	[in] server info
 * \param *pszReadWriteBuf 
	[in/out] input when writing the com port, output when reading the com port
 * \param bRead 
	[in] true means read the com port
 * \param iComNumber 
	[in] index of com port
 
 * \return 
 * \retval S_OK		operation success
 * \retval -1		com port number is negative
 * \retval -2		server has no com port or iComNumber is larger than server's com port number
**************************************************************************/
int UartReadWrite(TSERVER_INFO tServerInfo, char *pszReadWriteBuf, BOOL bRead, int iComNumber)
{
	HANDLE hSrvUtl;
	HANDLE hDevice;
	char lpszModelName[64]; 
    int nTimeout = 0;                  //for more operations
	BOOL bFlushUart = FALSE;	   //for more operations
	SCODE scRet;
	TSrvDepResource_ServerInfo srvInfo;
	
	if (iComNumber < 0)
		return -1;

	scRet = ServerUtlInit(&hSrvUtl, &hDevice, lpszModelName, tServerInfo);
	if (scRet != 0)
		return scRet;

	scRet = SrvDepResource_GetParamForServer(lpszModelName, &srvInfo);
	if (scRet != S_OK)
	{
		ServerUtlRelease(&hSrvUtl, &hDevice);
		return scRet;		
	}

	if (srvInfo.dwComNum == 0 || srvInfo.dwComNum < iComNumber)
	{
		ServerUtlRelease(&hSrvUtl, &hDevice);
		return -2;
	}

	if (bRead)
	{
		scRet = ServerUtl_UartRead(hDevice, iComNumber, pszReadWriteBuf, 10, 
								   bFlushUart, nTimeout, FALSE);
		
		if (scRet != S_OK)
		{
			ServerUtlRelease(&hSrvUtl, &hDevice);
			return scRet;
		}
	}else
	{
		scRet = ServerUtl_UartWrite(hDevice, iComNumber, pszReadWriteBuf, FALSE);

		if (scRet != S_OK)
		{
			ServerUtlRelease(&hSrvUtl, &hDevice);
			return scRet;
		}
	}


	ServerUtlRelease(&hSrvUtl, &hDevice);

	return S_OK;
}

⌨️ 快捷键说明

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