Skip to content

Handy Code Snippets

In addition to the various examples provided for each API Extension, and the full usage examples provided on the forum, the following additional snippets can be used directly in lots of situations.

Convert between String and Hex

The following functions convert between string and Hex, for example “F” to 15.

-- Convert hex to string

function string.fromhex(str)

return (str:gsub('..', function (cc)

return string.char(tonumber(cc, 16))

end))

end

-- Convert string to hex

function string.tohex(str)

return (str:gsub('.', function (c)

return string.format('%02X', string.byte(c))

end))

end

--Usage

hexString = "1F"

convertedHexString = string.tohex(hexString)

convertedBackHexString = string.fromhex(convertedHexString)

print(hexString)

print(convertedHexString)

print(convertedBackHexString) --should match original string

This allows for easy manipulation of the data received from and sent to some devices

Simple Test for Valid IP

The following function does a simple IP parsing validity test. It requires 12 digits in groups of three be somewhere in the string. It returns false if the IP address is not valid or it returns the parsed IP address.

function parseIPAddress (IPAddress)

parsedIPAddress = string.match(IPAddress, "%d%d%d.%d%d%d.%d%d%d.%d%d%d")

if debug then

print("received: " .. IPAddress)

if parsedIPAddress then

print("parsed: " .. parsedIPAddress)

else

print("not valid IP")

end

end

return parsedIPAddress

end

If a debug variable is set to true (see Best Practices, Tips and Tricks), the function will print additional information to the Debug Output File.

In production, this code can be extended to check for more errors and handle more entry formats.

Simple FIFO Queue

This code provides a set of functions to create a simple Queue using a sequence you can use in a variety of applications. It can be used for First In First Out (FIFO) or Last In First Out (LIFO) applications.

--Push a value onto the sequence at the bottom

function pushLast(sequence, value)

table.insert(sequence, value)

end

--Pop a value off of the sequence from the top (FIFO)

function popFirst(sequence)

if #sequence > 0 then

return table.remove(sequence, 1)

else

return nil

end

end

--Pop a value off of the sequence from the bottom (LIFO)

function popLast(sequence)

if #sequence > 0 then

return table.remove(sequence, #sequence)

else

return nil

end

end

--Search Sequence for value and return position if present, otherwise return false.

--This can be used to test if a value is present before popping it.

--It can also be used in conjunction with the removeFromQueue function below.

function getPositionInQueue(sequence, value)

for k, v in pairs(sequence) do

if v == value then

return k

end

end

return false

end

--Remove a value from the sequence at an arbirtrary position if present.

function removeFromQueue(sequence, position)

if position ~= false and #sequence > position then

table.remove(sequence, position)

end

end

It is called a Simple Queue because there are more complex ways to do this which can better handle longer queues. Also, there are additional error checks that can be added to each function to make them more robust.

See the “Simple Queue” Example script for a usage example.