Jakob
Jakob2mo ago

Passing C enum to function and read in TypeScript

First of all I am not a trained software developper, so please forgive me if I am missing some basics. I am trying to build a wrapper around a dynamic library to IEC 61850 communication.
On the the exposed functions of that library is IedConnection_connect(..., IedClientError error, ...) , where IedClientError is a enum with 23 entries. To be able to use the library I need to 1. create the enumeration in TypeScript and change it to a type array? to be able to pass it down 2. but then I also need to read the changed error again in TypeScript I am not sure how enumerations are coded and how to decode those in TypeScript again and I was hoping one of you can help me out 🙂 BR, Jakob
10 Replies
marvinh.
marvinh.2mo ago
There is a good page about enums in the TS documentation https://www.typescriptlang.org/docs/handbook/enums.html
Leokuma
Leokuma2mo ago
They are just numbers You need to find the enum declaration in the header file (something like IedClientError.h) and see what value is assigned to each enum option. If they are not assigned anything, by default they are assigned a number starting from 0. So if you want to use the first enum option, you pass 0
Jakob
Jakob2mo ago
How would+ This means that I could do something like this? let error = 0; lib.symbols.IedConnection_connect(... , error, ...); console.log(error) This gives me a Segmentation fault (core dumped) error. I think the enum has to be instantiated as an ArrayBuffer, but do not know how to read that buffer correctly.
Leokuma
Leokuma2mo ago
That should work, without an ArrayBuffer Are you declaring the enum as i32? IedConnection_connect: {parameters: [..., "i32", ...]} ooh sorry I just reread OP the function will change the value of the error you pass?
Jakob
Jakob2mo ago
Yes exactly that
Leokuma
Leokuma2mo ago
I'm stupid
Jakob
Jakob2mo ago
It does work like so for me. But I am not sure if this is the propper way of doing it: const error = new Uint8Array(1); lib.symbols.IedConnection_connect(conn, error, host, port); The result is then error[0] And I declare the variable as buffer in the symbols object
Leokuma
Leokuma2mo ago
That's correct
Jakob
Jakob2mo ago
Thank you for the fast response 😄
Leokuma
Leokuma2mo ago
BTW, to make sure the function is actually writing to the buffer, I suggest you fill up the buffer with something before calling it, just to see if it goes back to zero If it doesn't change, I think we'll have to create a pointer to a buffer or something