UART data error when using uart.alt(1)

痞子三分冷 提交于 2019-12-11 13:48:10

问题


I am trying to acquire rs232 data from a device connected to the ESP8266 (data will then be sent our via http/wifi).

I am using max3232 IC to provide the necessary 3.3v TTL to the ESP8266.

I have have connected the max3232 (pin 12) to GPIO pin 13 (rx) on the ESP8266 (I am only receiving data not sending data, so only the rx pin is connected).

The code i am using:

--
--file: test2.lua 
--
tst2 = require "tst2"
tst2.start()


--tst2.lua (testing script)
local module = {}

function module.start()
    print("in tst2.start")
    uart.alt(1)         --use alt GPIO pin 13 (Rx)
    uart.setup(0, 9600,8, uart.PARITY_NONE, uart.STOPBITS_1,0)
    uart.on("data",10, 
        function(data) 
            file.open("data.tmp", "w+")
            file.writeline("starting")
            for i=1,10 do
                 file.writeline(string.byte(string.sub(data,i,i)) )
            end
            file.writeline("from uart: ", data)
            file.writeline("finished")
            file.close()
    end, 0)
    uart.alt(0)         --switch back to standard Rx/Tx pins
end
return module

The rs232 device connected to the ESP8266 is putting out a single alphabetic character every 3 seconds, however the data written to file (data.tmp) is as follows

starting
10
13
10
13
10
13
10
13
10
13
from uart: 
finished
file.close()

Problems:

1- The rs232 device is not issuing any newln or cr characters, but these are appearing in the data file.

2- the string "file.close()" is being written to the data file, and looks like it is the actual lua command that follows the final file.writeline command.

3- the alphabetic data is not appearing in the data file.

4- switching back to the standard uart pins via uart.alt(0) does not work (the ESP8266 must be rebooted - this is not a major issue as the standard uart pins are only used during debugging).

I am writing the rs232 data to a file instead of simply printing it out on the screen (I am using ESPlorer v0.2.0) because the uart.alt(1) command redirects the serial port to the alternative ESP8266 gpio pins.

I think I am doing something fundamentally wrong with the uart set up, but i can't tell what it is.


回答1:


SOLVED:

It appears that you can't connect the ESP8266 to both the serial port for debugging (e.g. the serial port on a pc running ESPlorer) and also have the alternate serial pins (ESP8266 GPIO 13 and 15) connected (to an external serial device) at the same time.

The nodemcu uart.alt() function does not appear to "turn off" the standard serial i/o pins.

Disconnecting the pc from the standard serial i/o pins solved the problem (debugging becomes an issue, but there are work-arounds to resolve this).




回答2:


(updated) one workaround is to use a simple telnet server to interact with the lua interpreter. you can either connect the ESP8266 to your wifi router or, even better, set it up as an access point (AP) so that all you have to do is to connect your computer to it and then simply telnet in (to the gateway's IP). so, in addition to the telnet code, you'll need set up the AP in your init.lua. full code for the telnet server and the AP setup is below. A nice benefit is that I can program and monitor the ESP8266 from my phone using an off-the-shelf telnet app!

jj =  [[
 sock = 22 -- just a placeholder, so it stays global. may not be needed.
 -- use sock:send("hello") to insert your own custom output to the client.
 telnet_srv = net.createServer(net.TCP, 180)
 telnet_srv:listen(2323, function(socket)
     local fifo = {}
     local fifo_drained = true

     local function sender(c)
         if #fifo > 0 then
             c:send(table.remove(fifo, 1))
         else
             fifo_drained = true
         end
     end

     local function s_output(str)
         table.insert(fifo, str)
         if socket ~= nil and fifo_drained then
             fifo_drained = false
             sender(socket)
         end
     end
     sock = socket -- make the socket globally available.

     node.output(s_output, 0)   -- re-direct output to function s_ouput.

     socket:on("receive", function(c, l)
         node.input(l)           -- works like pcall(loadstring(l)) but support multiple separate line
     end)
     socket:on("disconnection", function(c)
         node.output(nil)        -- un-regist the redirect output function, output goes to serial
     end)
     socket:on("sent", sender)

     print("Welcome to NodeMCU world.")
 end)
]]

file.open("telnet.lua", "w")
file.write(jj)
file.close()


jj = [[
 wifi.setmode(wifi.STATIONAP);
 wifi.ap.config({ssid="ESPtest",pwd=""});
 print("Server IP Address:",wifi.ap.getip())
 dofile("telnet.lua")
]]

file.open("init.lua","w")
file.write(jj)
file.close()

node.restart()

output:

Server IP Address:  192.168.4.1 255.255.255.0   192.168.4.1
> 


来源:https://stackoverflow.com/questions/36749282/uart-data-error-when-using-uart-alt1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!