I am completely new to working with PLCs, but I have a project that need fetch data from an OPC client server, then send it to an Access database table. The bulk of the code minus OPCWriteGroupItems was from someone else. I am just lost on transferring the data that the get from OPCReadGroupItems to the proper variables in my function OPCWriteGroupItems. I just want to get the values that are read into the variables. Thanks.
F.Y.I.
My query statement I know needs to be tweaked so that it will read the variables correctly but that is anouther problem that I am working on. This is the more pressing concern.
Below are the two functions that I spoke of:
//---------------------------------------------------------
// Read items from an OPC Group
//---------------------------------------------------------
OPCStat OPCReadGroupItems (int iGrp, struct Var_Stru v[] )
{
HRESULT r1;
HRESULT *hr;
OPCITEMSTATE *is;
int iItem;
OPCStat RetVal=OPCSuccess;
if (pGRPTagSIO[iGrp]) {
if (bOPCDebug) printf("Reading data .... \n");
r1 = pGRPTagSIO[iGrp]->Read(OPC_DS_CACHE,
OPCDataGroup[iGrp].iNumItems,
OPCDataGroup[iGrp].hItem,
&is,
&hr);
if (FAILED(r1)) {
printf("Error from Read(%lx)\n", r1);
RetVal = OPCError;
} else {
// if the read worked then copy results to the v structure based on type
for (iItem=0;iItem<OPCDataGroup[iGrp].iNumItems;iItem++ ) {
//printf("Read item= %d, hr= %lx, Quality= %2x", iItem, hr[iItem], is[iItem].wQuality);
if(!FAILED(hr[iItem])) {
// printf("%d", is[iItem].vDataValue.vt);
//printf("%d", iGrp);
switch(is[iItem].vDataValue.vt) {
case VT_I2: // 2
v[iItem].iVal = is[iItem].vDataValue.iVal;
break;
case VT_I4: // 3
v[iItem].lVal = is[iItem].vDataValue.lVal;
break;
case VT_R4: // 4
// printf("Z");
v[iItem].fVal = is[iItem].vDataValue.fltVal;
break;
case VT_R8: // 5
v[iItem].dblVal = is[iItem].vDataValue.dblVal;
break;
case VT_BOOL: // 11
v[iItem].bVal = is[iItem].vDataValue.boolVal;
break;
case VT_UI1: // 11
printf("TEST");
sprintf(v[iItem].cVal, "%s", is[iItem].vDataValue.cVal);
printf("%s", v[iItem].cVal);
printf("%c", v[iItem].cVal);
break;
case VT_BSTR: // 8
// printf("TEST");
sprintf(v[iItem].cVal,"%-.*ls", kOPCStringSize,is[iItem].vDataValue.bstrVal );
//printf("\n STRING STRING %s \n", v[iItem].cVal);
break;
default:
break;
}
}
if(iGrp >= 0 && iGrp <=)
{
stuct_double[iGrp-1][iItem] = v[iItem].dblVal;
// printf("group %d ", iGrp);
// printf("Tag %i: %10.2f\n", iItem, root_plc[iItem]);
}
if(iGrp > 0 && iGrp <= 44)
{
struc_floats[iGrp-1][iItem] = v[iItem].fVal;
//printf("GROUP %d ", iGrp);
//printf("Tag %i: %10.2f\n", iItem, struc_floats[iGrp-1][iItem]);
}
else
{
printf("Error reading data item %d\n", iItem);
//RetVal=OPCError; //Make not as severe -- rjf 06/03/02
RetVal=OPCWarning;
//break; //Escape the for-loop -- rjf- 5/13/02
}
*/
pIMalloc->Free(hr);
for (iItem=0;iItem<OPCDataGroup[iGrp].iNumItems;iItem)
{
VariantClear($is[iItem].vdatavalue)
}
pIMalloc->Free(is);
}
}
else{
RetVal=OPCError;
}
return (RetVal);
}
//-----------------------------------------
// Write items contained in an OPC Group
//-----------------------------------------
OPCStat OPCWriteGroupItems (int iGrp, struct Var_Stru vOut[] )
{
/* Data Access Method used in this sample */
const char* DAM = "Direct ODBC";
/* Connection string for Direct ODBC */
char szDSN[256] = "DSN=Gas_Meter_check;";
HRESULT r1,r2;
HRESULT *hr;
VARIANT v[nMaxOPCItems];
int iItem;
OPCStat RetVal=OPCSuccess;
if (pGRPTagSIO[iGrp]) {
for (iItem=0;iItem<OPCDataGroup[iGrp].iNumItems;iItem++) {
v[iItem].vt = OPCDataGroup[iGrp].iType;
switch(OPCDataGroup[iGrp].iType) {
case VT_I2: // 2
v[iItem].iVal = vOut[iItem].iVal;
break;
case VT_I4: // 3
v[iItem].lVal = vOut[iItem].lVal;
break;
case VT_R4: // 4
v[iItem].fltVal = vOut[iItem].fVal;
break;
case VT_R8: // 5
v[iItem].dblVal = vOut[iItem].dblVal;
HENV hEnv; HDBC hDbc;
/* ODBC API return status */ RETCODE rc;
int iConnStrLength2Ptr;
char szConnStrOut[256];
int i= 0;
char datetime[256];
double HMCO=1.0;
double HMR,HMT;
unsigned char* InsertQuery = "INSERT INTO Data ( [Date / Time], [Hot Strip Mill rate], [Hot Strip Mill Comm Okay], [Hot Strip Mill Total] ) SELECT #datetime# AS Expr1, HMR AS Expr2, 1 AS Expr3, HMCO AS Expr4, HMT AS Expr5;";
SQLCHAR chval1[128], chval2[128], colName[128];
int ret1;
int ret2;
/* Number of rows and columns in result set */
SQLINTEGER rowCount = 0;
SQLSMALLINT fieldCount = 0, currentField = 0;
HSTMT hStmt;
/* Allocate an environment handle */
rc = SQLAllocEnv(&hEnv);
/* Allocate a connection handle */
rc = SQLAllocConnect(hEnv, &hDbc);
/* Connect to the TakeCharge database */
rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN,
SQL_NTS, (unsigned char*)szConnStrOut,
255, (SQLSMALLINT*)&iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
if (SQL_SUCCEEDED(rc))
{
printf("%s: Successfully connected to database. Data source name: \n %s\n",
DAM, szConnStrOut);
/* Prepare SQL query */
printf("%s: SQL InsertQuery:\n %s\n", DAM, InsertQuery);
rc = SQLAllocStmt(hDbc,&hStmt);
rc = SQLPrepare(hStmt, InsertQuery, SQL_NTS);
/* Excecute the query and create a record set */
rc = SQLExecute(hStmt);
if (SQL_SUCCEEDED(rc))
{
printf("Executing query...");
printf("\n");
}
while (SQL_SUCCEEDED(rc))
{
printf(" insert passed\n");
rc = SQLFetch(hStmt);
rowCount++;
};
}
else
{
printf("%s: Couldn't connect to %s.\n", DAM, szDSN);
}
/* Disconnect*/
SQLDisconnect(hDbc);
printf("%s: Cleanup. Done.\n", DAM);
break;
case VT_BOOL: // 11
v[iItem].bVal = vOut[iItem].bVal;
break;
case VT_BSTR: // 8
// printf(" In Message value (VT_BSTR) = %s \n",vOut[iItem].cVal );
r2 = LPTSTR_to_BSTR ( &v[iItem].bstrVal , vOut[iItem].cVal);
if ( r2 != S_OK ) {
printf ("error in memory \n");
RetVal = OPCError;
}
// printf(" Write Msg value(VT_BSTR) = %ls \n", v[iItem].bstrVal );
report(0,0,0, "STRINGS data %s",vOut[iItem].cVal);
break;
default:
printf(" value(unknown type:%d) ", OPCDataGroup[iGrp].iType );
RetVal = OPCError;
break;
}
//if (bOPCDebug) DumpVariant(OPCDataGroup[iGrp].cTagNames[iItem], &v[iItem]);
}
r1 = pGRPTagSIO[iGrp]->Write(
OPCDataGroup[iGrp].iNumItems,
OPCDataGroup[iGrp].hItem,
v,
&hr);
if (FAILED(r1))
{
printf("Error from Write(%lx)\n", r1);
RetVal = OPCError;
} else {
//if (bOPCDebug) printf("Successful Write ... \n");
// Clear the Variant
for (iItem=0;iItem<OPCDataGroup[iGrp].iNumItems;iItem++) {
VariantClear(&v[iItem]);
}
pIMalloc->Free(hr);
}
} else {
RetVal = OPCError;
}
return(RetVal);
}
C/C++ is not my first language, and I am not familiar with your specific OPC library (there are so many), but according to my understanding of the code you are reading some kind of a variant variable, and with case statements you determine it's type and store it to proper property of v[iItem]. You repeat that in a loop for all tags in that group. There must be some property that tells you the name of the tag you have just read, and you can use it to solve the problem.
For example, if the current tag you are reading is of single float type, then this case section will be executed:
case VT_R4:
v[iItem].fVal = is[iItem].vDataValue.fltVal;
break;
So v[iItem].fVal contains the 4 bytes single float value of the OPC tag. If the current tag you are reading is of double float type, then this case section will be executed:
case VT_R8:
v[iItem].dblVal = is[iItem].vDataValue.dblVal;
break;
and v[iItem].dblVal contains the 8 bytes double float value of the OPC tag. I think you get the picture.
You should really spend some time with OPC DA tutorials on http://www.opcfoundation.org.
Related
I have this code right here I am using mingw to compile and I am running windows XP. When I run the output executable it gives me error prompt about the application..
Do I set everything correctly ? How do I set the SSID password for it to connect ? Also I want to know if I am using the correct network interface to connect (I have an external wifi adapter)..
I got the code from this link any help is very much appreciated..
//#define _WIN32_DCOM
#define _WIN32_WINNT 0x0600
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <objbase.h>
#include <rpcsal.h>
#include <objbase.h>
#include <wlanapi.h>
int main(){
PDOT11_SSID pSsid;
strcpy(pSsid->ucSSID, "SUPERONLINE-WiFi_24811");
pSsid->uSSIDLength = 25;
HANDLE wlanHandle;
unsigned long nv;
WlanOpenHandle(1, NULL, &nv, &wlanHandle);
DWORD dwResult = 0;
WLAN_CONNECTION_PARAMETERS cp;
memset(&cp, 0, sizeof(WLAN_CONNECTION_PARAMETERS));
cp.wlanConnectionMode = wlan_connection_mode_profile;
cp.strProfile = NULL;
cp.dwFlags = 0;
cp.pDot11Ssid = pSsid;
cp.pDesiredBssidList = 0;
cp.dot11BssType = dot11_BSS_type_any;
PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
PWLAN_INTERFACE_INFO pIfInfo = NULL;
// only use the first wifi interface
dwResult = WlanEnumInterfaces(wlanHandle, NULL, &pIfList);
pIfInfo = (WLAN_INTERFACE_INFO *)&pIfList->InterfaceInfo[1];
if (dwResult == ERROR_SUCCESS)
{
dwResult = WlanConnect(wlanHandle, &(pIfInfo->InterfaceGuid), &cp, NULL);
if (dwResult == ERROR_SUCCESS)
{
printf("Connected..\n");
//connected = true;
}
}
return 0;
}
This is the error I get:
Edit: I have made it this far to the code below.. But still I can not assign the value to PDOT11_SSID struct.. It seems the struct has UCHAR array yet I can't assign.. Here is the link
PDOT11_SSID pSsid;
UCHAR arr[22] = { 0 };
//memset(pSsid->ucSSID, '\0', 32); //this causes the same error too
memcpy(
arr,
(unsigned char[]){ 'S','U','P','E','R','O','N','L','I','N','E','-','W','i','F','i','_','2','4','8','1','1' },
sizeof(arr)); //memcpy to arr struct works..
ULONG len = 22;
pSsid->uSSIDLength = len;
memcpy(pSsid->ucSSID, arr, sizeof(arr)); // this line fails still..
Edit: I ran with -Wall -Wextra -pedantic -Werror Like David mentioned and with the code segment below:
PDOT11_SSID pSsid;
memset(pSsid->ucSSID, '\0', 32); //fails
I got this error
yet if I initialized with
PDOT11_SSID pSsid = { 0 };
or
PDOT11_SSID pSsid = NULL;
It compiled but I got the crash image above I used to get.. Important to note that memset(pSsid->ucSSID, '\0', 32); causes the crash as well. I can know this by commenting out each line and compiling/running again.
This is a burden since I am yet to figure out how to add SSID password with wlanapi.h so help for these will be much appreciated for me to move on forward.
Fyi: I use basic gcc to compile gcc.exe wlan.c -lwlanapi. If there is any working solution code to connect to an ssid with the ssid and password using wlanapi, a link to that is also appreciated.
Wlan connect from msdn is this link.. I can't see any security parameters, how do I set the password of the SSID I want to connect to ?
Here is what you have been looking for:
#include <winsock2.h>
#include <Wlanapi.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma warning(disable:4996) /*fuss sprintf*/
#define SSID_MAX_LEN (32)
int ConnectToTargetWifiSSID(char *pSSIDName, char *pPassword)
{
DWORD dwVersion;
#if(0)
DWORD dwMajorVersion;
dwVersion = 0 = dwMajorVersion;
dwVersion = GetVersion();
dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
DWORD dwClientVersion;
dwClientVersion = ( dwMajorVersion >= 6) ? 2 : 1 ; /*vista or latter*/
#endif
DWORD result;
HANDLE iWifiHandle;
PWLAN_INTERFACE_INFO_LIST iAvailableInterfaces;
PWLAN_AVAILABLE_NETWORK_LIST availableNetworkList;
GUID iInterfaceGuid;
int isHavingProfile;
char authentication[64];
char encryption[64];
int isOpenedAP;
unsigned int i;
unsigned int iii;
WLAN_CONNECTION_PARAMETERS connParam;
iWifiHandle = NULL;
iAvailableInterfaces = NULL;
availableNetworkList = NULL;
/*part zero : one a wlan handle*/
result = WlanOpenHandle(1,NULL,&dwVersion, &iWifiHandle);
if(NULL != iWifiHandle)
{
/*get wireless network card*/
result = WlanEnumInterfaces(iWifiHandle, NULL, &iAvailableInterfaces);
if(ERROR_SUCCESS == result && iAvailableInterfaces->dwNumberOfItems > 0)
{
/*chose the zeroth one*/
printf("InterFaceName: %ls",iAvailableInterfaces->InterfaceInfo[0].strInterfaceDescription);
iInterfaceGuid = iAvailableInterfaces->InterfaceInfo[0].InterfaceGuid;
}
else
{
/*no wireless card*/
result = -2;
goto Exit_ConnectToTargetSSID;
}
}
else
{
result = -1;
goto Exit_ConnectToTargetSSID;
}/*if NULL != iWifiHandle*/
/*part one: scan available wifi routers(SSID)*/
result= WlanGetAvailableNetworkList(iWifiHandle, &iInterfaceGuid,
0x00000001, NULL, &availableNetworkList);
//WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES, NULL, &availableNetworkList);
if(ERROR_SUCCESS != result)
return -3;
isHavingProfile = FALSE;
isOpenedAP = FALSE;
iii = -1;
memset(&authentication[0], 0, 64);
memset(&encryption[0], 0, 64);
if( 0 == availableNetworkList->dwNumberOfItems)
{
/*on wifi router has been found*/
result = -4;
goto Exit_ConnectToTargetSSID;
}/*if 0 < wifiList->dwNumberOfItems*/
for(i = 0; i < availableNetworkList->dwNumberOfItems; i++)
{
DWORD flag;
printf("SSID:\t\t\t%s\nSIGNAL:\t\t\t%d\n",
availableNetworkList->Network[i].dot11Ssid.ucSSID,
availableNetworkList->Network[i].wlanSignalQuality);
printf("SECURITY:\t\t");
switch(availableNetworkList->Network[i].dot11DefaultAuthAlgorithm)
{
case DOT11_AUTH_ALGO_80211_OPEN:
printf("OPEN\n");
break;
case DOT11_AUTH_ALGO_80211_SHARED_KEY:
printf("WEP\n");
break;
case DOT11_AUTH_ALGO_WPA:
case DOT11_AUTH_ALGO_WPA_PSK:
case DOT11_AUTH_ALGO_WPA_NONE:
printf("WPA\n");
break;
case DOT11_AUTH_ALGO_RSNA:
case DOT11_AUTH_ALGO_RSNA_PSK:
printf("WPA2\n");
break;
default:
printf("UNKNOWN\n");
break;
}
printf("encryption:\t\t");
switch(availableNetworkList->Network[i].dot11DefaultCipherAlgorithm)
{
case DOT11_CIPHER_ALGO_NONE:
printf("NONE\n");
break;
case DOT11_CIPHER_ALGO_WEP40:
printf("WEP40\n");
break;
case DOT11_CIPHER_ALGO_TKIP:
printf("TKIP\n");
break;
case DOT11_CIPHER_ALGO_WEP104:
printf("WEP104\n");
break;
case DOT11_CIPHER_ALGO_CCMP:
printf("CCMP\n");
break;
default:
printf("UNKNOWN\n");
break;
}/*switch*/
flag = availableNetworkList->Network[i].dwFlags;
if(flag & 0x00000001)
printf("\t NOTE : Current connecting\n");
if(flag & 0x00000002)
printf("\t NOTE : WLAN_AVAILABLE_NETWORK_HAS_PROFILE\n");
if(flag & 0x00000004)
printf("\t NOTE : WLAN_AVAILABLE_NETWORK_CONSOLE_USER_PROFILE\n");
/*if(flag & WLAN_AVAILABLE_NETWORK_CONNECTED)
printf("\t NOTE : Current connecting\n");
if(flag & WLAN_AVAILABLE_NETWORK_HAS_PROFILE)
printf("\t NOTE : WLAN_AVAILABLE_NETWORK_HAS_PROFILE\n");
if(flag & WLAN_AVAILABLE_NETWORK_CONSOLE_USER_PROFILE)
printf("\t NOTE : WLAN_AVAILABLE_NETWORK_CONSOLE_USER_PROFILE\n");*/
printf("\n");
}/*for */
/*part two: save target SSID propertis*/
for(i = 0; i < availableNetworkList->dwNumberOfItems; i++)
{
if(0 == strncmp(pSSIDName, (char*)availableNetworkList->Network[i].dot11Ssid.ucSSID , SSID_MAX_LEN))
{
//WLAN_AVAILABLE_NETWORK_CONNECTED
if( 0x00000001 & availableNetworkList->Network[i].dwFlags)
{
printf("%s is current connecting!!\n", pSSIDName);
result = 1;
goto Exit_ConnectToTargetSSID;
}/*if*/
iii = i;
if(0x00000002 & availableNetworkList->Network[i].dwFlags)
//if(WLAN_AVAILABLE_NETWORK_HAS_PROFILE & availableNetworkList->Network[i].dwFlags)
isHavingProfile = TRUE;
/*list the target SSID properties*/
switch(availableNetworkList->Network[i].dot11DefaultAuthAlgorithm)
{
case DOT11_AUTH_ALGO_80211_OPEN:
sprintf(&authentication[0], "OPEN");
break;
case DOT11_AUTH_ALGO_80211_SHARED_KEY:
sprintf(&authentication[0], "WEP");
break;
case DOT11_AUTH_ALGO_WPA:
case DOT11_AUTH_ALGO_WPA_PSK:
case DOT11_AUTH_ALGO_WPA_NONE:
sprintf(&authentication[0], "WPAPSK");
break;
case DOT11_AUTH_ALGO_RSNA:
case DOT11_AUTH_ALGO_RSNA_PSK:
sprintf(&authentication[0], "WPA2PSK");
break;
default:
sprintf(&authentication[0], "UNKNOWN");
break;
}/*switch dot11DefaultAuthAlgorithm*/
switch(availableNetworkList->Network[i].dot11DefaultCipherAlgorithm)
{
case DOT11_CIPHER_ALGO_NONE:
sprintf(&encryption[0], "NOEN");
break;
case DOT11_CIPHER_ALGO_TKIP:
sprintf(&encryption[0], "TKIP");
break;
case DOT11_CIPHER_ALGO_CCMP:
sprintf(&encryption[0], "AES");
break;
default:
sprintf(&encryption[0], "WEP");
break;
}/*/*switch dot11DefaultCipherAlgorithm*/
break;
}/*if 0 == strncmp(pSSIDName, (char*)availableNetworkList->Network[i].dot11Ssid.ucSSID , SSID_MAX_LEN)*/
}/*for i*/
if(-1 == iii)
{
/*target router could not found */
result = -5;
goto Exit_ConnectToTargetSSID;
}/*if */
/*part there, set XML profile*/
if(FALSE == isHavingProfile)
{
/*if current computer has never connected to target router..*/
wchar_t profileXMLUnicode[4096];
char buff[4096];
DWORD reasonCode;
char profileTemplateXML[] = "<?xml version=\"1.0\"?>" \
"<WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\">"\
" <name>%s</name>" \
" <SSIDConfig> " \
" <SSID>" \
" <name>%s</name>"\
" </SSID>"\
" </SSIDConfig>"\
" <connectionType>ESS</connectionType>"\
" <connectionMode>auto</connectionMode>"\
" <MSM>"\
" <security>"\
" <authEncryption>"\
" <authentication>%s</authentication>"\
" <encryption>%s</encryption>"\
" <useOneX>false</useOneX>"\
" </authEncryption>"\
" <sharedKey>"\
" <keyType>passPhrase</keyType>"\
" <protected>false</protected>"\
" <keyMaterial>%s</keyMaterial>"\
" </sharedKey>"\
" </security>" \
" </MSM>" \
"</WLANProfile>" ;
reasonCode = 0;
sprintf(buff, profileTemplateXML, availableNetworkList->Network[iii].dot11Ssid.ucSSID,
availableNetworkList->Network[iii].dot11Ssid.ucSSID,
&authentication[0], &encryption[0], pPassword);
/*Covert ansi to unicode*/
MultiByteToWideChar(CP_ACP, 0, &buff[0], -1, &profileXMLUnicode[0], 4096);
result = WlanSetProfile(iWifiHandle, &iInterfaceGuid, 0, &profileXMLUnicode[0],
NULL, TRUE, NULL, &reasonCode);
wprintf( L"%s", profileXMLUnicode);
if(ERROR_SUCCESS != result)
{
result = -6;
goto Exit_ConnectToTargetSSID;
}/*if */
}
else
{
/*if current computer had connected to target router, and remember who it is ...*/
DWORD dwFlags;
DWORD dwGrantedAccess;
LPWSTR xml;
result = WlanGetProfile(iWifiHandle, &iInterfaceGuid,
availableNetworkList->Network[iii].strProfileName, NULL, &xml ,&dwFlags,&dwGrantedAccess);
wprintf( L"%s", xml);
}/*if FASLSE == isHavingProfile*/
/*part four, connect to target ssid */
connParam.pDot11Ssid= NULL;
connParam.strProfile= availableNetworkList->Network[iii].strProfileName;
connParam.wlanConnectionMode = wlan_connection_mode_profile;
connParam.pDesiredBssidList=NULL;
connParam.dot11BssType= availableNetworkList->Network[iii].dot11BssType;
connParam.dwFlags = 0;
//wprintf( L"%s", &iInterfaceGuid,wifiList->Network[iii].strProfileName);
result = WlanConnect(iWifiHandle, &iInterfaceGuid, &connParam, NULL);
if(ERROR_SUCCESS != result)
result = -7;
Exit_ConnectToTargetSSID:
if(NULL != availableNetworkList)
WlanFreeMemory(availableNetworkList);
if(NULL != iAvailableInterfaces)
WlanFreeMemory(iAvailableInterfaces);
if(NULL != iWifiHandle)
WlanCloseHandle(iWifiHandle, NULL);
return result;
}/*ConnectToTargetWifiSSID*/
int main(int argc, char *argv[])
{
ConnectToTargetWifiSSID("SUPERONLINE-WiFi_24811", "pass123");
return 0;
}/*main*/
I have a c file written using mex functions, run by Matlab using mex compiler Now I want to run those mex type .c files without Matlab environment. So, is there any way to convert mex type .c file to normal .c file ? Or any other options available for the above mentioned concern.
Thanks in advance....!
Here is the one mex type .c file for sample.
#include <stdlib.h>
#include "mex.h"
#include "matrix.h"
#include "distances.h"
#include "ltl2tree.h"
#include "param.h"
#define BUFF_LEN 4096
char * emalloc(size_t n)
{
char *tmp;
if (!(tmp = (char *) mxMalloc(n)))
mexErrMsgTxt("mx_dp_taliro: not enough memory!");
memset(tmp, 0, n);
return tmp;
}
int tl_Getchar(int *cnt, size_t hasuform, char *uform)
{
if (*cnt < hasuform)
return uform[(*cnt)++];
(*cnt)++;
return -1;
}
void tl_UnGetchar(int *cnt)
{
if (*cnt > 0) (*cnt)--;
}
#define Binop(a) \
fprintf(miscell->tl_out, "("); \
dump(n->lft, miscell); \
fprintf(miscell->tl_out, a); \
dump(n->rgt, miscell); \
fprintf(miscell->tl_out, ")")
static void sdump(Node *n, char *dumpbuf)
{
switch (n->ntyp) {
case PREDICATE: strcat(dumpbuf, n->sym->name);
break;
case U_OPER: strcat(dumpbuf, "U");
goto common2;
case V_OPER: strcat(dumpbuf, "V");
goto common2;
case OR: strcat(dumpbuf, "|");
goto common2;
case AND: strcat(dumpbuf, "&");
common2: sdump(n->rgt,dumpbuf);
common1: sdump(n->lft,dumpbuf);
break;
case NEXT: strcat(dumpbuf, "X");
goto common1;
case NOT: strcat(dumpbuf, "!");
goto common1;
case TRUE: strcat(dumpbuf, "T");
break;
case FALSE: strcat(dumpbuf, "F");
break;
default: strcat(dumpbuf, "?");
break;
}
}
Symbol *DoDump(Node *n, char *dumpbuf, Miscellaneous *miscell)
{
if (!n) return ZS;
if (n->ntyp == PREDICATE)
return n->sym;
dumpbuf[0] = '\0';
sdump(n,dumpbuf);
return tl_lookup(dumpbuf, miscell);
}
void dump(Node *n, Miscellaneous *miscell)
{
if (!n) return;
switch(n->ntyp) {
case OR: Binop(" || "); break;
case AND: Binop(" && "); break;
case U_OPER: Binop(" U "); break;
case V_OPER: Binop(" V "); break;
case NEXT:
fprintf(miscell->tl_out, "X");
fprintf(miscell->tl_out, " (");
dump(n->lft, miscell);
fprintf(miscell->tl_out, ")");
break;
case NOT:
fprintf(miscell->tl_out, "!");
fprintf(miscell->tl_out, " (");
dump(n->lft, miscell);
fprintf(miscell->tl_out, ")");
break;
case FALSE:
fprintf(miscell->tl_out, "false");
break;
case TRUE:
fprintf(miscell->tl_out, "true");
break;
case PREDICATE:
fprintf(miscell->tl_out, "(%s)", n->sym->name);
break;
case -1:
fprintf(miscell->tl_out, " D ");
break;
default:
printf("Unknown token: ");
tl_explain(n->ntyp);
break;
}
}
void tl_explain(int n)
{
switch (n) {
case ALWAYS: printf("[]"); break;
case EVENTUALLY: printf("<>"); break;
case IMPLIES: printf("->"); break;
case EQUIV: printf("<->"); break;
case PREDICATE: printf("predicate"); break;
case OR: printf("||"); break;
case AND: printf("&&"); break;
case NOT: printf("!"); break;
case U_OPER: printf("U"); break;
case V_OPER: printf("V"); break;
case NEXT: printf("X"); break;
case TRUE: printf("true"); break;
case FALSE: printf("false"); break;
case ';': printf("end of formula"); break;
default: printf("%c", n); break;
}
}
static void non_fatal(char *s1, char *s2, int *cnt, char *uform, int *tl_yychar, Miscellaneous *miscell)
{
int i;
printf("TaLiRo: ");
if (s2)
printf(s1, s2);
else
printf(s1);
if ((*tl_yychar) != -1 && (*tl_yychar) != 0)
{ printf(", saw '");
tl_explain((*tl_yychar));
printf("'");
}
printf("\nTaLiRo: %s\n---------", uform);
for (i = 0; i < (*cnt); i++)
printf("-");
printf("^\n");
fflush(stdout);
(miscell->tl_errs)++;
}
void
tl_yyerror(char *s1, int *cnt, char *uform, int *tl_yychar, Miscellaneous *miscell)
{
Fatal(s1, (char *) 0, cnt, uform, tl_yychar, miscell);
}
void
Fatal(char *s1, char *s2, int *cnt, char *uform, int *tl_yychar, Miscellaneous *miscell)
{
non_fatal(s1, s2, cnt, uform, tl_yychar, miscell);
tl_exit(0);
}
void fatal(char *s1, char *s2, int *cnt, char *uform, int *tl_yychar, Miscellaneous *miscell)
{
non_fatal(s1, s2, cnt, uform, tl_yychar, miscell);
tl_exit(0);
}
void put_uform(char *uform, Miscellaneous *miscell)
{
fprintf(miscell->tl_out, "%s", uform);
}
void tl_exit(int i)
{
mexErrMsgTxt("mx_dp_taliro: unexpected error, tl_exit executed.");
}
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
/* Variables needed to process the input */
int status, pstatus;
mwSize buflen, pbuflen;
size_t NElems,NCells;
mwSize ndimA, ndimb, ndimG, ndim, pdim,ndimP;
const mwSize *dimsA, *dimsb, *dimsG, *dims, *pardims,*dimsP;
mwIndex jstruct, iii, jjj, i1, j1, idx_j;
mxArray *tmp,*tmp1,*tmp_cell;
/* Variables needed for monitor */
Node *node;
double *XTrace, *TStamps, *LTrace;
DistCompData distData;
PMap *predMap;
int ii,jj,kk,ll,objs,tempI;
bool par_on;
bool is_Multi_HA;
bool initial_of_par;
int npred, npar;
static char uform[BUFF_LEN];
static size_t hasuform=0;
static int *cnt;
int temp = 0;
Miscellaneous *miscell = (Miscellaneous *) emalloc(sizeof(Miscellaneous));
int *tl_yychar = (int *) emalloc(sizeof(int));
miscell->dp_taliro_param.LTL = 1;
miscell->dp_taliro_param.ConOnSamples = 0;
miscell->dp_taliro_param.SysDim = 0;
miscell->dp_taliro_param.nSamp = 0;
miscell->dp_taliro_param.nPred = 0;
miscell->dp_taliro_param.true_nPred = 0;
miscell->dp_taliro_param.tnLoc = 0;
miscell->dp_taliro_param.nInp = 0;
miscell->dp_taliro_param.nCLG = 0;
miscell->tl_errs = 0;
miscell->type_temp = 0;
/* Reset cnt to 0:
cnt is the counter that points to the next symbol in the formula
to be processed. This is a static variable and it retains its
value between Matlab calls to mx_dp_taliro. */
cnt = &temp;
/* Other initializations */
miscell->dp_taliro_param.nInp = nrhs;
par_on = false;
initial_of_par = false;
npred = 0;
npar= 0;
/* Make sure the I/O are in the right form */
if(nrhs < 3)
mexErrMsgTxt("mx_dp_taliro: At least 3 inputs are required.");
else if(nlhs > 1)
mexErrMsgTxt("mx_dp_taliro: Too many output arguments.");
else if(nrhs > 8)
mexErrMsgTxt("mx_dp_taliro: Too many input arguments.");
else if(!mxIsChar(prhs[0]))
mexErrMsgTxt("mx_dp_taliro: 1st input must be a string with TL formula.");
else if(!mxIsStruct(prhs[1]))
mexErrMsgTxt("mx_dp_taliro: 2nd input must be a structure (predicate map).");
else if(!mxIsDouble(prhs[2]))
mexErrMsgTxt("mx_dp_taliro: 3rd input must be a numerical array (State trace).");
else if(nrhs>3 && !mxIsDouble(prhs[3]))
mexErrMsgTxt("mx_dp_taliro: 4th input must be a numerical array (Time stamps).");
else if(nrhs>5 && !mxIsDouble(prhs[4]))
mexErrMsgTxt("mx_dp_taliro: 5th input must be a numerical array (Location trace).");
else if(nrhs>5 && !(mxIsDouble(prhs[5])||mxIsCell(prhs[5])))
mexErrMsgTxt("mx_dp_taliro: 6th input must be a numerical array \n (Minimum path distance to each control location for each predicate).");
else if(nrhs>7 && !(mxIsCell(prhs[6])))
mexErrMsgTxt("mx_dp_taliro: 7th input must be a cell array (Adjacency list).");
else if(nrhs>7 && !(mxIsStruct(prhs[7])||mxIsCell(prhs[7])))
mexErrMsgTxt("mx_dp_taliro: 8th input must be a structure (guard map).");
if(nlhs > 1)
mexErrMsgTxt("Too many output arguments.");
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
/* Process inputs */
/* Get the formula */
ndim = mxGetNumberOfDimensions(prhs[0]);
dims = mxGetDimensions(prhs[0]);
buflen = dims[1]*sizeof(mxChar)+1;
if (buflen >= BUFF_LEN)
{
mexPrintf("%s%d%s\n", "The formula must be less than ", BUFF_LEN," characters.");
mexErrMsgTxt("mx_dp_taliro: Formula too long.");
}
status = mxGetString(prhs[0], uform, buflen);
hasuform = strlen(uform);
for (iii=0; iii<hasuform; iii++)
{
if (uform[iii] == '\t' || uform[iii] == '\"' || uform[iii] == '\n')
uform[iii] = ' ';
}
/* Get state trace */
ndim = mxGetNumberOfDimensions(prhs[2]);
if (ndim>2)
mexErrMsgTxt("mx_dp_taliro: The state trace is not in proper form!");
dims = mxGetDimensions(prhs[2]);
miscell->dp_taliro_param.nSamp = dims[0];
miscell->dp_taliro_param.SysDim = dims[1];
XTrace = mxGetPr(prhs[2]);
/* Get time stamps */
if (nrhs>3)
{
ndim = mxGetNumberOfDimensions(prhs[3]);
if (ndim>2)
mexErrMsgTxt("mx_dp_taliro: The time stamp sequence is not in proper form!");
dims = mxGetDimensions(prhs[3]);
if (miscell->dp_taliro_param.nSamp != dims[0])
mexErrMsgTxt("mx_dp_taliro: The lengths of the time stamp sequence and the state trace do not match!");
TStamps = mxGetPr(prhs[3]);
}
/* Get location trace and location graph */
if (nrhs>4)
{
ndim = mxGetNumberOfDimensions(prhs[4]);
if (ndim>2)
mexErrMsgTxt("mx_dp_taliro: The location trace is not in proper form!");
dims = mxGetDimensions(prhs[4]);
if (miscell->dp_taliro_param.nSamp != dims[0])
mexErrMsgTxt("mx_dp_taliro: The lengths of the location trace and the state trace do not match!");
LTrace = mxGetPr(prhs[4]);
/* For Multiple H.A.s is added */
if(nrhs>5 && (mxIsCell(prhs[5]))){/* For Multiple H.A.s */
is_Multi_HA=1;
miscell->dp_taliro_param.nCLG = dims[1];
ndim = mxGetNumberOfDimensions(prhs[5]);
if (ndim>2)
{
mexErrMsgTxt("mx_dp_taliro.c: !");
}
dims = mxGetDimensions(prhs[5]);
if (dims[0]!=1)
{
mexErrMsgTxt("mx_dp_taliro: DMin is a cell, it must be a column vector cell!");
}
if (dims[1]!=miscell->dp_taliro_param.nCLG)
{
mexErrMsgTxt("mx_dp_taliro: DMin is a cell, it must be of the size of nCLG!");
}
distData.LDistNCLG = (double **)emalloc((miscell->dp_taliro_param.nCLG)*sizeof(double*));
miscell->dp_taliro_param.tnLocNCLG = (mwSize*)emalloc((miscell->dp_taliro_param.nCLG)*sizeof(mwSize));
for(kk=0; kk<miscell->dp_taliro_param.nCLG; kk++){
tmp = mxGetCell(prhs[5],kk);
dims = mxGetDimensions(tmp);
miscell->dp_taliro_param.tnLocNCLG[kk] = dims[0];
distData.LDistNCLG[kk]=mxGetPr(tmp);
}
}
else if(nrhs>5) {/* For Single H.A. */
is_Multi_HA=0;
ndim = mxGetNumberOfDimensions(prhs[5]);
if (ndim>2)
mexErrMsgTxt("mx_dp_taliro: The minimum distance array is not in proper form!");
dims = mxGetDimensions(prhs[5]);
miscell->dp_taliro_param.tnLoc = dims[0];
distData.LDist = mxGetPr(prhs[5]);
}
}
}
In your code, you are using a lot of functions and data types, provided by the MEX API. Therefore, if you want to produce "normal" C-Code you will have to completly refactor your code.
But one possible solution might be to compile your source code outside MATLAB IDE, by gcc or any other compiler. In this case you will have to include all necessary header files and link against all libraries you need.
Follwo the link to find a pretty good documentation from Mathworks:
https://de.mathworks.com/matlabcentral/answers/377799-compiling-mex-files-without-the-mex-command
Another way for creating C Code from Matlab Scipts/Functions is to use MATLAB Coder:
https://de.mathworks.com/products/matlab-coder.html
Hope this is helpful.
I have written UDP packet forwarder which opens UDP port and it continuously listen on perticolar socket and when data comes to it, it forwards data to another end point.
Problem I am facing is while running process for longer duration its process eats up almost 50% of system memory and other process suffers from it.
I have use PCRE library for some regex checking but it seems like main cause of problem how can i optimize my code that can run for infinite without any memory issue.
below is my code i have use of PCRE regex and socket forwarder.
int parse(char *str){
pcre *reCompiled;
pcre_extra *pcreExtra;
int pcreExecRet;
int subStrVec[30];
const char *pcreErrorStr;
int pcreErrorOffset;
int returnval;
char *aStrRegex;
char **aLineToMatch;
const char *psubStrMatchStr;
int j;
//*aLineToMatch = str;
aStrRegex = "(.*)(\"ABC\":)(\\d+)+";
//printf("Regex to use: %s\n", aStrRegex);
// First, the regex string must be compiled.
reCompiled = pcre_compile(aStrRegex, 0, &pcreErrorStr, &pcreErrorOffset, NULL);
/* OPTIONS (second argument) (||'ed together) can be:
PCRE_ANCHORED -- Like adding ^ at start of pattern.
PCRE_CASELESS -- Like m//i
PCRE_DOLLAR_ENDONLY -- Make $ match end of string regardless of \n's
No Perl equivalent.
PCRE_DOTALL -- Makes . match newlins too. Like m//s
PCRE_EXTENDED -- Like m//x
PCRE_EXTRA --
PCRE_MULTILINE -- Like m//m
PCRE_UNGREEDY -- Set quantifiers to be ungreedy. Individual quantifiers
may be set to be greedy if they are followed by "?".
PCRE_UTF8 -- Work with UTF8 strings.
*/
// pcre_compile returns NULL on error, and sets pcreErrorOffset & pcreErrorStr
if(reCompiled == NULL) {
printf("ERROR: Could not compile '%s': %s\n", aStrRegex, pcreErrorStr);
exit(1);
} /* end if */
// Optimize the regex
pcreExtra = pcre_study(reCompiled, 0, &pcreErrorStr);
/* pcre_study() returns NULL for both errors and when it can not optimize the regex. The last argument is how one checks for
errors (it is NULL if everything works, and points to an error string otherwise. */
if(pcreErrorStr != NULL) {
printf("ERROR: Could not study '%s': %s\n", aStrRegex, pcreErrorStr);
exit(1);
}
/* Try to find the regex in aLineToMatch, and report results. */
pcreExecRet = pcre_exec(reCompiled,
pcreExtra,
str,
strlen(str), // length of string
0, // Start looking at this point
0, // OPTIONS
subStrVec,
30); // Length of subStrVec
/* pcre_exec OPTIONS (||'ed together) can be:
PCRE_ANCHORED -- can be turned on at this time.
PCRE_NOTBOL
PCRE_NOTEOL
PCRE_NOTEMPTY */
// Report what happened in the pcre_exec call..
//printf("pcre_exec return: %d\n", pcreExecRet);
if(pcreExecRet < 0) { // Something bad happened..
returnval = -1;
switch(pcreExecRet) {
case PCRE_ERROR_NOMATCH : log_message("String did not match the pattern"); break;
case PCRE_ERROR_NULL : log_message("Something was null"); break;
case PCRE_ERROR_BADOPTION : log_message("A bad option was passed"); break;
case PCRE_ERROR_BADMAGIC : log_message("Magic number bad (compiled re corrupt?)"); break;
case PCRE_ERROR_UNKNOWN_NODE : log_message("Something kooky in the compiled re"); break;
case PCRE_ERROR_NOMEMORY : log_message("Ran out of memory"); break;
default : log_message("Unknown error"); break;
} /* end switch */
} else {
//printf("Result: We have a match!\n");
// At this point, rc contains the number of substring matches found...
if(pcreExecRet == 0) {
pcreExecRet = 30 / 3;
} /* end if */
// PCRE contains a handy function to do the above for you:
for(j=0; j<pcreExecRet; j++) {
pcre_get_substring(str, subStrVec, pcreExecRet, j, &(psubStrMatchStr));
} /* end for */
returnval = atoi(psubStrMatchStr);
// Free up the substring
pcre_free_substring(psubStrMatchStr);
} /* end if/else */
// Free up the regular expression.
pcre_free(reCompiled);
// Free up the EXTRA PCRE value (may be NULL at this point)
if(pcreExtra != NULL) {
#ifdef PCRE_CONFIG_JIT
pcre_free_study(pcreExtra);
#else
pcre_free(pcreExtra);
#endif
}
return returnval;
}
-------------------Udp Listener---------------
while (1) {
n = recvfrom(sock_receiver,buf,2048,0,(struct sockaddr *)&from,&sender_length);
if (n < 0) error("recvfrom");
//write(1,"Received a datagram: ",21);
// printf("n = %d\n", n);
// printf("length of buffer = %lu\n", strlen(buf));
// write(1,buf,n);
strncpy(str, buf, n);
str[n] = '\0'; // IMPORTANT!
value = parse(str);
if(curTime != get_hour()){
//sprintf(temp_str,"Counter",minute,get_minute());
log_message("Counter reset");
reset_component_hits(components);
//reset_component_hits(dropped_msg);
curTime = get_hour();
}
if(value >=0 && components[value] < component_limit && value < max_component){
n = sendto(sock_forwarder,buf,n,0,(const struct sockaddr *)&sender,sender_length);
if (n < 0){
error("sendto");
}
components[value] = components[value]+1;
}else{
if(value < 0 ) {
error_msg = "Unable to parse component";
}else if(value >= max_component){
error_msg = "value not found in valid component list";
}else if(components[value] >= component_limit) {
error_msg = "Rate limit exceeded";
}else {
error_msg = "Message dropped for Unknown reason";
}
sprintf(temp_str,"[Component:%d] %s",value,error_msg);
log_message(temp_str);
bzero(temp_str,100);
bzero(error_msg,100);
}
// print_array(components);
// print_array(dropped_msg);
bzero(buf,2048);
bzero(str,2048);
}
I have a problem with the sqlite3_bind_text function when I try to use it for select.
The intention is to get the newest value in a 10 min time slot of my data.
If I use a prepared statement and bind my value the result is different compared to a normal string with SQL syntax.
The SQL syntax of the two tests 'should' be the same.
When the code runs I get the following output:
test 1 = 0.000000 AnalogRPM <-- Error
test 2 = 7.700000 7.69999980926514 <-- Correct value
It seems to me that my bound statement returns the name of the column instead of the value (as if the value is inserted as 'AnalogRPM'
Has anyone of you experienced anything similar? or can you see any faults in my code?
Any feedback is appreciated :)
char str[1000];
sqlite3_stmt *test1;
/** First test, use prepared statement to get double value */
snprintf(str, sizeof(str),
"select ? from DATA WHERE ts_sec BETWEEN ? AND ? ORDER BY rowid DESC LIMIT 1");
/** All 'rc' are check in my code, i just left them out to make it easier to read */
rc = sqlite3_prepare_v2(db_livedata, str, -1, &test1, 0);
if(rc != SQLITE_OK)
printf("SQL error on line:%d msg:%s \n",__LINE__, sqlite3_errmsg(db_livedata));
rc = sqlite3_bind_text( test1, 1, "AnalogRPM",-1,0);
rc = sqlite3_bind_int( test1, 2, stat_time.tv_sec - 600);
rc = sqlite3_bind_int( test1, 3, stat_time.tv_sec);
do
{
rc = sqlite3_step( test1);
switch( rc )
{
/** No more data */
case SQLITE_DONE:
break;
/** New data */
case SQLITE_ROW:
{
uint16_t size = sqlite3_column_count( test1);
if(size == 1) // should always be one
{
value = sqlite3_column_double( test1, 0);
printf("test 1 = %f %s\n",value, sqlite3_column_text(test1, 0));
}
}
break;
default:
break;
}
}while( rc==SQLITE_ROW );
/** Second test use normal string for prepare */
sqlite3_stmt *test2;
snprintf(str, sizeof(str),
"select AnalogRPM from DATA WHERE ts_sec BETWEEN %d AND %d ORDER BY rowid DESC LIMIT 1"
,stat_time.tv_sec - 600, stat_time.tv_sec);
rc = sqlite3_prepare_v2(db_livedata, str, -1, &test2, 0);
if(rc != SQLITE_OK)
printf("SQL error on line:%d msg:%s \n",__LINE__, sqlite3_errmsg(db_livedata));
do
{
rc = sqlite3_step( test2);
switch( rc )
{
/** No more data */
case SQLITE_DONE:
break;
/** New data */
case SQLITE_ROW:
{
uint16_t size = sqlite3_column_count( test2);
if(size == 1)
{
value = sqlite3_column_double( test2, 0);
printf("test 2 = %f %s\n",value, sqlite3_column_text(test2, 0));
}
}
break;
default:
break;
}
}while( rc==SQLITE_ROW );
First version is essentially
SELECT 'AnalogRPM' ...
while the second is
SELECT AnalogRPM ...
The difference is the expression being either a string literal or a column name.
You cannot use variable binding for column names. Column names need to be known at SQL statement compile time.
I'm having an incomprehensible problem in Scicoslab in the last few days.
I've been writing some communication blocks (to send data to an external application) for scicos in C and then wrap them with it's own code. The problem is that, even if the code is working (I've checked every output possible), scicos gives me this error message: sicosim: error. Type 0 not yet supported by outtb. Error Screenshot
Here is the code for the c function of the Sensor Dispatcher Block:
int bro_sens_send (scicos_block *block)
{
int rc, i;
bro_fist_t packet[BUFFER_SIZE];
for (i = 1; i < block->nin; i++) {
bro_encode_sci_datablock(&packet[i-1], block->inptr[i]);
};
printf ("Data for first block: %i, %i, %.2f\n", packet[0].port, packet[0].operation, packet[0].data);
rc = send(block->inptr[0][0], packet, sizeof(bro_fist_t) * BUFFER_SIZE, 0);
if (rc < 0)
{
perror("send() failed");
return -1;
}
printf("%d bytes of data were sent\n", rc);
return 0;
}
int bro_sens_read (scicos_block *block)
{
int rc, i;
bro_fist_t packet[BUFFER_SIZE];
rc = recv(block->inptr[0][0], packet, sizeof(bro_fist_t) * BUFFER_SIZE, 0);
printf("%d bytes of data were received\n", rc);
if (rc < 0)
{
perror("recv() failed");
return -1;
}
printf("Starting to set outputs :3 [%i]\n", block->nout);
for (i = 0; i < block->nout; i++) {
printf("Next Step defining outputs :D [%i]\n", i);
bro_decode_sci_datablock(&packet[i], &block->outptr[i][0]);
printf("Output value for port %i is: %.2f[%i]\n", i, block->outptr[i][0], block->outsz[(2*block->nout)+i]);
}
return 0;
}
void bro_comm_sens_disp (scicos_block *block, int flag)
{
switch (flag) {
case 1: /* set output */
bro_sens_send(block);
bro_sens_read(block);
break;
case 2: /* get input */
break;
case 4: /* initialisation */
break;
case 5: /* ending */
break;
default:
break;
}
}
And this is the code for the block definition (In scilab code):
function [x,y,typ] = SENS_Disp(job,arg1,arg2)
x=[];y=[];typ=[];
select job
case 'plot' then
exprs=arg1.graphics.exprs;
standard_draw(arg1)
case 'getinputs' then
[x,y,typ]=standard_inputs(arg1)
case 'getoutputs' then
[x,y,typ]=standard_outputs(arg1)
case 'getorigin' then
[x,y]=standard_origin(arg1)
case 'set' then
x=arg1
model=arg1.model;graphics=arg1.graphics;
exprs=graphics.exprs;
case 'define' then
model = scicos_model()
model.sim = list('bro_comm_sens_disp',4)
model.out = [1;1;1;1;1;1;1]
model.out2 = [1;1;1;1;1;1;1]
model.outtyp= [1;1;1;1;1;1;1]
model.in = [1;3;3;3;3;3;3;3]
model.in2 = [1;1;1;1;1;1;1;1]
model.intyp = [3;1;1;1;1;1;1;1]
model.evtin = []
model.rpar = []
model.ipar = []
model.dstate=[1];
model.blocktype='c'
model.dep_ut=[%t %f]
exprs=[]
gr_i=['xstringb(orig(1),orig(2),[''Sensors'';+''Dispatcher''],sz(1),sz(2),''fill'');']
x=standard_define([3 2],model,exprs,gr_i)
end
endfunction
The first inpput port for the block is the socket descriptor for communications while the other seven are connected to setup blocks. The outputs returns the data received from the external application.
I've tried to navigate through Scilab code and I've understood that the error I'm getting tells me that the type of data has been set wrongly, but I've checked and it's not quite the case.
Here is the code of Scicoslab that outputs the error:
/*set vectors of outtb*/
for (j=0; j<nlnk; j++) { /*for each link*/
subheader=(int *)(listentry(il_state_outtb,j+1)); /*get header of outtbl(j+1)*/
outtbsz[j]=subheader[1]; /*store dimensions*/
outtbsz[j+nlnk]=subheader[2];
switch (subheader[0]) { /*store type and address*/
/*matrix of double*/
case 1 :
switch (subheader[3]) {
case 0 :
outtbtyp[j]=SCSREAL_N; /*double real matrix*/
outtbptr[j]=(SCSREAL_COP *)(subheader+4);
break;
case 1 :
outtbtyp[j]=SCSCOMPLEX_N; /*double complex matrix*/
outtbptr[j]=(SCSCOMPLEX_COP *)(subheader+4);
break;
default :
Scierror(888,\
"%s : error. Type %d of double scalar matrix not yet supported "
"for outtb.\n",\
fname,subheader[3]);
FREE(outtbptr);
FREE(outtbtyp);
FREE(outtbsz);
FREE(opar);
FREE(oparsz);
FREE(opartyp);
FREE(oz);
FREE(ozsz);
FREE(oztyp);
FREE(lfunpt);
freeparam;
FREE(outtb_elem);
break;
}
break;
/*matrix of integers*/
case 8 :
switch (subheader[3]) {
case 1 :
outtbtyp[j]=SCSINT8_N; /*int8*/
outtbptr[j]=(SCSINT8_COP *)(subheader+4);
break;
case 2 :
outtbtyp[j]=SCSINT16_N; /*int16*/
outtbptr[j]=(SCSINT16_COP *)(subheader+4);
break;
case 4 :
outtbtyp[j]=SCSINT32_N; /*int32*/
outtbptr[j]=(SCSINT32_COP *)(subheader+4);
break;
case 11 :
outtbtyp[j]=SCSUINT8_N; /*uint8*/
outtbptr[j]=(SCSUINT8_COP *)(subheader+4);
break;
case 12 :
outtbtyp[j]=SCSUINT16_N; /*uint16*/
outtbptr[j]=(SCSUINT16_COP *)(subheader+4);
break;
case 14 :
outtbtyp[j]=SCSUINT32_N; /*uint32*/
outtbptr[j]=(SCSUINT32_COP *)(subheader+4);
break;
default :
Scierror(888,\
"%s : error. Type %d of integer scalar matrix not yet supported "
"for outtb.\n",\
fname,subheader[3]);
FREE(outtbptr);
FREE(outtbtyp);
FREE(outtbsz);
FREE(opar);
FREE(oparsz);
FREE(opartyp);
FREE(oz);
FREE(ozsz);
FREE(oztyp);
FREE(lfunpt);
freeparam;
FREE(outtb_elem);
break;
}
break;
default :
Scierror(888,"%s : error. Type %d not yet supported for outtb.\n",fname,subheader[0]);
FREE(outtbptr);
FREE(outtbtyp);
FREE(outtbsz);
FREE(opar);
FREE(oparsz);
FREE(opartyp);
FREE(oz);
FREE(ozsz);
FREE(oztyp);
FREE(lfunpt);
freeparam;
FREE(outtb_elem);
return 0;
break;
}
Ok. After a lot of time I've solved the problem. There was a function that was setting a value in a wrong memory space. The problem was that there weren't any error messages nor warnings.
The problem was in a function that did this:
block->outptr[0][3] = 0;
In a block that had only 3 output ports. This wrote the wrong data inside the data registry. After I removed it everything works fine.