Post by smeitink on Timeout Error in Modbus Communication with WAGO PFC200 and iEM2050 Meter using 750-652 Module
CODESYS Forge
talk
(Post)
Hi all, I'm looking for help with an issue I've come across while trying to facilitate Modbus communication between a WAGO PFC200 PLC using a 750-652 communication module and an iEM2050 Series Single Phase Energy Meter. I believe to have everything wired and setup correcty, but I keep running into a "Error time out" message, and by now I don't really know what else to try. My setup is as follows: A PFC200 Wago PLC, which has 2 750-652 Serial Interfaces extension modules connected to its field bus. I'm using one of these to talk to a Schneider iEM2050 - kWh-meter over modbus. I have connected terminal 23 (A) of the iEM2050 to connector 6 (A) on the 750-652. I have connected terminal 24 (B) of the iEM2050 to connector 2 (B) of the 750-652. I'm using 200mm of twisted together wires to connected them both, and I have placed a 120 ohm resistor between A and B at both ends. I've attached relevant pinout images to this post. I then wrote a simple program that configures the Mobus port, as per the datasheet of the iEM2050. You can find an image of the relavent page attached to this post too. This is my program: PROGRAM PLC_PRG VAR Master: FbMbMasterSerial; xIsConnected: BOOL; xError: BOOL; iIndex: INT := 1; xTrigger: BOOL; utQuery : typMbQuery := ( bUnitId := 1, // The Modbus unit or slave address bFunctionCode := 4, // Function code for reading input registers uiReadAddress := 1829, // adress for the Power on off counter uiReadQuantity := 1 // Quantity of registers to read ); iStep: INT; oStatusModbus: WagoSysErrorBase.FbResult; utResponseModbus: typMbResponse; xConnect: BOOL := FALSE; delayTimer: TON; END_VAR Master( xConnect:= xConnect, I_Port:= _750_652_24_1, udiBaudrate:= 9600, usiDataBits:= 8, eParity:= WagoTypesCom.eTTYParity.Even, eStopBits:= WagoTypesCom.eTTYStopBits.One, eHandshake:= WagoTypesCom.eTTYHandshake.None, ePhysical:= WagoTypesCom.eTTYPhysicalLayer.RS485_HalfDuplex, xIsConnected=> xIsConnected, xError=> xError, oStatus=> oStatusModbus, eFrameType:= WagoAppPlcModbus.eMbFrameType.RTU, tTimeOut:= T#5S, utQuery:= utQuery, xTrigger:= xTrigger, utResponse:= utResponseModbus); delayTimer(IN := TRUE, PT := T#3S); // Use the Q output of the timer to set xConnect after the delay IF delayTimer.Q THEN xConnect := TRUE; END_IF CASE iStep OF 0: //Wacht totdat de master de poort geopend heeft IF xIsConnected THEN iStep := 1; END_IF 1: //Stuur request naar de slave xTrigger := TRUE; iStep := 2; 2: //Wacht totdat de master klaar is met het afhandelen van de request IF NOT xTrigger THEN iStep := 3; END_IF END_CASE The TON delay before opening the port is due to a an error I encountered when opening it straight away. This seems to be a bug, as described here. However, the TON solved that particular issue. I tried reading multiple registers, but like I said, I still always end up with the "Error time out". What else can I test or try at this point?
Last updated: 2024-02-24
Post by anonymous on Hi, I try to send and receive data using a UDP connection via SysSocket 3.5.17.0. While sending data works fine, I have problems with the receiving part. I am able to capture the received data of client side in wireshark But unable to capture it on the codesys
CODESYS Forge
talk
(Post)
Hi, I try to send and receive data using a UDP connection via SysSocket 3.5.17.0. While sending data works fine, I have problems with the receiving part.I am able to capture the data of client side in wireshark but i am unable to capture it in the codesys. Heres the below part of code of client side. PROGRAM POU_udpclient_program VAR istep : INT := 1;//step variable for state machine xStart: BOOL;// Flag to start the UDP protocol iecSocketId: syssocket_interfaces.RTS_IEC_HANDLE;//socket handle for receiving iecCreateResult: syssocket_interfaces.RTS_IEC_RESULT; ipAddr: syssocket.SOCKADDRESS;//Socket address structure for receiving sIpAddress : STRING := '192.168.0.2'; wPort: WORD:= 12346; iecConnectResult : syssocket_interfaces.RTS_IEC_RESULT;//connect paramters sDataRec : STRING[255];//Buffer for received data xiRecBytes : __XINT;//number of bytes received iecRecResult : syssocket_interfaces.RTS_IEC_RESULT;//receive data parameters iecCloseResult : syssocket_interfaces.RTS_IEC_RESULT; END_VAR syssocket.SysSockInetAddr(sIpAddress,ADR(ipAddr.sin_addr)); ipAddr.sin_family := syssocket.SOCKET_AF_INET; ipAddr.sin_port := syssocket.SysSockHtons(wPort); CASE istep OF 1: //create socket IF xStart THEN iecSocketId:= syssocket.SysSockCreate(syssocket.SOCKET_AF_INET,syssocket.SOCKET_DGRAM,syssocket.SOCKET_IPPROTO_IP,ADR(iecCreateResult)); IF iecSocketId = syssocket_interfaces.RTS_INVALID_HANDLE THEN xStart := FALSE; istep := 1; ELSE istep := 2; END_IF END_IF 2: //connect to socket server using setoption iecConnectResult := syssocket.SysSockSetOption(iecSocketId,syssocket.SOCKET_SOL,syssocket.SOCKET_SO_REUSEADDR,ADR(ipAddr),SIZEOF(ipAddr)); istep := 3; 3: //receive data xiRecBytes := syssocket.SysSockRecvFrom(iecSocketId,ADR(sDataRec),SIZEOF(sDataRec),0,ADR(ipAddr),SIZEOF(ipAddr),ADR(iecRecResult)); istep := 4; 4: //close socket iecCloseResult:= syssocket.SysSockClose(iecSocketId); xStart := FALSE; istep := 1; END_CASE
Last updated: 2024-06-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 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 mogam on Internal error:System.NullReferenceException: The object reference was not set to an object instance.
CODESYS Forge
talk
(Post)
Hi everyone, I have a my App that reads values from a shared memory than 2 variables x,y were calculated. here as you can see in my code: PROGRAM PLC_PRG VAR szName : STRING := '/SM'; ( Name of the shared memory.) uxiSize : __UXINT := 646 * 2 * SIZEOF(REAL); ( Size of the shared memory. ) hShm : RTS_IEC_HANDLE; ( Handle to the shared memory ) pStart : POINTER TO REAL; ( Pointer to the first element in the memory ) pData : POINTER TO REAL; ( Pointer, which is incremented until the last sign in the memory is reached. ) shared_data :ARRAY[0..645, 0..1] OF REAL; velocity : ARRAY[0..2] OF REAL; ( Velocity array ) position : ARRAY[0..2] OF REAL; ( Position array ) angleDepth : ARRAY[0..645, 0..1] OF REAL; ( Angle and depth array ) xEndOfMemory : BOOL; x : ARRAY[0..645] OF REAL := 1.0; y : ARRAY[0..645] OF REAL := 1.0; (* Result of actions at the memory. *) OpenResult : RTS_IEC_RESULT; ReadResult : RTS_IEC_RESULT; PointerResult : RTS_IEC_RESULT; DeleteResult : RTS_IEC_RESULT; CloseResult : RTS_IEC_RESULT; i : INT; END_VAR ( Open the shared memory ) hShm := SysSharedMemoryOpen2(szName, 0, ADR(uxiSize), ADR(OpenResult)); IF hShm <> RTS_INVALID_HANDLE THEN (* Read the entire shared memory table *) SysSharedMemoryRead(hShm:= hShm, ulOffset:= 0, pbyData:= ADR(shared_data), ulSize:= uxiSize, pResult:= ADR(ReadResult)); (* Fetch the pointer from the shared memory. The pointer is pointing to the first element address *) //pStart := SysSharedMemoryGetPointer(hShm, ADR(PointerResult)); (* Close the shared memory *) //CloseResult := SysSharedMemoryClose(hShm := hShm); (* Read velocity and position data from the shared memory *) FOR i := 0 TO 2 DO velocity[i] := shared_data[i, 0]; position[i] := shared_data[i, 1]; END_FOR; (* Read angle and depth data from the shared memory *) FOR i := 0 TO 645 DO angleDepth[i, 0] := shared_data[(i + 6), 0]; angleDepth[i, 1] := shared_data[(i + 6), 1]; END_FOR; FOR i := 0 TO 645 DO x[i] := angleDepth[i, 1]*COS(angleDepth[i, 0]); y[i] := angleDepth[i, 1]*SIN(angleDepth[i, 0]); END_FOR; END_IF For these values an XY-CHART needs to be done and when i create a visualisation and set the x data and y data when i try to compile, i recieve this error: ------ Übersetzungslauf gestartet: Applikation: Device.Read_App ------- Code typisieren ... [FEHLER] Internal error:System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. bei _3S.CoDeSys.LanguageModelManager.LDateType.Accept(ITypeVisitor typvis) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IArrayType ) bei ..(_IPointerType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IPointerType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IPointerType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei _3S.CoDeSys.Compiler35140.Compiler.(_ICompileContext , _IPreCompileContext , _ICompileContext , IProgressCallback ) bei _3S.CoDeSys.Compiler35140.Compiler.(_ICompileContext , Boolean , Boolean , _IPreCompileContext , _IPreCompileContext , _ICompileContext , Boolean , Boolean& , IProgressCallback ) bei _3S.CoDeSys.Compiler35140.Compiler.(Guid , Boolean , Boolean , Boolean , Boolean& , _ICompileContext& , _ICompileContext& , IProgressCallback , Boolean , Boolean ) Kompilierung abgeschlossen -- 1 Fehler, 0 Warnungen Übersetzung abgeschlossen -- 1 Fehler, 0 Warnungen : Kein Download möglich!
Last updated: 2024-05-24
Post by mogam on Internal error:System.NullReferenceException: The object reference was not set to an object instance.
CODESYS Forge
talk
(Post)
Hi everyone, I have a my App that reads values from a shared memory than 2 variables x,y were calculated. here as you can see in my code: PROGRAM PLC_PRG VAR szName : STRING := '/SM'; ( Name of the shared memory.) uxiSize : __UXINT := 646 * 2 * SIZEOF(REAL); ( Size of the shared memory. ) hShm : RTS_IEC_HANDLE; ( Handle to the shared memory ) pStart : POINTER TO REAL; ( Pointer to the first element in the memory ) pData : POINTER TO REAL; ( Pointer, which is incremented until the last sign in the memory is reached. ) shared_data :ARRAY[0..645, 0..1] OF REAL; velocity : ARRAY[0..2] OF REAL; ( Velocity array ) position : ARRAY[0..2] OF REAL; ( Position array ) angleDepth : ARRAY[0..645, 0..1] OF REAL; ( Angle and depth array ) xEndOfMemory : BOOL; x : ARRAY[0..645] OF REAL := 1.0; y : ARRAY[0..645] OF REAL := 1.0; (* Result of actions at the memory. *) OpenResult : RTS_IEC_RESULT; ReadResult : RTS_IEC_RESULT; PointerResult : RTS_IEC_RESULT; DeleteResult : RTS_IEC_RESULT; CloseResult : RTS_IEC_RESULT; i : INT; END_VAR ( Open the shared memory ) hShm := SysSharedMemoryOpen2(szName, 0, ADR(uxiSize), ADR(OpenResult)); IF hShm <> RTS_INVALID_HANDLE THEN (* Read the entire shared memory table *) SysSharedMemoryRead(hShm:= hShm, ulOffset:= 0, pbyData:= ADR(shared_data), ulSize:= uxiSize, pResult:= ADR(ReadResult)); (* Fetch the pointer from the shared memory. The pointer is pointing to the first element address *) //pStart := SysSharedMemoryGetPointer(hShm, ADR(PointerResult)); (* Close the shared memory *) //CloseResult := SysSharedMemoryClose(hShm := hShm); (* Read velocity and position data from the shared memory *) FOR i := 0 TO 2 DO velocity[i] := shared_data[i, 0]; position[i] := shared_data[i, 1]; END_FOR; (* Read angle and depth data from the shared memory *) FOR i := 0 TO 645 DO angleDepth[i, 0] := shared_data[(i + 6), 0]; angleDepth[i, 1] := shared_data[(i + 6), 1]; END_FOR; FOR i := 0 TO 645 DO x[i] := angleDepth[i, 1]*COS(angleDepth[i, 0]); y[i] := angleDepth[i, 1]*SIN(angleDepth[i, 0]); END_FOR; END_IF For these values an XY-CHART needs to be done and when i create a visualisation and set the x data and y data when i try to compile, i recieve this error: ------ Übersetzungslauf gestartet: Applikation: Device.Read_App ------- Code typisieren ... [FEHLER] Internal error:System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. bei _3S.CoDeSys.LanguageModelManager.LDateType.Accept(ITypeVisitor typvis) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IArrayType ) bei ..(_IPointerType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IPointerType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IPointerType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei ..(_ISignature , _IPreCompileContext ) bei ..( ) bei ..(String ) bei ..(String , IVariable[]& , ISignature[]& ) bei ..(String , IVariable[]& , ISignature[]& , IScope& ) bei ..visit(_IVariableExpression , AccessFlag ) bei ..(_IUserdefType ) bei ..(_IVariable , IScope5 , _ICompileContext , _ISignature ) bei ..(_ISignature , IScope5 , _ICompileContext ) bei _3S.CoDeSys.Compiler35140.Compiler.(_ICompileContext , _IPreCompileContext , _ICompileContext , IProgressCallback ) bei _3S.CoDeSys.Compiler35140.Compiler.(_ICompileContext , Boolean , Boolean , _IPreCompileContext , _IPreCompileContext , _ICompileContext , Boolean , Boolean& , IProgressCallback ) bei _3S.CoDeSys.Compiler35140.Compiler.(Guid , Boolean , Boolean , Boolean , Boolean& , _ICompileContext& , _ICompileContext& , IProgressCallback , Boolean , Boolean ) Kompilierung abgeschlossen -- 1 Fehler, 0 Warnungen Übersetzung abgeschlossen -- 1 Fehler, 0 Warnungen : Kein Download möglich!
Last updated: 2024-05-24
Post by paulg on RasPi CAA Serial example - unexpected behavior during debug
CODESYS Forge
talk
(Post)
I've trimmed down the CAA Serial Codesys example to only listen on one port but, when stepping through the Case structure in debug mode, it jumps out of the structure during a specific point in every scan (I'll point it out below after describing the setup and listing the code). I'm using a Pi 4 Model B, and I have an Arduino Nano Every plugged in via USB which is streaming the following serial message at 1 Hz: Time since opening connection: 1 s Time since opening connection: 2 s ...and so on. The Pi shows the Nano at /dev/ttyACM0 so I edited CODESYSControl_User.cfg to read: Linux.Devicefile=/dev/ttyACM The code in my PLC_PRG is (ignore some of the comments, I hadn't deleted them out from the original example): PROGRAM PLC_PRG VAR xStartTest : BOOL:= TRUE; iState : INT; xTestDone : BOOL;(* True, when the test was done succesfully *) (* Settings to communicate with the COM Port *) aCom1Params : ARRAY [1..7] OF COM.PARAMETER; como1 : COM.Open; comc1 : COM.Close; comw1 : COM.Write; comr1 : COM.Read; //sWrite : STRING := 'Test String!'; sRead : STRING(25); szRead : CAA.SIZE; xCom1OpenError : BOOL; xCom1CloseError : BOOL; xCom1WriteError : BOOL; xCom1ReadError : BOOL; END_VAR //This example shows the communication of two COM Ports with each other. //The first one writes a string of characters, which is read by the second one. //After successful execution, the two COM Ports are closed and the test is done. IF xStartTest THEN CASE iState OF 0: //The parameters are set for the COM Port aCom1Params[1].udiParameterId := COM.CAA_Parameter_Constants.udiPort; aCom1Params[1].udiValue := 1; // the correct Port should be adapted aCom1Params[2].udiParameterId := COM.CAA_Parameter_Constants.udiBaudrate; aCom1Params[2].udiValue := 115200; aCom1Params[3].udiParameterId := COM.CAA_Parameter_Constants.udiParity; aCom1Params[3].udiValue := INT_TO_UDINT(COM.PARITY.NONE); aCom1Params[4].udiParameterId := COM.CAA_Parameter_Constants.udiStopBits; aCom1Params[4].udiValue := INT_TO_UDINT(COM.STOPBIT.ONESTOPBIT); aCom1Params[5].udiParameterId := COM.CAA_Parameter_Constants.udiTimeout; aCom1Params[5].udiValue := 0; aCom1Params[6].udiParameterId := COM.CAA_Parameter_Constants.udiByteSize; aCom1Params[6].udiValue := 8; aCom1Params[7].udiParameterId := COM.CAA_Parameter_Constants.udiBinary; aCom1Params[7].udiValue := 0; //The first Port is opened with the given parameters como1(xExecute := TRUE, usiListLength:=SIZEOF(aCom1Params)/SIZEOF(COM.PARAMETER),pParameterList:= ADR(aCom1Params)); IF como1.xError THEN xCom1OpenError := TRUE; iState := 1000; END_IF //After a successful opening, the next state is reached IF como1.xDone THEN iState := 15; END_IF 15: // the reading process is started comr1(xExecute := TRUE,hCom:= como1.hCom, pBuffer:= ADR(sRead), szBuffer:= SIZEOF(sRead)); IF comr1.xError THEN xCom1ReadError := TRUE; END_IF //After completion the size of the written bytes are saved IF comr1.xDone OR comr1.xError THEN szRead := comr1.szSize; iState := 20; END_IF 20: // If everything was successful the ports are closed and the handles are released comc1(xExecute := TRUE,hCom:= como1.hCom); IF comc1.xError THEN xCom1CloseError := TRUE; END_IF IF comc1.xDone OR comc1.xError THEN iState := 25; END_IF 25: // The first port is closed and the used handle released xTestDone := TRUE; xStartTest := FALSE; iState := 0; como1(xExecute := FALSE); comw1(xExecute := FALSE); comc1(xExecute := FALSE); ELSE iState := 0; END_CASE END_IF I realize as I write this that the .udiPort should be 0 and not 1, but that shouldn't be causing the issue I'm seeing. I'm forcing xStartTest:=TRUE every scan so that I can step into each line and observe what's happening. What I see is that the port parameters are set and the port is opened with no errors, but the code jumps out of the case structure to the last line every time it reaches (and I step into) the iState:=15 line (at the end of the iState:=0 block). So every scan cycle it goes through the block for iState=0 and jumps out at the same spot. I'm a little new to PLC programming so I may be misunderstanding the flow, but shouldn't this case structure keep moving down in the same scan? If it only handles one case per scan, why doesn't the value of iState persist? Thanks! Update: I restarted the Codesys control today and I was then able to see an error for como1.eError of "WRONG_PARAMETER". I tried doing some digging and another post made me think I should add another line to CODESYSControl_User.cfg, so I now have: [SysCom] Linux.Devicefile=/dev/ttyACM portnum := COM.SysCom.SYS_COMPORT1 So now when I set .udiPort to 1, I get "NO_ERROR" but I also don't read anything from the port (i.e. szRead = 0 always). If I try setting the port to 0 (which I'm confused about, because I added a COMPORT1 line but the device shows on the Pi as ACM0), I get the "WRONG_PARAMETER" error again. Is there an easier way to troubleshoot the Pi and view what ports the Codesys runtime is actually able to see while the Pi is running?
Last updated: 2024-06-06
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
.