Last active
August 29, 2015 14:15
-
-
Save imp-guru/30d5f2dc9503b1d18bc8 to your computer and use it in GitHub Desktop.
Read a UART via Agent Function
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
uart_response <- "dummy" | |
device.on("resp", function(data) { | |
server.log(data) | |
}); | |
function readUart(cmd) { | |
device.send("req", cmd) | |
// Blocking wait to receive response from the imp | |
device.on("resp", function(data) { | |
uart_response = data | |
}); | |
return uart_response | |
} | |
server.log(readUart("adc")) |
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
uart <- hardware.uart12; | |
rxBuffer <- "" | |
local soh = 0x01 | |
local etx = 0x03 | |
function dataRx() { | |
local i = 0 | |
local c = 0 | |
//Pull a byte from the UART FIFO | |
local uart_blob = uart.readblob() | |
for (i=0;i<uart_blob.len();i++) | |
{ | |
c = uart_blob[i] | |
//server.log(c) | |
if (c != etx) | |
{ | |
if (c != soh) | |
{ | |
rxBuffer += format("%c",c); | |
} | |
} | |
else | |
{ | |
agent.send("resp", rxBuffer) | |
} | |
} | |
} | |
function getRequest(cmd) { | |
server.log("Requesting"+cmd) | |
rxBuffer = "" | |
uart.write(soh); | |
uart.write(cmd); | |
uart.write(etx); | |
} | |
agent.on("req", getRequest); | |
uart.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS, dataRx); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment