📄 ext_transport232.c
字号:
}
#endif
return EXT_NO_ERROR;
}
/* no intercepted message available (-> regular pending message) */
/* read requested amount of data values from COM */
if(ReadFile(userData->hCom, (LPSTR)(dst), nBytesToGet, nBytesGot, NULL)) {
if (*nBytesGot == nBytesToGet) {
#if DEBUG_MSG_LVL >= 2
{
int_T i;
for(i=0; i<*nBytesGot; i++)
mexPrintf("ExtGetTargetMsg: message [%d/%d]) %d\n", i+1, *nBytesGot, (uint_T)(dst[i]));
}
#endif
/* direct calls of this function (not following an ExtTargetTelPending() call) should get MES first */
if(dst[0] == MES) {
int_T oneByteGot = 0;
#if DEBUG_MSG_LVL >= 2
mexPrintf("ExtGetTargetMsg: discarding first byte, shifting data...\n");
#endif
/* shift everything by one byte */
memcpy(dst, &dst[1], *nBytesGot-1);
/* and get the last byte of the message */
/* fw-02-05 : corrected fault *nBytesGot -> *nBytesGot-1 (only makes a difference on targets that need bytes swapped) */
if(ReadFile(userData->hCom, (LPSTR)(&dst[*nBytesGot-1]), 1, &oneByteGot, NULL)) {
if(oneByteGot == 1) {
#if DEBUG_MSG_LVL >= 2
/* fw-02-05 : corrected fault *nBytesGot -> *nBytesGot-1 (only makes a difference on targets that need bytes swapped) */
mexPrintf("ExtGetTargetMsg: message [%d/%d]) %d\n", *nBytesGot, *nBytesGot, (uint_T)(dst[*nBytesGot-1]));
#endif
}
else {
/* oneByteGot != 1 -> TIMEOUT occurred during reception of data */
mexPrintf ("ExtGetTargetMsg: timeout occurred during data reception (ReadFile).\n");
return EXT_ERROR;
}
}
else {
/* unspecified error during data reception */
mexPrintf ("ExtGetTargetMsg: unspecified error during data reception (ReadFile).\n");
return EXT_ERROR;
}
} /* dst[0] == MES */
/* successful reception -> re-order bytes (ms9S12) -- fw-02-05 */
//slCopyNBytes(dst, dst, *nBytesGot, TRUE, 4);
{
int_T i;
char_T dat;
for(i=0; i<*nBytesGot; i +=4) {
dat = dst[i+3];
dst[i+3] = dst[i+0];
dst[i+0] = dat;
dat = dst[i+2];
dst[i+2] = dst[i+1];
dst[i+1] = dat;
}
}
#if DEBUG_MSG_LVL >= 2
{
int_T i;
for(i=0; i<*nBytesGot; i++)
mexPrintf("ExtGetTargetMsg: returning telegram [%d/%d]) %d\n", i+1, *nBytesGot, (uint_T)(dst[i]));
}
#endif
/* reset TelType and return */
userData->TelType = 0;
return EXT_NO_ERROR;
}
else {
/* nBytesGot != nBytesNeeded -> TIMEOUT occurred during reception of data */
mexPrintf ("ExtGetTargetMsg: timeout occurred during data reception (ReadFile).\n");
*nBytesGot = 0;
return EXT_ERROR;
}
}
else {
/* unspecified error during data reception */
mexPrintf ("ExtGetTargetMsg: unspecified error during data reception (ReadFile).\n");
*nBytesGot = 0;
return EXT_ERROR;
}
} /* end ExtGetTargetMsg */
/* Function: ExtGetTargetData ===================================================
* Abstract:
* Attempts to get the specified number of bytes from the data upload line.
* The number of bytes read is returned via the 'nBytesGot' parameter. There
* is no guarantee that the number of bytes read will be equal to the number
* of bytes selected. EXT_NO_ERROR is returned on success, EXT_ERROR is
* returned on failure.
*
* NOTES:
* o it is not an error for 'nBytesGot' to be returned as 0
* o it is o.k. for this function to block if no data is available
*/
PUBLIC boolean_T ExtGetTargetData(
ExternalSim *ES,
const int nBytesToGet,
int *nBytesGot, /* out */
char *dst) /* out */
{
UserData *userData = (UserData *)esGetUserData(ES);
/* check if the ENTIRE data telegram is already available in interceptDatBuf (-> intercepted data telegram) */
if(userData->DataTelIntercepted == TRUE) {
/* yep... -> fetch it from there */
#if DEBUG_MSG_LVL >= 2
mexPrintf("ExtGetTargetData: Previously intercepted data telegram available in interceptDatBuf\n");
#endif
/* first or second call to this function ? */
if(nBytesToGet == sizeof(int32_T) && (int_T)(userData->TelType) != 0) {
/* first call (get 4 bytes : "nBytes information") */
#if DEBUG_MSG_LVL >= 2
mexPrintf("ExtGetTargetData: (first call) Getting 'nBytes' information.\n");
#endif
/* check telegram type */
if(userData->TelType == USR) {
/* USR telegram */
myUsrBuf *rxdbuf = userTelBuf[interceptDatBuf[1]]; /* destination buffer pointer */
int_T nBytesGotAlready = (int_T)interceptDatBuf[0]; /* size of the USR telegram */
#if DEBUG_MSG_LVL >= 2
{
int_T i;
for(i=0; i<nBytesGotAlready; i++)
mexPrintf("ExtGetTargetData: user data [%d/%d]) %d\n", i+1, nBytesGotAlready, (uint_T)(interceptDatBuf[i]));
mexPrintf("ExtGetTargetData: Copy user telegram to the respective data buffer.\n");
}
#endif
/* move the entire USR telegram to the respective buffer; there won't be a second call to this function */
memcpy(rxdbuf->buf, interceptDatBuf, nBytesGotAlready);
/* set data buffer full flag */
rxdbuf->buffer_full = 1;
/* all done -> reset DataTelIntercepted */
userData->DataTelIntercepted = FALSE;
/* reset TelType */
userData->TelType = 0;
/* signal the receipt of a USR telegram (this bypasses the second call to this function) */
*nBytesGot = -1;
}
else {
/* DAT telegram */
#if DEBUG_MSG_LVL >= 2
mexPrintf("ExtGetTargetData: Regular log data -> Reading data length information (4 bytes) from interceptDatBuf\n");
#endif
/* return the first 4 bytes of the intercepted log data telegram from the buffer 'interceptDatBuf' */
memcpy(dst, interceptDatBuf, nBytesToGet);
/* re-order bytes (ms9S12) -- fw-02-05 */
//slCopyNBytes(dst, dst, 4, TRUE, 4);
{
char_T dat;
dat = dst[3];
dst[3] = dst[0];
dst[0] = dat;
dat = dst[2];
dst[2] = dst[1];
dst[1] = dat;
}
/* reset TelType */
userData->TelType = 0;
/* adjust number of bytes received */
*nBytesGot = nBytesToGet;
}
}
else {
/* second call (get remaining "nBytes" data bytes) */
#if DEBUG_MSG_LVL >= 2
mexPrintf("ExtGetTargetData: (second call) reading remainder of the data telegram.\n");
#endif
/* read data from the local buffer 'interceptDatBuf' */
memcpy(dst, interceptDatBuf + sizeof(int32_T), nBytesToGet);
/* re-order bytes (ms9S12) -- fw-02-05 */
//slCopyNBytes(dst, dst, nBytesToGet, TRUE, 4);
{
int_T i;
char_T dat;
for(i=0; i<nBytesToGet; i +=4) {
dat = dst[i+3];
dst[i+3] = dst[i+0];
dst[i+0] = dat;
dat = dst[i+2];
dst[i+2] = dst[i+1];
dst[i+1] = dat;
}
}
#if DEBUG_MSG_LVL >= 2
{
int_T i;
for(i=0; i<nBytesToGet; i++)
mexPrintf("ExtGetTargetData: data telegram [%d/%d]) %d\n", i+1, nBytesToGet, (uint_T)(dst[i]));
}
#endif
/* all done -> reset DataTelIntercepted */
userData->DataTelIntercepted = FALSE;
/* adjust number of bytes received */
*nBytesGot = nBytesToGet;
}
return EXT_NO_ERROR;
} /* userData->DataTelIntercepted == TRUE */
/* no data telegram has previously been intercepted -> regular data reception */
/* read requested amount of data bytes from COM */
if(ReadFile(userData->hCom, (LPSTR)dst, nBytesToGet, nBytesGot, NULL)) {
/* successful reception -> re-order bytes (ms9S12) -- fw-02-05 */
//slCopyNBytes(dst, dst, *nBytesGot, TRUE, 4);
{
int_T i;
char_T dat;
for(i=0; i<*nBytesGot; i +=4) {
dat = dst[i+3];
dst[i+3] = dst[i+0];
dst[i+0] = dat;
dat = dst[i+2];
dst[i+2] = dst[i+1];
dst[i+1] = dat;
}
}
if (*nBytesGot == nBytesToGet) {
/* successful reception */
#if DEBUG_MSG_LVL >= 2
{
int_T i;
for(i=0; i<*nBytesGot; i++)
mexPrintf("ExtGetTargetData: data [%d/%d]) %d\n", i+1, *nBytesGot, (uint_T)(dst[i]));
}
#endif
/* ------------------------------------------------------------------------------------------- */
/* filter out USR telegrams */
/* ------------------------------------------------------------------------------------------- */
if(userData->TelType == USR) {
/* USR telegram (first 4 bytes) */
int_T nBytesGotAlready;
int_T nBytesNeeded;
/* reverse order of first 4 bytes (accounts for the above reversal...) -- 9S12 */
{
char_T dat;
dat = dst[3];
dst[3] = dst[0];
dst[0] = dat;
dat = dst[2];
dst[2] = dst[1];
dst[1] = dat;
}
#if DEBUG_MSG_LVL >= 2
{
int_T i;
for(i=0; i<*nBytesGot; i++)
mexPrintf("ExtGetTargetData: USR data [%d/%d]) %d\n", i+1, *nBytesGot, (uint_T)(dst[i]));
}
#endif
/* determine length of the incoming user telegram */
nBytesGotAlready = *nBytesGot;
nBytesNeeded = (int_T)dst[0] - nBytesGotAlready;
#if DEBUG_MSG_LVL >= 2
mexPrintf("ExtGetTargetData: User telegram. Reading the remaining %d bytes.\n", nBytesNeeded);
#endif
/* read remainder of the detected user telegram */
if(ReadFile(userData->hCom, (LPSTR)(dst+nBytesGotAlready), nBytesNeeded, nBytesGot, NULL)) {
/* USR telegram (remainder) */
if (*nBytesGot == nBytesNeeded) {
myUsrBuf *rxdbuf = userTelBuf[dst[1]];
// successful reception of the remainder of the user data telegram
/* reverse order of bytes within the user telegram... 9S12 */
/* only re-order if the data type length is 4 (single, xint32_T) */
if(dst[2] == 4) {
int_T i;
char_T dat;
for(i=4; i<4+nBytesNeeded; i +=4) {
#if DEBUG_MSG_LVL >= 1
mexPrintf("i = %d/%d\n", i, 4+nBytesNeeded);
#endif
dat = dst[i+3];
dst[i+3] = dst[i+0];
dst[i+0] = dat;
dat = dst[i+2];
dst[i+2] = dst[i+1];
dst[i+1] = dat;
}
}
#if DEBUG_MSG_LVL >= 2
{
int_T i;
for(i=0; i<*nBytesGot; i++)
mexPrintf("ExtGetTargetData: user data [%d/%d]) %d\n", i+1, *nBytesGot, (uint_T)(dst[nBytesGotAlready+i]));
}
#endif
#if DEBUG_MSG_LVL >= 2
mexPrintf("ExtGetTargetData: Copy user telegram to the respective data buffer.\n");
#endif
/* move USR telegram to its associated buffer. Check for potential buffer overflows... */
{
uint_T bufsize = nBytesGotAlready + *nBytesGot;
/* safeguard against buffer overflows in case of communication problems (too many bytes would crash MATLAB) */
if(bufsize > rxdbuf->buf_size) {
#if DEBUG_MSG_LVL >= 2
mexPrintf("ExtGetTargetData: Discarding data which would overflow the buffer (-> communication problem!).\n");
#endif
bufsize = rxdbuf->buf_size;
}
/* move the entire USR telegram to the buffer associated with the detected user channel */
memcpy(rxdbuf->buf, dst, bufsize);
}
/* set data buffer full flag */
rxdbuf->buffer_full = 1;
/* reset TelType */
userData->TelType = 0;
/* signal the receipt of a USR telegram (this bypasses a second call to this function) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -