我正在尝试使用NodeMCU将string.format原始输出输出到uart。
我正在尝试该功能
uart.write(0,string.format("loop %03d local: %02d | gmt %02d:%02d:%02d local %02d/%02d/%04d\n",loops,timezonetime,gmthours,gmtmins,gmtsecs,Nmonth,Nday,Nyear))
,但\ n被忽略,并且文本被串联。
print(string.format("loop %03d local: %02d | gmt %02d:%02d:%02d local %02d/%02d/%04d",loops,timezonetime,gmthours,gmtmins,gmtsecs,Nmonth,Nday,Nyear))
工作正常,但是我无法控制总是由print()添加的换行符
如何使用uart.write和string.format来控制输出,包括换行符和其他控制字符的放置和使用?
答案 0 :(得分:0)
该问题是由于用于访问NodeMCU板的LuaLoader中换行处理的结果。当与PUTTY一起使用时,输出是预期的。
这是更详细的测试结果。看来\ r在传递给uart.write()的字符串参数中不起作用
-- uart.write Test
print("______first test____________") -- prime the output with a line and newline
uart.write(0,"asdfasdfasdfasdfasdf") -- no newline
print("______should be at end of same line as asdf...______")
uart.write(0,"asdfasdfasdfasdfasdf(newline)\n") -- with newline
print("______should be on line following asdf...____________")
uart.write(0,"asdfasdfasdfasdfasdf(CR)\r") -- with return only
uart.write(0,"OVERWRITE\n") -- overwrite the first part of asdf line, then newline
print("______should be on newline below OVERWRITE line ____________")
输出结果:
dofile("uwtest.lua")
______first test____________
asdfasdfasdfasdfasdf______should be at end of same line as asdf...______
asdfasdfasdfasdfasdf(newline)
______should be on line following asdf...____________
asdfasdfasdfasdfasdf(CR)
OVERWRITE
______should be on newline below OVERWRITE line ____________
>
预期结果是字符串“ asdfasdfasdfasdfasdf(CR)\ r”后跟一个CR而不是LF,从而导致终端光标向左移动
这似乎与LuaLoader中的终端仿真有关。
当我用腻子连接到NodeMCU时,得到以下输出:
> dofile("uwtest.lua")
______first test____________
asdfasdfasdfasdfasdf______should be at end of same line as asdf...______
asdfasdfasdfasdfasdf(newline)
______should be on line following asdf...____________
OVERWRITEsdfasdfasdf(CR)
______should be on newline below OVERWRITE line ____________
>
腻子输出符合预期。