Skip to content

Instantly share code, notes, and snippets.

@WilliamBerryiii
Last active May 2, 2019 01:37
Show Gist options
  • Save WilliamBerryiii/90d233de838bfd0d24f9c23cb0f314dc to your computer and use it in GitHub Desktop.
Save WilliamBerryiii/90d233de838bfd0d24f9c23cb0f314dc to your computer and use it in GitHub Desktop.
Build and Configure Modbus Sample for Azure Iot Gateway SDK
# load up the visucal c envirionment varaibles for use in the build, and call the SDKs build script
Invoke-CmdScript "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64_x86
.\tools\build.cmd --skip-unittests
# with the build complete, restore our environment variables
Restore-Environment $env
# pull in the modbus template configuration file for modification
$config = ConvertFrom-Json (gc .\samples\modbus\src\modbus_win.json -Raw)
# Set up project variables
$rootProjectPath = "C:\source\ModbusTest"
# iot hub configuration
$iotHubName = "<iot-hub-name>"
$iotHubSuffix = "<iot-hub-suffix, i.e.`azure-devices.net`>"
$transport = [Transport]::AMQP
# device configuration
$deviceName = "ModbusDevice"
$deviceKey = "DeviceKey"
$deviceMac = "00:01:05:00:BE:9F"
# modbus server configuration
$modbusServerIp = "192.168.1.115"
$modbusPollingInterval = "500"
$modbusDeviceType = "Encoder"
$modbusUnitId = "0"
$modbusFunctionCode = "4"
$modbusReadStartingAddress = "2"
$modbusReadLength = "1"
# move into the azure-iot-gateway-sdk project for build and configure steps
Set-Location .\azure-iot-gateway-sdk
# copy the existing environment variable dictionary for restore later
$env = Get-Environment
#Create the projects target directory and set it as the current location
New-Item -ItemType Directory -Force -Path $rootProjectPath
Set-Location $rootProjectPath
# use the --quiet option to deal with git pushing output to stderr rather than stdout
# see here: https://github.com/dahlbyk/posh-git/issues/109
# this will gut down on some of the annoyance, but won't stop all the stderr output
git clone --recursive https://github.com/Azure/azure-iot-gateway-sdk.git
git clone https://github.com/Azure/iot-gateway-modbus.git
<#
The following functions are taken from: http://windowsitpro.com/powershell/take-charge-environment-variables-powershell
#>
# Invokes a Cmd.exe shell script and updates the environment.
function Invoke-CmdScript {
param(
[String] $scriptName
)
$cmdLine = """$scriptName"" $args & set"
& $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
select-string '^([^=]*)=(.*)$' | foreach-object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
set-item Env:$varName $varValue
}
}
# Returns the current environment.
function Get-Environment {
get-childitem Env:
}
# Restores the environment to a previous state.
function Restore-Environment {
param(
[parameter(Mandatory=$TRUE)]
[System.Collections.DictionaryEntry[]] $oldEnv
)
# Remove any added variables.
compare-object $oldEnv $(Get-Environment) -property Key -passthru |
where-object { $_.SideIndicator -eq "=>" } |
foreach-object { remove-item Env:$($_.Name) }
# Revert any changed variables to original values.
compare-object $oldEnv $(Get-Environment) -property Value -passthru |
where-object { $_.SideIndicator -eq "<=" } |
foreach-object { set-item Env:$($_.Name) $_.Value }
}
# copy modbus module code to azure-iot0-gateway-sdk folder for building
cp .\iot-gateway-modbus\samples\modbus .\azure-iot-gateway-sdk\samples -Recurse
cp .\iot-gateway-modbus\modules\modbus_read .\azure-iot-gateway-sdk\modules -Recurse
# add cmake args for modbus module building in azure-iot-gateway-sdk
ac .\azure-iot-gateway-sdk\modules\CMakeLists.txt "`nadd_subdirectory(modbus_read)"
ac .\azure-iot-gateway-sdk\samples\CMakeLists.txt "`nadd_subdirectory(modbus)"
# Define and enum for the allowable transports for the Azure Iot Hub Gateway
Enum Transport {
AMQP
HTTP
MQTT
}
# Set up project variables
$rootProjectPath = "C:\source\ModbusTest1"
# iot hub configuration
$iotHubName = "iot-fsharp-hub"
$iotHubSuffix = "azure-devices.net"
$transport = [Transport]::AMQP
# device configuration
$deviceName = "ModbusDevice"
$deviceKey = "MHffHeJWR09ArvM4b4M5zLHCyrdE/kiisvVp3krnx4M="
$deviceMac = "00:01:05:00:BE:9F"
# modbus server configuration
$modbusServerIp = "192.168.1.115"
$modbusPollingInterval = "500"
$modbusDeviceType = "Encoder"
$modbusUnitId = "0"
$modbusFunctionCode = "4"
$modbusReadStartingAddress = "2"
$modbusReadLength = "1"
<#
The following functions are taken from: http://windowsitpro.com/powershell/take-charge-environment-variables-powershell
#>
# Invokes a Cmd.exe shell script and updates the environment.
function Invoke-CmdScript {
param(
[String] $scriptName
)
$cmdLine = """$scriptName"" $args & set"
& $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
select-string '^([^=]*)=(.*)$' | foreach-object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
set-item Env:$varName $varValue
}
}
# Returns the current environment.
function Get-Environment {
get-childitem Env:
}
# Restores the environment to a previous state.
function Restore-Environment {
param(
[parameter(Mandatory=$TRUE)]
[System.Collections.DictionaryEntry[]] $oldEnv
)
# Remove any added variables.
compare-object $oldEnv $(Get-Environment) -property Key -passthru |
where-object { $_.SideIndicator -eq "=>" } |
foreach-object { remove-item Env:$($_.Name) }
# Revert any changed variables to original values.
compare-object $oldEnv $(Get-Environment) -property Value -passthru |
where-object { $_.SideIndicator -eq "<=" } |
foreach-object { set-item Env:$($_.Name) $_.Value }
}
#Create the projects target directory and set it as the current location
New-Item -ItemType Directory -Force -Path $rootProjectPath
Set-Location $rootProjectPath
# use the --quiet option to deal with git pushing output to stderr rather than stdout
# see here: https://github.com/dahlbyk/posh-git/issues/109
# this will gut down on some of the annoyance, but won't stop all the stderr output
git clone --recursive https://github.com/Azure/azure-iot-gateway-sdk.git
git clone https://github.com/Azure/iot-gateway-modbus.git
# copy modbus module code to azure-iot0-gateway-sdk folder for building
cp .\iot-gateway-modbus\samples\modbus .\azure-iot-gateway-sdk\samples -Recurse
cp .\iot-gateway-modbus\modules\modbus_read .\azure-iot-gateway-sdk\modules -Recurse
# add cmake args for modbus module building in azure-iot-gateway-sdk
ac .\azure-iot-gateway-sdk\modules\CMakeLists.txt "`nadd_subdirectory(modbus_read)"
ac .\azure-iot-gateway-sdk\samples\CMakeLists.txt "`nadd_subdirectory(modbus)"
# move into the azure-iot-gateway-sdk project for build and configure steps
Set-Location .\azure-iot-gateway-sdk
# copy the existing environment variable dictionary for restore later
$env = Get-Environment
# load up the visucal c envirionment varaibles for use in the build, and call the SDKs build script
Invoke-CmdScript "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64_x86
.\tools\build.cmd --skip-unittests
# with the build complete, restore our environment variables
Restore-Environment $env
# pull in the modbus template configuration file for modification
$config = ConvertFrom-Json (gc .\samples\modbus\src\modbus_win.json -Raw)
# modify the IoTHub settings
$iothubargs = $config.modules.Where({$_.name -eq "IoTHub"}).args
$iothubargs.IoTHubName = $iotHubName
$iothubargs.IoTHubSuffix = $iotHubSuffix
$iothubargs.Transport = $transport.ToString()
# modify the mappings section to add the test device
$props = $mappingArgs = $config.modules.Where({$_.name -eq "mapping"}).args
$props.macAddress = $deviceMac
$props.deviceId = $deviceName
$props.deviceKey = $deviceKey
# modify the modbus_read section with server & modbus function details
$modbusRead = $config.modules.Where({$_.name -eq "modbus_read"}).args
$modbusRead.serverConnectionString = $modbusServerIp
$modbusRead.interval = $modbusPollingInterval
$modbusRead.macAddress = $deviceMac
$modbusRead.deviceType = $modbusDeviceType
$ops = @{}
$ops.unitId = $modbusUnitId
$ops.functionCode = $modbusFunctionCode
$ops.startingAddress = $modbusReadStartingAddress
$ops.length = $modbusReadLength
$opsArray = @( $ops )
$modbusRead.operations = $opsArray
# write the config to the output directory for the modubs sample
# make sure to output an ASCII encoded file.
$config | ConvertTo-Json -depth 100 | Out-File ".\build\samples\modbus\Debug\modbus_win.json" -Encoding ASCII
# modify the IoTHub settings
$iothubargs = $config.modules.Where({$_.name -eq "IoTHub"}).args
$iothubargs.IoTHubName = $iotHubName
$iothubargs.IoTHubSuffix = $iotHubSuffix
$iothubargs.Transport = $transport.ToString()
# modify the mappings section to add the test device
$props = $mappingArgs = $config.modules.Where({$_.name -eq "mapping"}).args
$props.macAddress = $deviceMac
$props.deviceId = $deviceName
$props.deviceKey = $deviceKey
# modify the modbus_read section with server & modbus function details
$modbusRead = $config.modules.Where({$_.name -eq "modbus_read"}).args
$modbusRead.serverConnectionString = $modbusServerIp
$modbusRead.interval = $modbusPollingInterval
$modbusRead.macAddress = $deviceMac
$modbusRead.deviceType = $modbusDeviceType
$ops = @{}
$ops.unitId = $modbusUnitId
$ops.functionCode = $modbusFunctionCode
$ops.startingAddress = $modbusReadStartingAddress
$ops.length = $modbusReadLength
$opsArray = @( $ops )
$modbusRead.operations = $opsArray
# write the config to the output directory for the modubs sample
# make sure to output an ASCII encoded file.
$config | ConvertTo-Json -depth 100 | Out-File ".\build\samples\modbus\Debug\modbus_win.json" -Encoding ASCII
# Define and enum for the allowable transports for the Azure Iot Hub Gateway
Enum Transport {
AMQP
HTTP
MQTT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment