Post by szotyi on Array of Program
CODESYS Forge
talk
(Post)
Hi! What is the proper and the easiest way to call programs through looping of array of programs, or pointer of program? instead of calling the programs one by one: prog1(inputX: 11); prog2(inputY: 12); prog3(inputZ: 13); similar like: VAR programsToCall : ARRAY [1..3] OF PROGRAM; END_VAR programsToCall[1] := prog1(inputX: 11); programsToCall[2] := prog2(inputY: 12); programsToCall[3] := prog3(inputZ: 13); FOR i := 1 TO 3 DO programsToCall[i] (); END_FOR Thanks.
Last updated: 2024-08-28
Post by andrebrandt on FB string and naming
CODESYS Forge
talk
(Post)
Hi all. I have a FB written in ST. FUNCTION_BLOCK NTC10k VAR_INPUT Syst:STRING; In:REAL; END_VAR VAR_OUTPUT Out:REAL; OTag:STRING; Out_St:Struct_NTC10K; END_VAR VAR Tag:STRING; InstanceName: STRING; Structure:Struct_NTC10K; END_VAR What i'm trying to do, is to pass data to structure. In structure i want to add a tag with systemnumber and sensorname. '320.001-RT401' In pou i use this RT401: NTC10k;, and tried this RT401: NTC10k(Tag:='RT401'); Anyone done this?
Last updated: 2024-09-26
Post by otbeka on CmpCrypto CryptoGenerateHash Not Outputting
CODESYS Forge
talk
(Post)
Hi, I have been trying to use CryptoGenerateHash from the CmpCrypto Implementation library. My code is taken almost directly from the CryptoDemo.project example provided on Codesys Forge, yet the CryptoGenerateHash function does not write to the address listed in pHash. RTS_IEC_RESULT is OK, but I am getting nothing out of the function. No errors either, all my libraries are up to date. Any help would be appreicated! PROGRAM PLC_PRG VAR sMessage : MESSAGE := 'The red fox runs across the ice'; abyHashCode : HASH_CODE := [ 16#52, 16#ED, 16#87, 16#9E, 16#70, 16#F7, 16#1D, 16#92, 16#6E, 16#B6, 16#95, 16#70, 16#08, 16#E0, 16#3C, 16#E4, 16#CA, 16#69, 16#45, 16#D3 ]; xMessageOK : BOOL; END_VAR xMessageOK := CheckMessage(sMessage, abyHashCode); FUNCTION CheckMessage : BOOL VAR_INPUT sMessage : REFERENCE TO MESSAGE; abyHashCode : REFERENCE TO HASH_CODE; END_VAR VAR _hHASH : RTS_IEC_HANDLE := CryptoGetAlgorithmById(ui32CryptoID:=RtsCryptoID.HASH_SHA1, pResult:=0); Result : RTS_IEC_RESULT; bsMessage : RtsByteString := (ui32MaxLen:=SIZEOF(sMessage), pByData:=ADR(sMessage), ui32Len:=TO_UDINT(LEN(sMessage))); abyNewHashCode : HASH_CODE; bsNewHashCode : RtsByteString := (ui32MaxLen:=SIZEOF(abyNewHashCode), pByData:=ADR(abyNewHashCode)); diCmpResult : DINT; END_VAR Result := CryptoGenerateHash(hAlgo:=_hHASH, pData:=ADR(bsMessage), pHash:=ADR(bsNewHashCode)); diCmpResult := SysMemCmp(pBuffer1:=ADR(abyHashCode), pBuffer2:=ADR(abyNewHashCode), udiCount:=SIZEOF(HASH_CODE)); CheckMessage := diCmpResult = 0;
Last updated: 2024-09-06
Post by salvadegianluca on List files in a directory with SysDiropen And SysDirRead
CODESYS Forge
talk
(Post)
Good morning; I'm facing an issue that seems being caused by the library itself. I'm trying to create the list of all the files that are stored inside a psecific directory. Unluckily the first file is allways hidden. VAR CONSTANT arysEmptyArray:ARRAY[iArrayMinDimension..iArrayMAXDimension] OF STRING := [(iArrayMAXDimension - iArrayMinDimension)('')]; iArrayMinDimension:INT := 1; iArrayMAXDimension:INT := 99; END_VAR VAR_INPUT sDirectoryToInspect:STRING; sSearchFilter:STRING; END_VAR VAR_IN_OUT xCheckFileInsideDirectory:BOOL; END_VAR VAR arysListOfFoundFiles:ARRAY[iArrayMinDimension..iArrayMAXDimension] OF STRING; sNullString:STRING; iNullIndex:INT := 0; libInfoAboutThePath:DirInfo; libResultOfThePathMonitoring:CmpErrors.RTS_IEC_RESULT; libInstanceToMonitorThePath:CmpErrors.RTS_IEC_HANDLE; sEntityToSearch:STRING; dMaxEntityDimension:DINT := SIZEOF(sEntityToSearch); libFileInfo:DirInfo; sFilteredFileName:STRING; iIndexToScrollArrays:INT; END_VAR IF xCheckFileInsideDirectory THEN arysListOfFoundFiles:=arysEmptyArray; iIndexToScrollArrays:=iArrayMinDimension; libInstanceToMonitorThePath:= SysDirOpen(szDir:= sDirectoryToInspect,szDirEntry:=sNullString,diMaxDirEntry:= iNullIndex,pDirInfo:= ADR(libInfoAboutThePath),pResult:= ADR(libResultOfThePathMonitoring)); WHILE libResultOfThePathMonitoring = Errors.ERR_OK AND iIndexToScrollArrays <= iArrayMAXDimension DO sEntityToSearch:= ''; libResultOfThePathMonitoring:=SysDirRead(hDir:=libInstanceToMonitorThePath,szDirEntry:=sEntityToSearch,diMaxDirEntry:=dMaxEntityDimension,pDirInfo:=ADR(libFileInfo)); sFilteredFileName:= FUN_06_00_FindItemInString_0(sFilter:=sSearchFilter,sSource:=sEntityToSearch); IF sFilteredFileName <> '' THEN arysListOfFoundFiles[iIndexToScrollArrays]:=sFilteredFileName; iIndexToScrollArrays:=iIndexToScrollArrays + 1; END_IF IF libResultOfThePathMonitoring <> Errors.ERR_OK THEN libResultOfThePathMonitoring:= Errors.ERR_OK; END_IF END_WHILE libResultOfThePathMonitoring:=SysDirClose(hDir:= libInstanceToMonitorThePath); xCheckFileInsideDirectory:=FALSE; END_IF How is possible to solve this issue? Any known work around?
Last updated: 2024-09-17
Post by salvadegianluca on List files in a directory with SysDiropen And SysDirRead
CODESYS Forge
talk
(Post)
Good morning; I'm facing an issue that seems being caused by the library itself. I'm trying to create the list of all the files that are stored inside a psecific directory. Unluckily the first file is allways hidden. VAR CONSTANT arysEmptyArray:ARRAY[iArrayMinDimension..iArrayMAXDimension] OF STRING := [(iArrayMAXDimension - iArrayMinDimension)('')]; iArrayMinDimension:INT := 1; iArrayMAXDimension:INT := 99; END_VAR VAR_INPUT sDirectoryToInspect:STRING; sSearchFilter:STRING; END_VAR VAR_IN_OUT xCheckFileInsideDirectory:BOOL; END_VAR VAR arysListOfFoundFiles:ARRAY[iArrayMinDimension..iArrayMAXDimension] OF STRING; sNullString:STRING; iNullIndex:INT := 0; libInfoAboutThePath:DirInfo; libResultOfThePathMonitoring:CmpErrors.RTS_IEC_RESULT; libInstanceToMonitorThePath:CmpErrors.RTS_IEC_HANDLE; sEntityToSearch:STRING; dMaxEntityDimension:DINT := SIZEOF(sEntityToSearch); libFileInfo:DirInfo; sFilteredFileName:STRING; iIndexToScrollArrays:INT; END_VAR IF xCheckFileInsideDirectory THEN arysListOfFoundFiles:=arysEmptyArray; iIndexToScrollArrays:=iArrayMinDimension; libInstanceToMonitorThePath:= SysDirOpen(szDir:= sDirectoryToInspect,szDirEntry:=sNullString,diMaxDirEntry:= iNullIndex,pDirInfo:= ADR(libInfoAboutThePath),pResult:= ADR(libResultOfThePathMonitoring)); WHILE libResultOfThePathMonitoring = Errors.ERR_OK AND iIndexToScrollArrays <= iArrayMAXDimension DO sEntityToSearch:= ''; libResultOfThePathMonitoring:=SysDirRead(hDir:=libInstanceToMonitorThePath,szDirEntry:=sEntityToSearch,diMaxDirEntry:=dMaxEntityDimension,pDirInfo:=ADR(libFileInfo)); sFilteredFileName:= FUN_06_00_FindItemInString_0(sFilter:=sSearchFilter,sSource:=sEntityToSearch); IF sFilteredFileName <> '' THEN arysListOfFoundFiles[iIndexToScrollArrays]:=sFilteredFileName; iIndexToScrollArrays:=iIndexToScrollArrays + 1; END_IF IF libResultOfThePathMonitoring <> Errors.ERR_OK THEN libResultOfThePathMonitoring:= Errors.ERR_OK; END_IF END_WHILE libResultOfThePathMonitoring:=SysDirClose(hDir:= libInstanceToMonitorThePath); xCheckFileInsideDirectory:=FALSE; END_IF How is possible to solve this issue? Any known work around?
Last updated: 2024-09-17
Post by salvadegianluca on List files in a directory with SysDiropen And SysDirRead
CODESYS Forge
talk
(Post)
Good morning; I'm facing an issue that seems being caused by the library itself. I'm trying to create the list of all the files that are stored inside a psecific directory. Unluckily the first file is allways hidden. VAR CONSTANT arysEmptyArray:ARRAY[iArrayMinDimension..iArrayMAXDimension] OF STRING := [(iArrayMAXDimension - iArrayMinDimension)('')]; iArrayMinDimension:INT := 1; iArrayMAXDimension:INT := 99; END_VAR VAR_INPUT sDirectoryToInspect:STRING; sSearchFilter:STRING; END_VAR VAR_IN_OUT xCheckFileInsideDirectory:BOOL; END_VAR VAR arysListOfFoundFiles:ARRAY[iArrayMinDimension..iArrayMAXDimension] OF STRING; sNullString:STRING; iNullIndex:INT := 0; libInfoAboutThePath:DirInfo; libResultOfThePathMonitoring:CmpErrors.RTS_IEC_RESULT; libInstanceToMonitorThePath:CmpErrors.RTS_IEC_HANDLE; sEntityToSearch:STRING; dMaxEntityDimension:DINT := SIZEOF(sEntityToSearch); libFileInfo:DirInfo; sFilteredFileName:STRING; iIndexToScrollArrays:INT; END_VAR IF xCheckFileInsideDirectory THEN arysListOfFoundFiles:=arysEmptyArray; iIndexToScrollArrays:=iArrayMinDimension; libInstanceToMonitorThePath:= SysDirOpen(szDir:= sDirectoryToInspect,szDirEntry:=sNullString,diMaxDirEntry:= iNullIndex,pDirInfo:= ADR(libInfoAboutThePath),pResult:= ADR(libResultOfThePathMonitoring)); WHILE libResultOfThePathMonitoring = Errors.ERR_OK AND iIndexToScrollArrays <= iArrayMAXDimension DO sEntityToSearch:= ''; libResultOfThePathMonitoring:=SysDirRead(hDir:=libInstanceToMonitorThePath,szDirEntry:=sEntityToSearch,diMaxDirEntry:=dMaxEntityDimension,pDirInfo:=ADR(libFileInfo)); sFilteredFileName:= FUN_06_00_FindItemInString_0(sFilter:=sSearchFilter,sSource:=sEntityToSearch); IF sFilteredFileName <> '' THEN arysListOfFoundFiles[iIndexToScrollArrays]:=sFilteredFileName; iIndexToScrollArrays:=iIndexToScrollArrays + 1; END_IF IF libResultOfThePathMonitoring <> Errors.ERR_OK THEN libResultOfThePathMonitoring:= Errors.ERR_OK; END_IF END_WHILE libResultOfThePathMonitoring:=SysDirClose(hDir:= libInstanceToMonitorThePath); xCheckFileInsideDirectory:=FALSE; END_IF How is possible to solve this issue? Any known work around?
Last updated: 2024-09-17
Post by salvadegianluca on List files in a directory with SysDiropen And SysDirRead
CODESYS Forge
talk
(Post)
Good morning; I'm facing an issue that seems being caused by the library itself. I'm trying to create the list of all the files that are stored inside a psecific directory. Unluckily the first file is allways hidden. VAR CONSTANT arysEmptyArray:ARRAY[iArrayMinDimension..iArrayMAXDimension] OF STRING := [(iArrayMAXDimension - iArrayMinDimension)('')]; iArrayMinDimension:INT := 1; iArrayMAXDimension:INT := 99; END_VAR VAR_INPUT sDirectoryToInspect:STRING; sSearchFilter:STRING; END_VAR VAR_IN_OUT xCheckFileInsideDirectory:BOOL; END_VAR VAR arysListOfFoundFiles:ARRAY[iArrayMinDimension..iArrayMAXDimension] OF STRING; sNullString:STRING; iNullIndex:INT := 0; libInfoAboutThePath:DirInfo; libResultOfThePathMonitoring:CmpErrors.RTS_IEC_RESULT; libInstanceToMonitorThePath:CmpErrors.RTS_IEC_HANDLE; sEntityToSearch:STRING; dMaxEntityDimension:DINT := SIZEOF(sEntityToSearch); libFileInfo:DirInfo; sFilteredFileName:STRING; iIndexToScrollArrays:INT; END_VAR IF xCheckFileInsideDirectory THEN arysListOfFoundFiles:=arysEmptyArray; iIndexToScrollArrays:=iArrayMinDimension; libInstanceToMonitorThePath:= SysDirOpen(szDir:= sDirectoryToInspect,szDirEntry:=sNullString,diMaxDirEntry:= iNullIndex,pDirInfo:= ADR(libInfoAboutThePath),pResult:= ADR(libResultOfThePathMonitoring)); WHILE libResultOfThePathMonitoring = Errors.ERR_OK AND iIndexToScrollArrays <= iArrayMAXDimension DO sEntityToSearch:= ''; libResultOfThePathMonitoring:=SysDirRead(hDir:=libInstanceToMonitorThePath,szDirEntry:=sEntityToSearch,diMaxDirEntry:=dMaxEntityDimension,pDirInfo:=ADR(libFileInfo)); sFilteredFileName:= FUN_06_00_FindItemInString_0(sFilter:=sSearchFilter,sSource:=sEntityToSearch); IF sFilteredFileName <> '' THEN arysListOfFoundFiles[iIndexToScrollArrays]:=sFilteredFileName; iIndexToScrollArrays:=iIndexToScrollArrays + 1; END_IF IF libResultOfThePathMonitoring <> Errors.ERR_OK THEN libResultOfThePathMonitoring:= Errors.ERR_OK; END_IF END_WHILE libResultOfThePathMonitoring:=SysDirClose(hDir:= libInstanceToMonitorThePath); xCheckFileInsideDirectory:=FALSE; END_IF How is possible to solve this issue? Any known work around?
Last updated: 2024-09-17
Post by ihatemaryfisher on Sorting array of any-sized structure
CODESYS Forge
talk
(Post)
In my machine's operation, I need to display multiples tables containing arrays of structured variables. The arrays change during operation, and my supervisor has advised me to write a new bubble-sort for each array. I think I can make a function to sort an array of any data type. This was my own project, and I'm a relatively new coder. I want to know the weaknesses in my approach, and a better method, if one exists. As far as I can test, the function accepts an array of a structured variable of any size, and sort it by any VAR in that structure. But it relies heavily on pointers, which I've heard are bad practice? Function call: // SORT BY BYTE-SIZED VAR IF xDoIt[6] THEN FUNBubbleSortSansBuffer( IN_pbySourcePointer := ADR(astArray[1]), // address of first byte in first element of array IN_pbyComparePointer:= ADR(astArray[1].byCompByte), // points to first byte of the comparing variable (variable you sort by) IN_uiStructureSize := SIZEOF(TYPE_STRUCTURE), // size, in bytes, of the structured variable IN_uiCompareSize := SIZEOF(astArray[1].byCompByte), // size, in bytes, of the comparing variable (variable you sort by) diArrayElements := UPPER_BOUND(astArray,1), // number of elements in array IN_xSmallToLarge := xSortOrder // whether to sort by small2large or large2small ); END_IF Function: FUNCTION FUNBubbleSortSansBuffer : BOOL VAR_INPUT IN_pbySourcePointer : POINTER TO BYTE; // points to beginning of array (first byte of first element) IN_pbyComparePointer: POINTER TO BYTE; // points to first byte of the comparing variable (variable you sort by) IN_uiStructureSize : UINT; // size, in bytes, of the structured variable IN_uiCompareSize : UINT; // size, in bytes, of the comparing variable (variable you sort by) diArrayElements : DINT; // number of elements in array IN_xSmallToLarge : BOOL; // whether to sort by small2large or large2small END_VAR VAR j : DINT; // repeat iteration over array until array ends i : DINT; // iterarte over array, swapping when necesary k : DINT; // iterator from 1 to size of structure (stepping 'through' a single element in array) dwSize : DWORD; // internal var for use in MEMUtils.MemCpy(<size>) // FOR SORTING BY BYTE VAR pbySourcePointer : POINTER TO BYTE; pbySourcePointer2 : POINTER TO BYTE; pbyComparePointer : POINTER TO BYTE; pbyComparePointer2 : POINTER TO BYTE; pbyPointerToBuffer : POINTER TO BYTE; // pointer to single byte buffer byBufferByte : BYTE; // single byte buffer END_VAR dwSize := UINT_TO_DWORD(IN_uiStructureSize); // get structure size (number of bytes) pbyPointerToBuffer := ADR(byBufferByte); // assign pointer to address of buffer byte (because MEMUtils.MemCpy requires a pointer input) CASE IN_uiCompareSize OF // depending on the size of the VAR to sort by (current functionality for BYTE and WORD/INT 1: // BYTE (8 BIT) FOR j := 1 TO diArrayElements DO // for number of elements in array FOR i := 1 TO (diArrayElements-1) DO // same thing, but row[i+1] row is included in swap logic pbySourcePointer := IN_pbySourcePointer + dwSize*(i-1); // point at #1 byte in array element[i] pbySourcePointer2 := pbySourcePointer + dwSize; // point at #1 byte in array element[i+1] // NOTE: because of memory locations, each array element is offset from one another by a number of bytes equal to the size of the structure // We can "walk" from array[i] to array[i+1] via steps equal to the size of the structure // e.g., ADR(array[i+1]) == ADR(array[i]) + SIZEOF([array datatype]) pbyComparePointer := IN_pbyComparePointer + dwSize*(i-1); // point to sorting variable in array element[i] pbyComparePointer2 := pbyComparePointer + dwSize; // point to sorting variable in array element[i+1] // using sort order (small -> large/large -> small) IF SEL(IN_xSmallToLarge, (pbyComparePointer2^ > pbyComparePointer^),(pbyComparePointer2^ < pbyComparePointer^)) THEN // This is where it gets tricky. We've identified pointers for the starting bytes of aArray[i] and aArray[i+1] // and we know the size of aArray[i]. We are going to swap individual bytes, one at a time, from aArray[i] and aArray[i+1] // this allows us to use only a single byte var as a buffer or temporary data storage // e.g., consider a structure consisting of a word, a byte, and a string. it is stored like this // |------WORD-------| |--BYTE-| |STRING------...| // astArray[1] == 1000 0100 0010 0001 1100 0011 1010 1010.... etc // astArray[2] == 0001 0010 0100 1000 0011 1100 0101 0101.... etc // performing a single swap (copy into a buffer, etc.) of the first byte of each array element creates this // astArray[1] == 0001 0100 0010 0001 1100 0011 1010 1010.... etc // astArray[2] == 1000 0010 0100 1000 0011 1100 0101 0101.... etc // incrementing the pointer adresses for the swap by 1 and swapping again swaps the next byte in each array element // astArray[1] == 0001 0010 0010 0001 1100 0011 1010 1010.... etc // astArray[2] == 1000 0100 0100 1000 0011 1100 0101 0101.... etc // continuing this from k to SIZEOF(TYPE_STRUCTURE) results in a toally swapped row FOR k := 1 TO IN_uiStructureSize DO // copy single byte[k] of array element 1 to buffer MEMUtils.MemCpy(pbyDest := (pbyPointerToBuffer), pbySrc := (pbySourcePointer+k-1), dwSize := 1); // copy single byte[k] of array element 2 to 1 MEMUtils.MemCpy(pbyDest := pbySourcePointer+k-1, pbySrc := (pbySourcePointer2+k-1), dwSize := 1); // copy buffer to byte[k] array element 2 MEMUtils.MemCpy(pbyDest := (pbySourcePointer2+k-1), pbySrc := pbyPointerToBuffer, dwSize := 1); END_FOR END_IF END_FOR END_FOR
Last updated: 2023-08-17
Post by ton on How to create a stopwatch?
CODESYS Forge
talk
(Post)
One i wrote this to measure elepse time When xMeasure is true is starts en when false it stops and time is messured. FUNCTION_BLOCK FB_ElapseTime VAR_INPUT xMeasure: BOOL; END_VAR VAR_OUTPUT xRisingEdge: BOOL; xFallingEdge: BOOL; tElapsed: TIME; ltElapsed: LTIME; ltPrev_Elapsed: LTIME; ltElapsedMax: LTIME; END_VAR VAR xLastValue: BOOL; LTIMEStart: LTIME; LTIMEEnd: LTIME; tonReset: TON:= (IN:= TRUE, PT:= TIME#30S0MS); END_VAR ------------------------------------------- xRisingEdge:= (xLastValue XOR xMeasure) AND xMeasure; xFallingEdge:= (xLastValue XOR xMeasure) AND NOT xMeasure; IF xRisingEdge THEN ltPrev_Elapsed:= ltElapsed; LTIMEStart:= LTIME(); END_IF IF xMeasure OR xFallingEdge THEN LTIMEEnd:= LTIME(); END_IF ltElapsed:= LTIMEEnd - LTIMEStart; ltElapsedMax:= MAX(ltElapsedMax, ltElapsed); tElapsed:= LTIME_TO_TIME(ltElapsed); xLastValue:= xMeasure; tonReset(); IF tonReset.Q THEN tonReset.IN:= FALSE; ltElapsedMax:= LTIME#0NS; END_IF Meaby this will help.
Last updated: 2023-12-09
Post by kevinl on RevPi Connect RS485
CODESYS Forge
talk
(Post)
I figured this out on my own with this logic: usually the Serial port on a Raspberry pi is on /dev/ttyUSB0 then you need to set linux.DeviceFile=/dev/ttyUSB in codesys you then must set com port to 1 so i assumed codesys subtracts 1 from the number and adds this to the filename... (in code it would look like this: var comPort : byte := '1'; linuxDevice : string := '/dev/ttyUSB'; filename : string; end_var filename := concat(linuxDevice, (to_string(comPort-1)); filaname then is /dev/ttyUSB0 RevPi uses ttyRS485 as Serial port so you must remove the last number from the name and set the com port to this number +1 i hope this is understandable ;-)
Last updated: 2023-12-11
Post by struccc on Release SP20 - Changes in behaviour?
CODESYS Forge
talk
(Post)
Dear all, I've just started to migrate some of my ancient projects to SP20. There is one strange error (?) I have noticed so far. In a method call, depending on the circumstances I would like to return reference to an object, or an invalid reference: METHOD Add_EVT_OUT : REFERENCE TO FB_MSG VAR END_VAR IF __ISVALIDREF(refMSG_Entry) THEN Add_EVT_OUT REF= MANAGER.AddMsg_EVT_OUT( refMSG_Entry, _Get_EVT_Message(MSG_EVENT.OUT), _Get_EVT_AddCode(MSG_EVENT.OUT) )^; ELSE Add_EVT_OUT := 0; END_IF So far setting a reference variable to 0, did this. But now, the expression Add_EVT_OUT := 0; gives an error: [ERROR] DB_WTP_370: Add_EVT_ACK MSG_TRIGGER_EXT: C0032: Cannot convert type 'BIT' to type 'REFERENCE TO FB_MSG' Naturally... I can write: Add_EVT_OUT := DWORD#0; But is this the correct way? Is there any constant I could use instead, like "NULL"? Or this is totally wrong and to be avoided?
Last updated: 2024-03-24
Post by paro on Modbus Client Request Not Processed
CODESYS Forge
talk
(Post)
Hi, works in my case if I increase the timeout! to_udint(t#100ms) -> 100 -> 100us.. FUNCTION_BLOCK MODBUS_master_example_ST VAR initDone : BOOL := FALSE; aIPAddress : ARRAY [0..3] OF BYTE := [127,0,0,1]; clientTcp: ModbusFB.ClientTcp; // buffer to read input registers aDataInputRegisters : ARRAY[0..9] OF UINT; // some client requests clientRequestReadInputRegisters: ModbusFB.ClientRequestReadInputRegisters; xExecute: BOOL; uistart: UINT := 100; udiTimeout1: UDINT; END_VAR IF NOT initDone THEN initDone := TRUE; // configure clientTcp clientTcp(aIPaddr:=aIPAddress, uiPort:=502, udiLogOptions := ModbusFB.LoggingOptions.All); // configure clientRequestReadInputRegisters clientRequestReadInputRegisters(rClient:=clientTcp, uiUnitId:=1, udiTimeout:=1000000); // 1sec END_IF // call the client FB's clientTcp(); clientRequestReadInputRegisters(rClient:=clientTcp,xExecute := xExecute AND NOT clientRequestReadInputRegisters.xBusy ,uiStartItem:=uistart, uiQuantity:=3, pData:=ADR(aDataInputRegisters[0]));
Last updated: 2024-05-30
Post by zer0g on Modbus Client Request Not Processed
CODESYS Forge
talk
(Post)
I'm using the code bellow which is based on the Codesys example: FUNCTION_BLOCK MODBUS_master_example_ST VAR initDone : BOOL := FALSE; aIPAddress : ARRAY [0..3] OF BYTE := [127,0,0,1]; clientTcp: ModbusFB.ClientTcp; // buffer to read input registers aDataInputRegisters : ARRAY[0..9] OF UINT; // some client requests clientRequestReadInputRegisters: ModbusFB.ClientRequestReadInputRegisters; xExecute: BOOL; END_VAR IF NOT initDone THEN initDone := TRUE; // configure clientTcp clientTcp(aIPaddr:=aIPAddress, uiPort:=502, udiLogOptions := ModbusFB.LoggingOptions.All); // configure clientRequestReadInputRegisters clientRequestReadInputRegisters(rClient:=clientTcp, uiUnitId:=1, udiTimeout:=TO_UDINT(T#1000MS)); END_IF // call the client FB's clientTcp(); clientRequestReadInputRegisters(rClient:=clientTcp,xExecute := xExecute AND NOT clientRequestReadInputRegisters.xBusy ,uiStartItem:=2, uiQuantity:=3, pData:=ADR(aDataInputRegisters[0])); As you can see the clientTCP is called cyclically with the same result.
Last updated: 2024-05-30
Post by installwhat on C0077 on one machine but not another
CODESYS Forge
talk
(Post)
I have a version of codesys 3.5.16 on my local machine and a vm. The local machine has loads of codesys versions and libraries installed etc. I can open the FPosCR_Example_Project_SP16_patch1 project on my vm without any errors. On the local machine I get the error C0077: unknown type IoDrvEthercatLib.ETC_CO_SdoWrite. I can, on the local machine, declare a var of type IoDrvEthercatLib.ETC_CO_SdoWrite and include it in the code without adding an extra error. The problem only seems to affect the library. This is seems more an issue with my codesys set up rather than something with festo. I would like to know the possible causes. I understand most of the library manager features but it's not clear how to find the cause. Thanks
Last updated: 2024-06-11
Post by joschi2804 on Licensing info not available.
CODESYS Forge
talk
(Post)
Hi, maybe this fixes your problem. At least this was our issue back then. We tried to run the codesys runtime environment on an embedded Linux system. The problem on our side was that we changed the WORKDIR directory in the /etc/init.d/codesyscontrol script to /home/user/codesys, but we forgot to copy all files (including the hidden files) from the initial WORKDIR (I am not sure but I guess the initial WORKDIR was /var/opt/codesys) to the new one. After copying those files and restarting codemeter and codesyscontrol, we were able to upload licenses. Hope this help BR Josef
Last updated: 2024-07-10
Post by ucconversions on displaying all incoming CAN bus messages
CODESYS Forge
talk
(Post)
Hello Im using CAN API Im receiving CAN messages with no problem. below is my code. METHOD ProcessMessage VAR_IN_OUT ( Incoming message ) Message : CAN.RxMESSAGE; END_VAR VAR i: USINT; END_VAR recvCANid := Message.udiCanID; IF recvCANid = 16#18FF8247 THEN UserVarGlobal.g_countMsg_RPMset := UserVarGlobal.g_countMsg_RPMset + 1; FOR i := 0 TO 7 DO myDataFF82[i] := Message.abyData[i]; END_FOR END_IF but in this way I can only display CAN messages which I know its ID. I want to display all incoming CAN messages for example first incoming CAN message - I assign it into a variable Message1. second incoming CAN message - I assign it into a variable Message2. third incoming CAN message - I assign it into a variable Message3. Can you give me guidance? Thank you BR
Last updated: 2024-07-18
Post by askic on Generate FBs from source
CODESYS Forge
talk
(Post)
Hello, I'm coming from Siemens (TIA) world and currently learning Codesys. I'd like to know if there is an option to add external txt file with ST code for creation of a function block and then use this file as a source file from which FB will be generated? For example, in TIA, there is an option add external source file to the project structure and then use option "Generate blocks from source". This would create a FB. Does Codesys have something similar? This external source file would look like this: FUNCTION_BLOCK Scaling VAR_INPUT x, k, n : REAL; END_VAR VAR_OUTPUT y : REAL; END_VAR VAR END_VAR y := k*x+n; END_FUNCTION_BLOCK
Last updated: 2024-07-31
Post by alimans on OPC-UA Server, Symbol Set: Raise an error: Object reference not set to an instance of an object.
CODESYS Forge
talk
(Post)
Hi everyone, I just created a very simple library without any code and, added it to my very simple project. after adding this library, I get an error when I try to open "Symbol Set" in "OPC UA Server" in "Communication Manager". Here is the code of my POU in the library: FUNCTION_BLOCK POU VAR eCommand : (CMD_NONE:=0, CMD_RESET:=-1) INT := CMD_NONE; END_VAR Attached is the error that I get. I also noticed that by removing the enumeration variable above (eCommand), I can open the "Symbol Set" again. Anybody has any idea why this error is raised and how could I use enumeration variables without error in "OPC UA Symbol Set"?
Last updated: 2024-08-08
Post by gilbertamine on Comparing Arrays of structure
CODESYS Forge
talk
(Post)
Hi Everybody, I'm looking for a simple way of comparing two array of a structure. My structure is define like this : TYPE Positions_T : STRUCT PosX: DINT; PosY: DINT; PosZ: DINT; END_STRUCT END_TYPE I have multiples Var : ARRAY [0..220] OF Positions_T, that I need to compare quickly. I don't really want to do a Loop that goes by every 220 points and compare each one of them so I was wondering if there is another way. I came accross the MEM.Compare function, but it require to know the size in Bytes of the memory, and I don't know how to do that... Has anybody an idea on how to do the comparing easily ? Thanks in advance
Last updated: 2024-08-22
Post by dkugler on Webvisu client connection monitoring
CODESYS Forge
talk
(Post)
you can give this code snippet a try. It's extracted and simplyfied from my code: Install VisuElemBase lib if not installed yet. Execute in visu task: VAR pClientData : ARRAY [-1..100] OF POINTER TO VisuElemBase.VisuStructClientData; END_VAR VisuElemBase.g_ClientManager.BeginIteration(); pClientData := VisuElemBase.g_ClientManager.GetNextClient(); WHILE pClientData <> 0 DO pClientData[pClientData^.GlobalData.GlobalClientID] := pClientData; END_WHILE You have to make shure every no longer updated pointers in the array have to be deleted and no longer used by your code! Usage of this pointer access at your own risk :-) Works with SP16. From SP17 and newer there will be warnigs etc. using this solution as I remember. It will be great, if Codesys publishes a example or give a hint how to accesse this client values with the VisuUtil lib or other future-proof way!
Last updated: 2024-09-09
Post by timvh on Communication between applications on same device/controller/runtime (Win RTE 3.5.20.20)
CODESYS Forge
talk
(Post)
If you have a Windows system (RTE), then why not run the CODESYS HMI as separate controller. This could easily be moved to another PC if required. In the HMI you could also still use the Symbolic Var access as part of the Data Source Manager, although maybe OPC is the preferred way to make it more future proof. Or what about the Remote Target Visu. Then you can reduce the load of the main controller, while still only having to create one application including Visualization. This is now also supported for Linux systems. See: https://content.helpme-codesys.com/en/CODESYS%20Control/_rtsl_target_visu_for_linux.html https://content.helpme-codesys.com/en/CODESYS%20Visualization/_visu_execute_remote_target_visu.html
Last updated: 2024-09-28
Post by andrebrandt on FB string and naming
CODESYS Forge
talk
(Post)
Hi Tim. Not quite correct. What i'd like, is when you place the FB in prog, i choose a block NTC10k, and name this RT401. What i'm trying to do, is to get the name RT401 automaticly into the FB, so i can put this into the string i pass in a struct. So in var in POU, i saw this a place i could do this, RT401: NTC10k(Tag:='RT401'), but i can't get this to work. I shurly must have had something like this in FB: FUNCTION_BLOCK NTC10k String TAG But it's here where I'm stuck...
Last updated: 2024-09-30
Post by pistola on Case Function - Multiple Conditions, Is it possible?
CODESYS Forge
talk
(Post)
Looking at the help (https://content.helpme-codesys.com/en/CODESYS%20Development%20System/_cds_st_instruction_case.html). I don't think it is possible but I figured I'd ask. I'm looking at have a case that evaluates two variables, instead of having multiple If Else statements I was hoping I could just use a case. Example * Case Var1 & Var 2 of 10 & 10: Instruction Code 10 & 20: Instruction Code 20 & 20: Instruction Code End_Case Obviously I tried typing as noted above and Codesys threw an error. But I was wondering if there was a different way of programming to make something like this work? If not I'll just use a bunch of If Else Statements, Thanks.
Last updated: 2024-11-03
Post by jackbrady on Function Blocks and arrays of function blocks
CODESYS Forge
talk
(Post)
Hello, I am new to Codesys and PLC programming in general (please go easy ha!) I'm not looking for code to be written for me just some help and pointing in the right direction. I am writing some code to send commands to a relay based on input values (to put it simply). Quite basic stuff. I have wrote a function block that takes a global variable (Open_command:BOOL) and outputs to another global variable (Opened : BOOL). The function block is simulating a device so I'll eventually get the globals from that. I now need to create multiple versions of this function block/ device (lets say 100) but I need each iteration of that function block to reference it's own relevant global variable. I think that the best way of doing this would be to use arrays, although I could be wrong. I am aware that for up to 100 instances I could very well manually assign everything but that seems rather time consuming and I want a fancier way of doing it. Here is a very basic example of what I am looking to do, please note I have not written this in proper code it's just to show what I mean. Global Variables V[0-100] int Open_command [0-100] Bool Opened [0-100] Bool Function Block var input x : BOOL Var output y : BOOL if x then y = TRUE ELSE y = FALSE The input to my function block will be Open_command, output will be Opened Example code. If V[x] > 10 then Open_command [x] = TRUE ELSE Open_command [x] = FALSE (So when V1 goes above 10 I need Open_command1 = TRUE therefore initiating FB1 output. V2 > 10, open_command2 = True > FB2 output V3 > 10, open_command3 = True > FB3 output ... ... ) What I can't seem to figure out is how to tie all this together, I have read through the codesys documentation and if anything it has confused me more! ha. Apologies for the poorly written post but hopefully you understand what I am trying to get at. Thanks, Jack
Last updated: 2024-02-14
Post by gustavocsw on MQTT memory leak problem
CODESYS Forge
talk
(Post)
Hello everyone, I'm using the IoT Library to implement the MQTT communication with my local broker server in order to publish and subscribe at specifics topics to share and consume information about my application. But, it seems that are occurring some memory leak problem in a "high" frequency (more than 10 Hz) subscribe process. I follow the same method as in IoT Lib exemples, and at first looks perfect but my PLC was rebooting frequently and when I check its memory usage that was increasing as fast as the subscribe massage was sent. I'm using a WEG PLC410 and a WEG PLC500, and this error occurred in both of them (including in CODESYS Control Win x64). The application sends to the system a message JSON with the float payload Ex. {"data" : 0.8500}, but this happens with a INT, or BOL as well. I use the follow code in my application to find the value: //FindFirstValueByKey VARs PROGRAM JSON_VELO VAR //------Setting the JSON Subscriber to Set the Relay Value jsonDataVelo : JSON.JSONData; jsonByteArrayReaderVelo : JSON.JSONByteArrayReader; xST1okVelo : BOOL; FindFirstValueByKeyVelo : JSON.FindFirstValueByKey; jsonElementVelo : JSON.JSONElement; xDoneReaderVelo : BOOL; xDoneFindVelo : BOOL; //STRING and WSTRING for Subscribe the massage sPayloadJsonVelo : STRING := 'opa'; psPayloadJsonVelo : POINTER TO BYTE := ADR(sPayloadJsonVelo); //wsPayloadJsonRelaySet : WSTRING := "opa"; wsPayloadJsonVelo : WSTRING := STRING_TO_WSTRING('opa'); pwsPayloadJsonVelo : POINTER TO WORD := ADR(wsPayloadJsonVelo); lrVelo : LREAL; xKeepAliveVelo : BOOL; xSetVelo : BOOL; RSSet : RS; LIMPAR : STRING; //Find the msg end sFindVelo : STRING := '}'; psFindVelo : POINTER TO STRING := ADR(sFindVelo); iLenVelo : INT; iSizeVelo : INT := 12; udiContMsg : UDINT; END_VAR // FindFirstValueByKey CODE // Relay Set configuration xSetVelo := MQTT_SUBSCRIBER.RSVelo.Q1; IF xSetVelo THEN xKeepAliveVelo := TRUE; END_IF IF xKeepAliveVelo THEN udiContMsg := udiContMsg + 1; iLenVelo := TO_INT(StrLenA(psPayloadJsonVelo)); iSizeVelo := iLenVelo - TO_INT(MQTT_SUBSCRIBER.udiPayloadSizeVelo); StrDeleteA(psPayloadJsonVelo,iSizeVelo,iLenVelo); wsPayloadJsonVelo := STRING_TO_WSTRING(sPayloadJsonVelo); pwsPayloadJsonVelo := ADR(wsPayloadJsonVelo); //MQTT.ConvertUTF8toUTF16(sourceStart:= ADR(sPayloadJsonVelo), targetStart:= ADR(wsPayloadJsonVelo), dwTargetBufferSize:= TAM, bStrictConversion:= 1); //Reset jsonByteArrayReader jsonByteArrayReaderVelo ( xExecute := TRUE, pwData := pwsPayloadJsonVelo, jsonData := jsonDataVelo, xDone => xDoneReaderVelo ); FindFirstValueByKeyVelo( xExecute := xDoneReaderVelo, wsKey := "data", diStartIndex:= 0, jsonData := jsonDataVelo, jsonElement => jsonElementVelo, xDone => xDoneFindVelo ); IF xDoneFindVelo THEN lrVelo := jsonElementVelo.value.lrValue; //Reset jsonByteArrayReader jsonByteArrayReaderVelo ( xExecute := FALSE, pwData := pwsPayloadJsonVelo, jsonData := jsonDataVelo, xDone => xDoneReaderVelo ); FindFirstValueByKeyVelo( xExecute := FALSE, wsKey := "data", diStartIndex:= 1, jsonData := jsonDataVelo, jsonElement => jsonElementVelo, xDone => xDoneFindVelo ); xKeepAliveVelo := FALSE; GVL.xSetVeloRead := TRUE; END_IF END_IF And this to subscribe at the topic: //SUBSCRIBE VAR: //----------------- Subscribe Velocity ----------------------- MQTTSubscribeVelo : MQTT.MQTTSubscribe;//Variable MQTTSubscriber block -X - function-X wsTopicSubscribeVelo : WSTRING(1024) := "CORE/odometry/GET/data/simp"; // Topic to publish a message sSubscribeMassageVelo : STRING; udiPayloadSizeVelo : UDINT; xSDoneVelo : BOOL; xSErrorVelo : BOOL; xReceiveVelo : BOOL; eSTypeVelo : MQTT.MQTT_ERROR; eSMQTTErrorVelo : MQTT.MQTT_ERROR; RSVelo : RS; udiCont : UDINT; //SUBSCRIBE CODE: MQTTSubscribeVelo( xEnable:= MQTT_CLIENT.xConnection_Broker AND NOT xSErrorVelo AND NOT JSON_VELO.xKeepAliveVelo, pbPayload:= JSON_VELO.psPayloadJsonVelo, udiMaxPayloadSize:= SIZEOF(JSON_VELO.sPayloadJsonVelo), udiPayloadSize => udiPayloadSizeVelo, mqttClient:= MQTT_CLIENT.ClientMQTT, wsTopicFilter:=wsTopicSubscribeVelo, xDone => xSDoneVelo, xError=> xSErrorVelo, xReceived => xReceiveVelo, eMQTTError=> eSMQTTErrorVelo ); RSVelo(SET := xReceiveVelo, RESET1 := JSON_VELO.xKeepAliveVelo);
Last updated: 2024-09-09
To search for an exact phrase, put it in quotes. Example: "getting started docs"
To exclude a word or phrase, put a dash in front of it. Example: docs -help
To search on specific fields, use these field names instead of a general text search. You can group with AND
or OR
.