MB.writeName("LUA_NO_WARN_TRUNCATION", 1) function print_UARTpins() rx_dio = MB.readName("ASYNCH_RX_DIONUM") tx_dio = MB.readName("ASYNCH_TX_DIONUM") print("TX pin: ", string.format("%i", tx_dio), ", RX pin: ", string.format("%i", rx_dio)) end function UART_Transmit(a, Bytes) local tx = {} for i = 1, a:len() do tx[i] = string.byte(a:sub(i,i)) end MB.writeName("ASYNCH_NUM_BYTES_TX", Bytes) MB.writeNameArray("ASYNCH_DATA_TX", Bytes, tx) MB.writeName("ASYNCH_TX_GO", 1) end function UART_Read() nBytes = MB.readName("ASYNCH_NUM_BYTES_RX") rx_data = MB.readNameArray("ASYNCH_DATA_RX", nBytes) return(rx_data) end function UART_Read_ASCII() rx = UART_Read() rx_str = "" -- Build a string from the resulting table; for k,v in pairs(rx) do -- array of bytes to string. rx_str = rx_str .. rx_str.char(v) end return rx_str end MB.writeName("ASYNCH_ENABLE", 0) MB.writeName("ASYNCH_TX_DIONUM", 3) MB.writeName("ASYNCH_RX_DIONUM", 2) MB.writeName("ASYNCH_BAUD", 9600) MB.writeName("ASYNCH_RX_BUFFER_SIZE_BYTES", 200) MB.writeName("ASYNCH_NUM_DATA_BITS", 8) MB.writeName("ASYNCH_NUM_STOP_BITS", 1) MB.writeName("ASYNCH_PARITY", 0) MB.writeName("ASYNCH_ENABLE", 1) LJ.IntervalConfig(0, 1000) test_data = {} -- This test only works with ASCII characters test_data[0] = "5555" test_data[1] = "777" test_data[2] = "abc 2" test_data[3] = "Test parttern A" test_data[4] = "Test parttern B" test_data[5] = "1234 Omicron" counter = 0 counter_reset = 0 while true do if LJ.CheckInterval(0) then if (counter % 2) == 1 then -- Read cycle rx_string = UART_Read_ASCII() print("rx test: ", rx_string) else -- Write cycle i_test = counter / 2 if test_data[i_test] == nil then -- End of the test data? MB.writeName("LUA_RUN", 0) -- Conclude test else nBytes = string.len(test_data[i_test]) print("tx test: ", test_data[i_test])--, string.format("%i",nBytes)) UART_Transmit(test_data[i_test], nBytes) end end counter = counter + 1 end end MB.writeName("LUA_RUN", 0)