| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- --- ykm_rtu 串口驱动
- -- @module
- -- @author lcg
- -- @release 2018.09.10
- require 'utils'
- require "ril"
- require "update32"
- -- 设置本文件被全体可见:
- module(..., package.seeall)
- --文件句柄
- --文件名称
- local filename = "rtthread.rbl"
- local SOH = 0x01
- local STX = 0x02
- local EOT = 0x04
- local ACK = 0x06
- local NAK = 0x15
- local CAN = 0x18
- --各种要用到的函数
- local i = 1
- local update_filesize = 0
- local message = {cmd = "", ext = {}}
- --- 上传预警信息
- -- t:type
- -- s:status
- function upwarn(t,s)
- log.warn("------------------------t,:",t)
- log.warn("------------------------s,:",s)
- local dat
- -- 设备上报数据表
- local data = {
- type = t,
- status = s,
- }
- message.cmd = "warn"
- message.ext = data
- dat = json.encode(message)
- log.info("upload warn:", dat)
- return dat
- end
- --计算crc
- local function calcrc(s)
- local crc = 0
- for i=1,s:len() do
- crc = bit.bxor(crc, bit.lshift(s:byte(i), 8))
- for j=1,8 do
- if crc >= 0x8000 then
- crc = bit.bxor((bit.lshift(crc, 1) % 0x10000), 0x1021)
- else
- crc = bit.lshift(crc, 1) % 0x10000
- end
- end
- crc = crc % 0x10000
- end
- return math.floor(crc/0x100),crc%0x100
- end
- --按位取反,1字节
- local function bnot(n)
- local r = 0
- for i=1,8 do
- local b = bit.rshift(n, 8-i) % 2
- b = b == 1 and 0 or 1
- r = r + bit.lshift(b, 8-i)
- end
- log.warn("r----------------------",r)
- return r
- end
- --取回文件大小
- local function getFilesize()
- local s = io.fileSize("/rtthread.rbl")
- return s
- end
- --字符串转16进制
- local function bin2hex(s)
- s = string.gsub(s,"(.)",function(x) return string.format("%s",string.byte(x)) end)
- return s
- end
- --16进制转字符串
- function hex2str(hex)
- if type(hex) ~= string then log.warn("not string data!!!")
- return
- end
- --拼接字符串
- local index = 1
- local ret = ""
- for index=1,hex:len() do
- ret = ret..string.format("%02X",hex:sub(index):byte())
- end
- return ret
- end
- --xmodem传头
- local function xmodem(data,n)
- local data = data..
- string.rep(string.char(0),
- --1024-data:len())
- 128-data:len())
- log.warn("data is:",data);
- --return string.char(STX,n,bnot(n))..
- return string.char(SOH,n,bnot(n))..
- data..
- string.char(calcrc(data))
- end
- --ymodem传文件
- local function ymodem(data,n)
- log.warn("start trans data!!!")
- local data = data..
- string.rep(string.char(0x1a),
- --1024-data:len())
- 128-data:len())
- --return string.char(STX,n,bnot(n))..
- return string.char(SOH,n,bnot(n))..
- data..
- string.char(calcrc(data))
- end
- --发某个数据
- local function sendData(s)
- uart.write(1,s)
- local cnt = 500
- local result, d
- while true do
- cnt = cnt - 1
- d = ""
- result, d =sys.waitUntil("YM_IS_OK")
- -- local d = uart.read(1,"*l")
- -- if s == string.char(EOT) and d~=nil then log.warn("will end!!!",d) uart.write(2,s) end
- if d:byte() == ACK or d:find("C") then return true end--收到了 --是否需要应答
- if d:byte() == NAK then
- log.warn("------------------NAK-----------------")
- return true end--没收到,再发一遍
- if d:len() == 0 and cnt == 0 then break end
- sys.wait(30)
- end
- end
- -- 清空串口的接收缓冲区:
- local function flush_uart_tmp()
- local clear_uart_tmp = uart.read(1,'*l')
- log.error("****************************clear_uart_tmp:",clear_uart_tmp)
- end
- --串口读数据[[
- -- l:读取到结束字符\n或者阻塞发送
- -- n:读取整型数据
- -- s:读取到空格字符
- -- 数字,number类型:只读取number长度的数据
- -- ]]
- -- 线程
- sys.taskInit(
- function()
- --初始化串口
- uart.setup(1,9600,8,uart.PAR_NONE,uart.STOP_1,nil,1)
- uart.write(1,filename)
- local downnum = 20
- local result,d
- local update_timeout = 0
- --
- local FD = io.open("/rtthread.rbl","rb")
- --local Core_fd = io.open("/update.bin","rb") --该句可以删除
- if FD then
- log.info("升级文件存在!!!!")
- FD:close()
- os.remove("/rtthread.rbl")
- end
- -- if Core_fd then
- -- Core_fd:close()
- -- os.remove("/update.bin") --该句可以删除
- -- end
- while true do
- log.warn("20S后开始下载....")
- for i = 0,downnum do
- log.warn("start connect Ymoem ....",i)
- log.info("size is ",i,rtos.get_fs_free_size())
- log.info("RAM_USEAGE", collectgarbage("count") .. " KB")
- sys.wait(1000)
- end
- while true do
- -- local d = uart.read(1,"*s")
- -- if CBDmqtt.recvBuff[1] then
- -- local d = table.concat(CBDmqtt.recvBuff[1])
- -- end
- if update32.updateok == 1 then
- update_timeout = update_timeout + 1
- fileHandle = io.open("/rtthread.rbl","rb")
- --if update_timeout== 90 then
- if update_timeout== 90 then
- fileHandle:close()
- os.remove("/rtthread.rbl")
- scd.updateflag = 0
- update_timeout = 0
- update32.updateok= 0
- --s = CBDmqtt.upwarn("device_stm32", "system is update timeout!!!")
- s = scd.upwarn("device_stm32", "system is update timeout!!!")
- --该句是否有用
- sys.publish("NET_SENT_RDY_", s)
- sys.wait(1000)
- sys.restart("update timeout!!!")
- end
- end
- result, d=sys.waitUntil("YM_IS_OK",1000)
- if d and d:find("C") and fileHandle then --该句是否有应答
- log.warn("********************************connect ok!!!*****************************")
- break
- else
- log.warn("wait ymodem connect.............",update32.updateok,update_timeout)
- end
- --flush_uart_tmp()
- log.info("-------size is------ ",rtos.get_fs_free_size())
- end
- log.warn("start download!!!")
- local fileHandle = io.open("/rtthread.rbl","rb")
- if not fileHandle then
- log.error("OPEN FAILED!!!!!!!")
- return
- else
- update_filesize = io.fileSize("/rtthread.rbl")
- log.warn("file size is:------------",update_size)
- end
- log.warn("downloading"..filename)
- --发送文件名
- if not sendData(xmodem(filename..string.char(0)..tostring(update_filesize),0)) then
- --audio.play(30, "FILE", "/lua/updatefailed.mp3", volume) --该句可以删除
- log.warn("send file name fail") return
- end
- log.warn("send file name success!!")
- local pack = 1
- while true do
- if pack == 1 then
- print(fileHandle:seek("set")) --0
- end
- print(fileHandle:seek("cur"))--从当前位置开始
- --local data = fileHandle:read(1024)
- local data = fileHandle:read(128)
- if data == nil then log.info("file send ok!!!") break end
- log.warn("data len",data:len())
- print(fileHandle:seek("cur"))--从当前位置开始
- -- local d = data:sub(p,p+1023)
- -- log.warn("d:len()",d:len())
- -- p = p + 1024
- --log.warn((p > data:len() and data:len() or p).."/"..data:len())
- local sd = ymodem(data,pack%256)
- --local sd = ymodem(data,pack)
- if not sd:len() then break end
- log.warn("start trans data !!!",sd:len())
- if not sendData(sd) then
- log.warn("send file fail") return
- end
- --if data:len() < 1024 then fileHandle:close() break end --读完了
- if data:len() < 128 then fileHandle:close() break end --读完了
- pack = pack + 1
- --sys.wait(1)
- end
- print("trans end")
- if not sendData(string.char(EOT)) then--结束该文件,第一个EOT
- log.warn("end file fail1") break
- end
- if not sendData(string.char(EOT)) then--结束该文件,第二个EOT
- log.warn("end file fail2") break
- end
- log.warn("end file success!!!")
- if not sendData(xmodem("".. string.char(0),0)) then--结束
- log.warn("last packet fail") break
- end
- local s = upwarn("update_stm32", "stm32 system update is ok!!") --平台上报升级成功消息
- sys.publish("NET_SENT_RDY_", s)
- sys.wait(2000)
- os.remove("/rtthread.rbl")
- log.warn("all done, reboot")
- sys.restart("STM32 upgrade completes the reboot!!!") -- STM32升级完成重启
- -- sys.publish("UP_IS_OK")
- update32.updateok = 0
- scd.updateflag = 0
- log.info("flag is ",scd.updateflag)
- sys.wait(10)
- end
- end)
|