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

📄 serialbase.c

📁 这是实际项目中的一个串口通信程序. Makefile通过开关, 可使此程序适用于 Linux 和嵌入式 ARM Linux. 代码注释较多. 是学习UART或Serail Port 通信的一个好例子.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
	if(nAllBytesRead == 2)//check the package head.
	{
		if(szRevBuf[0] != 0xFF || szRevBuf[1] != 0xFF)
		{
			szRevBuf[0] = szRevBuf[1];
			nAllBytesRead = 1;
		}
	}
	else*/
    if(nAllBytesRead == m_nPackageHeadLen)// modify the data length we should read.
	{
		bNewMessage = 0;
		
		int i =0;
		for( i = 0; i < m_nPackageHeadLen -1; i++ )
		{
			if(szRevBuf[i] == 0xFF && szRevBuf[i+1] == 0xFF)
			{
				break;
			}
	    }
	
		if(i == 0)//The first two characters mark the head flag.	 
		{
			unsigned char dataLen = szRevBuf[2+1]; // 2-> head flag, 1 -> command. 
			nAllBytesToRead = m_nPackageHeadLen + dataLen + m_nPackageTailLen ;  // Now, the data and tail need to be read.
		}	
		else if(i == 4) //Failed to find the head flag in the first five character.
		{
			if(szRevBuf[4] == 0xFF)
			{
				szRevBuf[0] = 0xFF;
				bNewMessage = 0; 
				nAllBytesRead = 1;//desert the first 4 characters received from UART.
			}
			else
			{		
				bNewMessage = 1; //desert the 5 characters received from UART.
				nAllBytesRead = 0;//desert the 5 characters received from UART.
			}
		}
		else if(i > 0)
		{
			int j=0;
			for(j =0; j < m_nPackageHeadLen -i; j++)
			{
				szRevBuf[j] = szRevBuf[j+i];
			}	
			nAllBytesRead = m_nPackageHeadLen -i;
		}			
	}
	else if(nAllBytesRead == nAllBytesToRead)// && nAllBytesRead > m_nPackageHeadLen) //A whole package is received.
	{
		bNewMessage = 1;

		//Step 1: To check the package head and package tail.
		int  bPackageCheck = 1; //boolean value, 1 for true, and o for false.
		
		if(szRevBuf[0] != 0xFF || szRevBuf[1] != 0xFF
			|| szRevBuf[nAllBytesRead-2] != 0xFE|| szRevBuf[nAllBytesRead-1] != 0xFE)
		{
			bPackageCheck = 0;
		}

		//Step 2: Sum Check
		//unsigned short wCheckSum = 0;
		unsigned char szCheckSum = 0;
		int i =0;
		for(i = 0; i < nAllBytesRead - 2  -2 -2 ; i++)  // head flag =2, the SC =2; Tail flag =2
		{
			szCheckSum +=  szRevBuf[2 + i];
		}

		//	unsigned short *pwSumCheckInPackage = (unsigned short *)(szMessage +dwMegLen -2 -1);
			 
		if (szCheckSum !=  szRevBuf[nAllBytesRead -2 -2]  ) //An error in sum Check
		{
			bPackageCheck= 0;
		}

		//Step 3 : To write the Package into queue
		if(bPackageCheck)
		{
			//add comments later. Rock Li
			unsigned char* szCoreData =  szRevBuf + 2; //skip the head flag 
			unsigned char nCoreDataLen =  szRevBuf[2+1] +2 +1; //2 -> data length, 1-> command

			WriteOneMsgIntoQue(szCoreData, nCoreDataLen);
		}
	}
	else //To continue to receive data until a whole package is received.
	{
		bNewMessage = 0;
	}

	return 0;
}

//this is a sub thread.
//pParam is equal to NUll; it is unuseful.
void* CommWatchPorc(void* pParam)
{
	while(m_bComDevOpen)
	{
//check if there is some buffer to read.
	   fd_set hUartSet;
	   struct timeval timeout;
	   
	   FD_ZERO(&hUartSet);
	   FD_SET(m_hComDev, &hUartSet);
	
	    int max_fd = m_hComDev + 1;
	    
		//polling one time / 100 seconds
	    timeout.tv_sec = 100; //any time is OK. maybe [10,100] is sensible. 
	    timeout.tv_usec = 0;
	
	    int retVal = select(max_fd, &hUartSet, NULL, NULL, &timeout);
	    if(retVal  == -1)  //UART error
	    {
	        perror("select failed.\n");
	        //return 0;
	    }
	    else if(retVal == 0) //Time out
	    {
			;//continue to fetch the data.
		 #if DEBUG	
		  	perror("retVal == 0. Tome out.");
		 #endif  
	    }
	    else //There is some data.
	    {
	    	//Read Uart Data 
	    	#if DEBUG	
				perror("There is some data from UART.");
			#endif
			
	    	ReadComm();
	    }    
	}

	pthread_exit(NULL);    

//Now, We will read data from the Serail Port
//
/*
    int nAvailableToRead = 0;
    int nRead =0;
    unsigned char  szRcvBuf[256] = {0};

    ioctl(m_hComDev, FIONREAD, &nAvailableToRead);

    if(nAvailableToRead >0)
    {
        nRead = read(m_hComDev, szRcvBuf, nAvailableToRead);

	if(nRead != nAvailableToRead)
	{
	    //There is some error.
	}
    }

    close(m_hComDev);
    
	}
*/
	return 0;
}


//This function is to get a whole message from Ack Message that is sent by App CPU as an answer.
//Parameters: 
//msg : the buffer to store one message. 
int GetAckMessage(pMsgQue* msg, unsigned int nTimeout)
{
	return ReadOneMsg(msg, &m_AckMsgQue, 1000);
	
//The following code does not work. the condition variable makes me confused.	
//	sem_t mysemp;
//	struct timespec ts;
//	int sts, val;
/*
	ts.tv_sec=time(NULL)+ 100;   // VIP, wai for 10 seconds.
    	ts.tv_nsec=0;
    
	int rc = 0; //result of wait condition
	
	if(pthread_mutex_lock(&m_hDataMutex) == 0)
	{
		while( (rc =pthread_cond_timedwait(&m_hDataCond,&m_hDataMutex,&ts)) && rc ==EINTR); 
		pthread_mutex_unlock(&m_hDataMutex);		
	}
        else
	{
		msg = NULL;
		return -1;
	}
	
	
	
	if(rc ==0)
	{	
		//if there is some error, msg shoule be NULL.
		perror("rc ==0 \n");
		ReadOneMsg(msg, &m_AckMsgQue, 1000);
	}
	else if (rc == ETIMEDOUT)
	{
		perror("timeout \n");
		msg = NULL;
	}	
	else //some error in timedwait
	{
		perror("other Error!\n");
		msg = NULL;
	}
	
	if(msg == NULL)
	{
		return -1;
	}
*/

//	return 0;					
}

//int GetAckMessage(pMsgQue* msg);
//int GetRxMessage(pMsgQue msg);

//This function is to get a whole message from Ack Message that is sent by App CPU as an answer.
//Parameters: 
//msg : the buffer to store one message. 
int GetRxMessage(pMsgQue* msg)
{
	return ReadOneMsg(msg, &m_RxMsgQue, 0);
}


//This is a test of serial communication. It works. 
/*
int main()
{
    unsigned char szBuf[]={0xFF, 0xFF, 0xEF, 0x06, 0x00,\
                           0x10, 0x11, 0x12, 0x13, 0x14, \
			                     0x15, 0x64, 0x00, 0xFE, 0xFE };

    int Temp  =100;

    Init("aaa", "/dev/ttyS0"); 
    
    int nAllLeft = sizeof(szBuf);

    int nAllWritten = 0;

    while(nAllLeft > 0)
    {
        int nWritten = write(m_hComDev,szBuf + nAllWritten, nAllLeft);
				if(nWritten >0)
				{
            nAllLeft -= nWritten;
	    			nAllWritten += nWritten;
            printf("write well");
				}
				else
				{
	   					printf("write error");
	   					//there is some error when put data into the UART
	   					return 1;
				}
    }

    
//    return 0;   

//check if there is some buffer to read.
   fd_set input;
   struct timeval timeout;
   
   FD_ZERO(&input);
   FD_SET(m_hComDev, &input);

    int max_fd = m_hComDev + 1;

    timeout.tv_sec = 1000;
    timeout.tv_usec = 0;

    int n = select(max_fd, &input, NULL, NULL, &timeout);
    if(n < 0)
    {
        perror("select failed.");
        return 0;
    }
    else if (n == 0)
    {
        perror("time out");
        return; 
    }

    

//Now, We will read data from the Serail Port
//
    int nAvailableToRead = 0;
    int nRead =0;
    unsigned char  szRcvBuf[256] = {0};

    ioctl(m_hComDev, FIONREAD, &nAvailableToRead);

    if(nAvailableToRead >0)
    {
        nRead = read(m_hComDev, szRcvBuf, nAvailableToRead);

	if(nRead != nAvailableToRead)
	{
	    //There is some error.
	}
    }

    close(m_hComDev);

//    ret = write(m_hComDev,
     
}
*/


#ifdef __cplusplus
}
#endif

⌨️ 快捷键说明

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