This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Easiest way to get datatype string
#1
Hi !
I want to get a datatype of each object as "dt.bool" or "dt.bit2".

When I use the function grp.find('0/1/0').datatype,  it gives me a number like 1, 2, not a datatype string.
Is there a mapping table somewhere so that I can get a datatype string as "dt.bool" or "dt.bit2" ?
For now I am trying to create a mapping table like this below but I am not sure if all of them are correct.
Is there any other way?


Code:
local convertToString = {
    [1] = "dt.bool",
    [2] = "dt.bit2",
    [3] = "dt.bit4",
    [4] = "dt.char",
    [5] = "dt.uint8",
    [6] = "dt.int8",
    [7] = "dt.uint16",
    [8] = "dt.int16",
    [9] = "dt.float16",
    [10] = "dt.time",
    [11] = "dt.date",
    [12] = "dt.uint32",
    [13] = "dt.int32",
    [14] = "dt.float32",
    [15] = "dt.access",
    [16] = "dt.string",
}
Reply
#2
Use this example. It also handles sub-datatypes which don't have a dt table entry (e.g. 01.011 -> dt.bool).
Code:
mapping = {}
for key, value in pairs(dt) do
  mapping[ value ] = 'dt.' .. key
end

obj = grp.find('1/1/2')
dpt = obj.datatype
if dpt then
  dtname = mapping[ dpt ]

  if not dtname and dpt >= 1000 then
    dtname = mapping[ math.floor(dpt / 1000) ]
  end
end

log(dtname)
Reply
#3
Thank you very very much admin !!

It's working perfect (:
Reply


Forum Jump: