【ESP8266之LUA开发】九、File操作,实现C#改变并存储工作模式,SSID与PWD的保存与读取
【摘要】
文件操作的意义
文件操作进行数据的保存,可实现启动时工作在哪一种模式,哪一种通信,以及其余需要保存在模块内部的信息。
文件操作的API调用
API查阅<这里>
写入 mode.lu...
文件操作的意义
文件操作进行数据的保存,可实现启动时工作在哪一种模式,哪一种通信,以及其余需要保存在模块内部的信息。
文件操作的API调用
API
查阅<这里>
写入 mode.lua
if file.open("mode.lua", "w+") then
file.writeline("123")
file.flush()
file.writeline("456")
file.flush()
file.writeline("789")
file.close()
else
print("open mode.lua faild")
end
注意,连续写之间要记得加上
file.flush()
读取
file.open("mode.lua", "r") then
s1= file.readline()
s2= file.readline()
s3= file.readline()
file.close()
end
-- 读取结果
s1 ="123\r"
s2 ="456\r"
s3 ="789\r"
注意,读出来会多加一个回车符\r
,因为读的时候是遇到\n
作为结束符的,API
也有说明的<这里>!
C#上位机协议说明
模式选择可直接在上位机上勾选相应的按钮,然后点击写入设置。
上位机的C#
部分代码
...
private void button2_Click(object sender, EventArgs e)
{
byte[] sendbyte = new byte[6];
sendbyte[0] = (byte)'+';//2B
sendbyte[1] = (byte)'+';
sendbyte[2] = (byte)'M';//4D
sendbyte[3] = (byte)'D';//44
sendbyte[4] = (byte)'0';//0
if (radioButton5.Checked)
{
sendbyte[5] = (byte)'0';
}
else if (radioButton6.Checked)
{
sendbyte[5] = (byte)'1';
}
else if (radioButton7.Checked)
{
sendbyte[5] = (byte)'2';
}
if (radioButtonNetCon.Checked)
{
TcpSendDataMethod(sendbyte);
}
else if (radioButtonSerialCon.Checked)
{
SerialSend(sendbyte);
}
}
...
上位机通过串口写入到8266
时的数据协议说明
++MD00 AP模式
++MD01 Station模式
++MD02 AP+Station模式
最后还有CRC16
校验码。
源码
配合C#
上位机,实现工作模式的动态设置与保存。
init.lua
gpio.mode(4,gpio.OUTPUT)
gpio.mode(2,gpio.OUTPUT)
gpio.write(4,1)
if adc.force_init_mode(adc.INIT_ADC) then
node.restart()
return
end
tmr.alarm(0, 1000, 1, function()
gpio.write(4,1-gpio.read(4))
end)
tmr.alarm(1, 5000, 0, function()
dofile("file.lua")
dofile("wifi.lua")
end)
wifi.lua
if file.open("mode.lua", "r") then
local Mode = file.readline()
if Mode == nil then
Mode="2"
end
if Mode:byte(1) == 48 then
print("Wifi MODE: SOFTAP")
wifi.setmode(wifi.SOFTAP)
elseif Mode:byte(1) == 49 then
print("Wifi MODE: STATION")
wifi.setmode(wifi.STATION)
else
print("Wifi MODE: STATIONAP")
wifi.setmode(wifi.STATIONAP)
end
file.close()
end
cfg={}
cfg = wifi.ap.getconfig(true)
if cfg.ssid == nil then
cfg.ssid="Hellow8266"
cfg.pwd="11223344"
end
print("APssid: "..cfg.ssid)
if cfg.pwd == nil then
print("APpwd: nil")
else
print("APpwd: "..cfg.pwd)
end
cfg.save=true
wifi.ap.config(cfg)
apcfg={}
apcfg = wifi.sta.getconfig(true)
if apcfg.ssid == nil then
apcfg.ssid="360"
apcfg.pwd="e903zcq567"
end
print("APssid: "..apcfg.ssid)
if apcfg.pwd == nil then
print("Stationpwd: nil")
else
print("Stationpwd: "..apcfg.pwd)
end
apcfg.save=true
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)
ConnectIP = "192.168.0.10"
ConnectPort = 8080
UdpSocket = net.createUDPSocket()
UdpSocket:listen(ConnectPort)
UdpSocketTable={}
UdpIPTable={}
UdpPortTable={}
UdpConnectCnt = 0
UdpCanConnect = 0
UdpSocket:on("receive", function(socket, data, port, ip)
UdpCanConnect = 1
for i=0,2 do
if UdpIPTable[i] == ip and UdpPortTable[i] == port then
UdpCanConnect = 0
end
end
if ip == ConnectIP and port == ConnectPort then
UdpCanConnect = 0
end
if UdpCanConnect == 1 then
UdpSocketTable[UdpConnectCnt] = socket
UdpIPTable[UdpConnectCnt] = ip
UdpPortTable[UdpConnectCnt] = port
print("\r\n"..UdpConnectCnt.."-Connect")
UdpConnectCnt = UdpConnectCnt + 1
end
if UdpConnectCnt == 3 then
UdpConnectCnt = 0
end
uart.write(0,data)
end)
UartReadCnt=0
UartReadCntCopy=0
UartReadData=""
UartReadDataCopy=""
uart.on("data",0,function(data)
UartReadCnt = UartReadCnt + 1
UartReadData = UartReadData..data
end, 0)
tmr.alarm(2, 5, 1, function()
if UartReadCnt ~=0 then
if UartReadCnt == UartReadCntCopy then
UartReadCnt = 0
UartReadCntCopy = 0
UartReadDataCopy = UartReadData
UartReadData=""
Config(UartReadDataCopy)
NetSend(UartReadDataCopy)
else
UartReadCntCopy = UartReadCnt
end
end
end)
function NetSend(data)
if UdpSocket ~= nil then
UdpSocket:send(ConnectPort,ConnectIP,data)
end
for i=0,2 do
if UdpSocketTable[i] ~= nil then
UdpSocketTable[i]:send(UdpPortTable[i],UdpIPTable[i],data)
end
end
end
printip = 0
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
printip = 0
end)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
if printip == 0 then
print("+IP"..T.IP)
end
printip = 1
end)
file.lua
接收上位机写入设置后发来的数据,并进行CRC校验,打印写入的工作模式!
mode.lua
是创建在文件内部的!
function Config(data)
local RevLen = string.len (data)
local crc = ow.crc16(string.sub(data,1,RevLen-2))
local recrc = data:byte(RevLen)
recrc = recrc*256
recrc = recrc + data:byte(RevLen-1)
if crc == recrc then
--[[8266 Mode ]]
if RevLen == 8 then
if string.sub(data,1,5) == "++MD0" then----Mode
if file.open("mode.lua", "w+") then
file.writeline(string.sub(data,6,6))--MODE
print("MODE="..string.sub(data,6,6))
file.close()
else
print("open mode.lua faild")
end
end--[[8266 Mode ]]
end
end
end
wifi.lua
中的Config(data)
是定义在file.lua
中的,可以跨文件直接调用的!
修改启动模式实验
别忘了,每次写入后,复位一下!
文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。
原文链接:recclay.blog.csdn.net/article/details/78176825
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)