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

📄 ext_transport232.c

📁 simulink real-time workshop for dragon12 development board from
💻 C
📖 第 1 页 / 共 4 页
字号:
						*nBytesGot = -1;
						
						return EXT_NO_ERROR;

					}
					else {

						mexPrintf ("ExtGetTargetData: timeout during reception of user telegram remainder.\n");
						return EXT_ERROR;

					}

				}
				else {

					mexPrintf ("ExtGetTargetData: unspecified error during reception of user telegram remainder (ReadFile).\n");
					return EXT_ERROR;

				}

			}
			/* ------------------------------------------------------------------------------------------- */



			/* DAT telegram (both calls) */

			/* reset TelType */
			userData->TelType = 0;

			return EXT_NO_ERROR;
			
		}
		else {
		
			/* nBytesGot != nBytesNeeded -> TIMEOUT occurred during reception of data */
			#if DEBUG_MSG_LVL >= 2
			mexPrintf ("ExtGetTargetData: timeout occurred during data reception (ReadFile).\n");
			mexPrintf ("ExtGetTargetData: Only received %d bytes instead of %d.\n", *nBytesGot, nBytesToGet);
			mexPrintf ("ExtGetTargetData: Discarding incomplete data package and requesting next data upload.\n");
			#endif

			/* clear COM errors */
			checkCOMforData(userData->hCom);

			/* all done -> reset TelType */
			userData->TelType = 0;

			/* signal zero bytes received... -> this forces 'ext_comm.c' to discard the data (as log data) */
			*nBytesGot = 0;

			return EXT_NO_ERROR;

		}
	
	}
	else {
	
		/* unspecified error during data reception */
		mexPrintf ("ExtGetTargetData: unspecified error during data reception (ReadFile).\n");
		return EXT_ERROR;
	
	}
	
} /* end ExtGetTargetData */




/* Function: ExtSetTargetMsg ===================================================
 * Abstract:
 *  Sets (sends) the specified number of bytes on the serial line.  As long as
 *  an error does not occur, this function is guaranteed to set the requested
 *  number of bytes.  The number of bytes set is returned via the 'nBytesSet'
 *  parameter.  EXT_NO_ERROR is returned on success, EXT_ERROR is returned on
 *  failure.
 *
 * NOTES:
 *  o it is o.k. for this function to block if no room is available 
 */
PUBLIC boolean_T ExtSetTargetMsg(
    const ExternalSim *ES,
    const int         nBytesToSet,
    const char        *src,
    int               *nBytesSet) /* out */
{

    UserData	*userData = (UserData *)esGetUserData(ES);
	int_T		lookforACK;

	/* re-order bytes (ms9S12)   --  fw-02-05 */
	//slCopyNBytes(src, src, *nBytesGot, TRUE, 4);
	int_T     i;
	char_T  dat;


	#if DEBUG_MSG_LVL >= 3
	mexPrintf("ExtSetTargetMsg: IN\n");
	#endif

    
	/* re-order bytes (ms9S12)   --  fw-02-05 */
	//slCopyNBytes(src, src, *nBytesGot, TRUE, 4);

	memcpy(tempSendMsgBuf, src, nBytesToSet);

	for(i=0; i<nBytesToSet; i +=4) {

        dat                 = tempSendMsgBuf[i+3];
		tempSendMsgBuf[i+3] = tempSendMsgBuf[i+0];
		tempSendMsgBuf[i+0] = dat;
        dat                 = tempSendMsgBuf[i+2];
		tempSendMsgBuf[i+2] = tempSendMsgBuf[i+1];
		tempSendMsgBuf[i+1] = dat;

		src = tempSendMsgBuf;

	}

	
	#if DEBUG_MSG_LVL >= 3
	mexPrintf("ExtSetTargetMsg: Sending... ");
	for(i=0; i< nBytesToSet; i++) 
		mexPrintf("%d  ", (uint8_T)tempSendMsgBuf[i]);
	mexPrintf("\n");
	#endif
	
	
	/* send the entire string 'in one go' -> no echo */
	if(WriteFile(userData->hCom, (LPSTR)src, nBytesToSet, nBytesSet, NULL)) {

		if(*nBytesSet == nBytesToSet) {

			#if DEBUG_MSG_LVL >= 3
			mexPrintf("ExtSetTargetMsg: data sent successfully (%d bytes)\n", *nBytesSet);
			#endif
		
			/* make sure everything's gone... */
			if(!FlushFileBuffers(userData->hCom)) {
				
				/* flushing of the output buffer returns unsuccessful... */
				mexPrintf ("ExtSetTargetMsg: Error whilst flushing output buffers (FlushFileBuffers).\n");
				{
					DWORD err = GetLastError();
					mexPrintf("Error code: %ld\n", err);
				}
				
				return EXT_ERROR;
			}
			
			/* acknowledgement section (begin) */
			else {
				
				/* successful transmission -> await acknowledgement from the target (1 byte) */
				int		nBytesGot = 0;
				int		TelLenInfoSize;
				char_T	ack_byte = 0;

				/* await acknowledgement byte */
				#if DEBUG_MSG_LVL >= 3
				mexPrintf("ExtSetTargetMsg: Awaiting target acknowledgement...\n");
				#endif

				
				/* repeat this section until the awaited acknowledgement has been received */
				lookforACK = 5;
				while(lookforACK--) {

					if(ReadFile(userData->hCom, (LPSTR)&ack_byte, 1, &nBytesGot, NULL)) {

						#if DEBUG_MSG_LVL >= 3
						mexPrintf("ExtSetTargetMsg: Waiting for target acknowledgement (received %d bytes).\n", nBytesGot);
						#endif

					} else {

						/* unspecified error during reception of the acknowledgement byte */
						mexPrintf ("ExtSetTargetMsg: Unspecified error during reception of the telegram acknowledgement byte (ReadFile).\n");
							
						return EXT_ERROR;

					}

					if(nBytesGot == 1) {

						/* successful receipt of acknowledgement byte */
						#if DEBUG_MSG_LVL >= 3
						mexPrintf("ExtSetTargetMsg: Received target acknowledgement (%d).\n", ack_byte);
						#endif

						/* acknowledgement has been received within the time set by 'default_timeout' (1 s) */
						switch(ack_byte) {
	
						case ACK_SUCCESS:
						
							/* target signals ACK_SUCCESS */
							#if DEBUG_MSG_LVL >= 3
							mexPrintf("ExtSetTargetMsg: Successful acknowledgement (%d).\n", (uint8_T)ack_byte);
							#endif

							/* exit from the while loop */
							lookforACK = 0;
							
							break; /* switch */

							
						case ACK_NO_SUCCESS:

							/* target signals ACK_NO_SUCCESS -> resend last package */
							#if DEBUG_MSG_LVL >= 3
							mexPrintf("ExtSetTargetMsg: Received ACK_NO_SUCCESS (%d) -> resending last message telegram.\n", (uint8_T)ack_byte);
							#endif

							/* re-send the entire message telegram */
							if(WriteFile(userData->hCom, (LPSTR)src, nBytesToSet, nBytesSet, NULL)) {

								/* WriteFile returned successfully (no timeouts, etc.) -> check # of bytes written */
								if(*nBytesSet == nBytesToSet) {

									/* correct number of bytes has been sent -> flush buffers (just in case) */
									if(!FlushFileBuffers(userData->hCom)) {
									
										mexPrintf ("ExtSetTargetMsg: Error whilst flushing output buffers (FlushFileBuffers).\n");
										{
											DWORD err = GetLastError();
											mexPrintf("Error code: %ld\n", err);
										}
				
										return EXT_ERROR;

									}
									else {

										/* buffers have been flushed successfully -> assuming that everything's fine now... */
										return EXT_NO_ERROR;

									}

								}
								else {

									/* resending of message unsuccessful : incorrect # of bytes written */
									mexPrintf ("ExtSetTargetMsg: Resending telegram... incorrect number of bytes sent (WriteFile).\n");
									{
										DWORD err = GetLastError();
										mexPrintf("Error code: %ld\n", err);
									}

									return EXT_ERROR;
								}

							} /* resending information (if) */
							else {

								/* resending of message unsuccessful : unspecified error (WriteFile) */
								mexPrintf ("ExtSetTargetMsg: Resending telegram... unspecified error (WriteFile).\n");
								{
									DWORD err = GetLastError();
									mexPrintf("Error code: %ld\n", err);
								}

								return EXT_ERROR;
								
							} /* resending information (if) */

						
						case MES:

							/* intercepted message telegram, retrieve message and store it in interceptMsgBuf */
							#if DEBUG_MSG_LVL >= 3
							mexPrintf("ExtSetTargetMsg: Received MES -> commencement of an incoming target message.\n");
							#endif

							/* register telegram type userData->MsgTelBuf */
							userData->TelType = MES;
	
							/* read 8 more bytes from COM */
							if(ReadFile(userData->hCom, (LPSTR)interceptMsgBuf, 8, &nBytesGot, NULL)) {
		
								if (nBytesGot != 8) {

									/* TIMEOUT occurred during reception of data */
									mexPrintf ("ExtSetTargetMsg: timeout occurred during reception of intercepted message (ReadFile).\n");

									return EXT_ERROR;
		
								}
								else {

									/* register successful reception of the entire message header */
									userData->MsgTelIntercepted = TRUE;

									#if DEBUG_MSG_LVL >= 2
									{
										int_T	i;

										for(i=0; i<nBytesGot; i++)
											mexPrintf("ExtSetTargetMsg: incoming message [%d/8]) %d\n", i+1, (uint_T)(interceptMsgBuf[i]));

									}
									#endif

									/* from here we exit the switch statement and loop back (while) */
								}

							}
							else {

								/* unspecified error during data reception */
								mexPrintf ("ExtSetTargetMsg: unspecified error during reception of intercepted message (ReadFile).\n");

								return EXT_ERROR;

							}

							break; /* (switch) */
						

						/* USR, DAT or 'unknown' telegram */
						default:

							switch((int_T)ack_byte) {

							case USR:

								/* received USR -> incoming user telegram */
								#if DEBUG_MSG_LVL >= 1
								mexPrintf("ExtSetTargetMsg: incoming user telegram detected.\n");
								#endif
						
								/* register telegram type */
								userData->TelType = USR;
							
								/* flag detection of incoming user telegram */
								userData->UsrTelIntercepted = TRUE;
	
								#if DEBUG_MSG_LVL >= 2
								mexPrintf("ExtSetTargetMsg: reading data length byte.\n");
								#endif
			
								/* fetch data length  --  1 more byte */
								nBytesGot = 0;
								TelLenInfoSize = 1;
								if(ReadFile(userData->hCom, (LPSTR)&ack_byte, TelLenInfoSize, &nBytesGot, NULL)) {
							
									if (nBytesGot != TelLenInfoSize) {
				
										/* nBytesGot != 1 -> TIMEOUT occurred during reception */
										mexPrintf ("ExtSetTargetMsg: Timeout occurred during reception of user telegram length information (ReadFile).\n");
										{
											DWORD err = GetLastError();
											mexPrintf("Error code: %ld\n", err);
										}

										return EXT_ERROR;
									}
									else {

										/* telegram length in 'ack_byte' */

										#if DEBUG_MSG_LVL >= 2
										mexPrintf ("ExtSetTargetMsg: USR telegram length: %d bytes\n", (uint_T)ack_byte);
										#endif

										/* store length byte in the buffer 'interceptDatBuf' */
										interceptDatBuf[0] = ack_byte;
										ack_byte -= nBytesGot;				/* use 'ack_byte' to store the number of byte still to get */

									}

								} /* reception of data length byte  --  ReadFile (if) */
								else {

									/* unspecified error during reception of the data length byte */
									mexPrintf ("ExtSetTargetMsg: Unspecified error during reception of user telegram length byte (ReadFile).\n");
							
									return EXT_ERROR;

								}

								break;

							case DAT:

								/* received DAT -> incoming user telegram */
								#if DEBUG_MSG_LVL >= 1
								mexPrintf("ExtSetTargetMsg: incoming log data telegram detected.\n");
								#endif
						
								/* register telegram type */
								userData->TelType = DAT;
							
								/* flag detection of incoming user telegram */
								userData->DataTelIntercepted = TRUE;

								#if DEBUG_MSG_LVL >= 2
								mexPrintf("ExtSetTargetMsg: reading data length information (4 bytes).\n");
								#endif

								/* fetch data length  --  4 more bytes */
								nBytesGot = 0;
								TelLenInfoSize = 4;
								if(ReadFile(userData->hCom, (LPSTR)tempSendMsgBuf, TelLenInfoSize, &nBytesGot, NULL)) {
							
									if (nBytesGot != TelLenInfoSize) {
				
										/* nBytesGot != 4 -> TIMEOUT occurred during reception */
										mexPrintf ("ExtSetTargetMsg: Timeout occurred during reception of data telegram length information (ReadFile).\n");
										{
											DWORD err = GetLastError();
											mexPrintf("Error code: %ld\n", err);
										}

										return EXT_ERROR;
									}
									else {

										/* telegram length in 'tempSendMsgBuf[3]' */
										ack_byte = (uint_T)tempSendMsgBuf[3];

										#if DEBUG_MSG_LVL >= 2
										mexPrintf ("ExtSetTargetMsg: Data telegram length: %d bytes\n", ack_byte);
										#endif

										/* store length byte in the buffer 'interceptDatBuf' */
										memcpy(interceptDatBuf, tempSendMsgBuf, TelLenInfoSize);

										#if DEBUG_MSG_LVL >= 3
										{
											int i;
											
											for(i=0; i<TelLenInfoSize; i++) 
											   mexPrintf ("ExtSetTargetMsg: Intercepted data: [%d/%d]) %d\n", i+1, TelLenInfoSize, (uint_T)(interceptDatBuf[i]));
										}
										#endif

										ack_byte -= nBytesGot;				/* use 'ack_byte' to store the number of byte still to get */

									}

								} /* reception of data length byte  --  ReadFile (if) */
								else {

									/* unspecified error during reception of the data length byte */
									mexPrintf ("ExtSetTargetMsg: Unspecified error during reception of data telegram length byte (ReadFile).\n");
							
									return EXT_ERROR;

								}

								break;

⌨️ 快捷键说明

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