Skip to content

Instantly share code, notes, and snippets.

@imp-guru
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imp-guru/30d5f2dc9503b1d18bc8 to your computer and use it in GitHub Desktop.
Save imp-guru/30d5f2dc9503b1d18bc8 to your computer and use it in GitHub Desktop.
Read a UART via Agent Function
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"))
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