Created
November 11, 2019 16:46
-
-
Save gitlarryf/95d5291ac6f695dfcb2ea832411c4374 to your computer and use it in GitHub Desktop.
Neon-lang version of the MVI69E-LDM Test Harness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IMPORT io | |
IMPORT sys | |
IMPORT mvi69 | |
IMPORT time | |
VAR bpHandle: mvi69.MVI69Handle | |
VAR ver: mvi69.MVI69VersionInfo | |
VAR scanCount: Number := 0 | |
VAR bDone: Boolean := FALSE | |
VAR ret: Number := 0xFFFF | |
setInfo() | |
IF mvi69.Open(OUT bpHandle) <> mvi69.MVI69_SUCCESS THEN | |
fatal("Failed to open BP handle!") | |
END IF | |
_ := mvi69.GetScanCounter(bpHandle, OUT scanCount) | |
print("Backplane opened, scan count: \(scanCount)") | |
IF mvi69.GetVersionInfo(bpHandle, OUT ver) = mvi69.MVI69_SUCCESS THEN | |
print("APISeries\t: \(ver.APISeries)") | |
print("APIRevision\t: \(ver.APIRevision)") | |
print("DDSeries\t: \(ver.DDSeries)") | |
print("DDRevision\t: \(ver.DDRevision)") | |
END IF | |
_ := mvi69.GetScanCounter(bpHandle, OUT scanCount) | |
print("ScanCount at start of test: \(scanCount)") | |
print("Testing OK LED...") | |
TestLED(mvi69.MVI69_LEDID_OK) | |
print("Testing CFG LED...") | |
TestLED(mvi69.MVI69_LEDID_CFG) | |
print("Testing P1 LED...") | |
TestLED(mvi69.MVI69_LEDID_P1) | |
print("Testing P2 LED...") | |
TestLED(mvi69.MVI69_LEDID_P2) | |
print("Testing NET LED...") | |
TestLED(mvi69.MVI69_LEDID_NET) | |
print("Testing BP LED...") | |
TestLED(mvi69.MVI69_LEDID_BP) | |
_ := mvi69.GetScanCounter(bpHandle, OUT scanCount) | |
print("ScanCount at end of test: \(scanCount)") | |
print("Returning state to: GREEN...") | |
IF mvi69.SetLED(bpHandle, mvi69.MVI69_LEDID_OK, mvi69.MVI69_LED_STATE_GREEN) <> mvi69.MVI69_SUCCESS THEN | |
fatal("Failed to set LED on module.") | |
END IF | |
VAR setupMode: Number | |
IF mvi69.GetSetupJumper(bpHandle, OUT setupMode) <> mvi69.MVI69_SUCCESS THEN | |
fatal("Could not read Setup Jumper.") | |
END IF | |
IF setupMode <> 0 THEN | |
print("Setup Jumper is installed: \(setupMode)") | |
ELSE | |
print("Setup Jumper is not installed: \(setupMode)") | |
END IF | |
VAR scanMode: Number | |
IF mvi69.GetScanMode(bpHandle, OUT scanMode) <> mvi69.MVI69_SUCCESS THEN | |
fatal("Could not read operation mode of the PLC.") | |
END IF | |
IF scanMode = mvi69.MVI69_PROGRAM_MODE THEN | |
print("PLC is in PROGRAM Mode.") | |
ELSIF scanMode = mvi69.MVI69_RUN_MODE THEN | |
print("PLC is in RUN mode.") | |
END IF | |
%VAR info: mvi69.MVI69ModuleInfo := mvi69.MVI69ModuleInfo() | |
FUNCTION printModuleInfo() | |
VAR info: mvi69.MVI69ModuleInfo | |
IF mvi69.GetModuleInfo(bpHandle, OUT info) <> mvi69.MVI69_SUCCESS THEN | |
fatal("GetModuleInfo failed.") | |
END IF | |
print("VendorID\t: \(info.VendorID)") | |
print("DeviceType\t: \(info.DeviceType)") | |
print("ProductCode\t: \(info.ProductCode)") | |
print("ProductVersion\t: \(info.MajorRevision).\(info.MinorRevision)") | |
print("SerialNum\t: \(info.SerialNo)") | |
print("Module Name\t: \(info.Name)") | |
END FUNCTION | |
VAR iocfg: mvi69.MVI69IOConfig := mvi69.MVI69IOConfig() | |
FUNCTION printIOConfig() | |
IF mvi69.GetIOConfig(bpHandle, OUT iocfg) <> mvi69.MVI69_SUCCESS THEN | |
fatal("GetIOConfig failed.") | |
END IF | |
print("MappedInputWords\t: \(iocfg.MappedInputWords)") | |
print("MappedOutputWords\t: \(iocfg.MappedOutputWords)") | |
print("MsgRcvBufSize\t: \(iocfg.MsgRcvBufSize)") | |
print("MsgSndBufSize\t: \(iocfg.MsgSndBufSize)") | |
END FUNCTION | |
print("Initial Module Info:") | |
printModuleInfo() | |
setInfo() | |
print("\n\nUpdated module info:") | |
printModuleInfo() | |
print("\n\nModule IO Configuration:") | |
printIOConfig() | |
IF iocfg.MappedInputWords > mvi69.MVI69_MAX_INPUT_WORDS THEN | |
iocfg.MappedInputWords := mvi69.MVI69_MAX_INPUT_WORDS | |
END IF | |
IF iocfg.MappedOutputWords > mvi69.MVI69_MAX_OUTPUT_WORDS THEN | |
iocfg.MappedOutputWords := mvi69.MVI69_MAX_OUTPUT_WORDS | |
END IF | |
iocfg.MappedInputWords := 242 | |
iocfg.MappedOutputWords := 241 | |
ret := mvi69.SetIOConfig(bpHandle, iocfg) | |
IF ret <> mvi69.MVI69_SUCCESS THEN | |
fatal("SetIOConfig failed with error: \(ret)") | |
END IF | |
VAR data: String := "" | |
VAR bpOutput: Array<Number> := [] | |
VAR bpOutCur: Array<Number> := [] | |
VAR bOutputToConsole: Boolean := FALSE | |
VAR MsgData: Array<Number> := [] | |
io.write(io.stdout, "Passing data from Output to Input image...\n") | |
io.flush(io.stdout) | |
REPEAT | |
%print("Reading output image over backplane...") | |
ret := mvi69.ReadOutputImage(bpHandle, 0, iocfg.MappedOutputWords, OUT bpOutput) | |
IF ret <> mvi69.MVI69_SUCCESS THEN | |
fatal("ReadOutputImage failed. Error: \(ret)") | |
END IF | |
IF bpOutCur <> bpOutput THEN | |
bpOutCur := bpOutput | |
bOutputToConsole := TRUE | |
END IF | |
data := "" | |
FOR i := 0 TO 50 DO | |
data := data & "\(bpOutput[i])\t" | |
IF ((i+1) MOD 10) = 0 THEN | |
data := data & "\n" | |
END IF | |
bpOutput[i] := bpOutput[i] + 1 | |
END FOR | |
IF bOutputToConsole THEN | |
io.write(io.stdout, data & "\n") | |
io.flush(io.stdout) | |
bOutputToConsole := FALSE | |
END IF | |
%print("Writing input image to backplane...") | |
ret := mvi69.WriteInputImage(bpHandle, 0, iocfg.MappedInputWords, bpOutput) | |
IF ret <> mvi69.MVI69_SUCCESS THEN | |
fatal("ReadOutputImage failed. Error: \(ret)") | |
END IF | |
ret := mvi69.GetMsgRequestFromBp(bpHandle, OUT MsgData, iocfg.MsgRcvBufSize, 0) | |
IF ret = mvi69.MVI69_SUCCESS THEN | |
print("Got \(MsgData.size()) message words from Backplane!") | |
data := "" | |
FOR i := 0 TO MsgData.size() -1 DO | |
data := data & "\(MsgData[i])\t" | |
IF ((i+1) MOD 10) = 0 THEN | |
data := data & "\n" | |
END IF | |
END FOR | |
io.write(io.stdout, data & "\n") | |
io.flush(io.stdout) | |
END IF | |
UNTIL FALSE %mvi69.Signaled() <> mvi69.SIGNAL_NONE | |
IF mvi69.Close(bpHandle) <> mvi69.MVI69_SUCCESS THEN | |
fatal("Failed to close BP handle!") | |
END IF | |
print("Backplane closed.") | |
FUNCTION FlashLED(led, state: Number) | |
VAR r: Number := 0 | |
FOR x := 0 TO 1 DO | |
r := mvi69.SetLED(bpHandle, led, mvi69.MVI69_LED_STATE_OFF) | |
IF r <> mvi69.MVI69_SUCCESS THEN | |
print("SetLED failed! LED: \(led), State: \(state), Error: \(r)") | |
EXIT FOR | |
END IF | |
time.sleep(0.05) | |
r := mvi69.SetLED(bpHandle, led, state) | |
IF r <> mvi69.MVI69_SUCCESS THEN | |
print("SetLED failed! LED: \(led), State: \(state), Error: \(r)") | |
EXIT FOR | |
END IF | |
time.sleep(0.05) | |
END FOR | |
END FUNCTION | |
FUNCTION setInfo() | |
VAR modInfo: mvi69.MVI69ModuleInfo := mvi69.MVI69ModuleInfo() | |
% set module information object | |
modInfo.VendorID := 309 % ProSoft | |
modInfo.DeviceType := 12 % communications adapter | |
modInfo.ProductCode := 6112 % EDS product code LDM | |
modInfo.MajorRevision := 2 % major revision 2 | |
modInfo.MinorRevision := 0 % minor revision 0 | |
modInfo.Name := "MVI69E-NeonLDM" % Module Name | |
VAR r: Number := 0xFFFF | |
r := mvi69.SetModuleInfo(bpHandle, modInfo) | |
IF r <> mvi69.MVI69_SUCCESS THEN | |
fatal("Could not set module info. Error: \(r)") | |
END IF | |
END FUNCTION | |
FUNCTION TestLED(led: Number) | |
%print("Testing OK LED: RED...") | |
FlashLED(led, mvi69.MVI69_LED_STATE_RED) | |
%print("Testing OK LED: YELLOW...") | |
FlashLED(led, mvi69.MVI69_LED_STATE_YELLOW) | |
%print("Testing OK LED: GREEN...") | |
FlashLED(led, mvi69.MVI69_LED_STATE_GREEN) | |
%print("Returning state to: GREEN...") | |
END FUNCTION | |
FUNCTION fatal(errData: String) | |
print("\(errData)") | |
sys.exit(1) | |
END FUNCTION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment