动手学树莓派第12章:调试器在手,想看什么看什么

软件调试

软件调试是剔除软件中bug,加速软件开发的必经过程。
我们调试方式有很多,我们当前我演示我们常用2种调试方式:通过prinf输出需要查看的信息,通过专用调试器(例如python中的pdb)进行调试。

简单、粗暴的printf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#使用printf进行代码调试
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
 
if __name__ == "__main__"
    try:
         
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        for i in range(0,8):
            #显示0~7数组
            SAKS.digital_display.show(("%4d" % i).replace(' ','#'))
             
            print("当前显示的i值:" + str(i))
             
            time.sleep(3)
             
        print("程序运行结束")
             
         
    except KeyboardInterrupt:
        print("任务被终止了")

更精准的调试器pdb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#使用printf进行代码调试
import time
from sakshat import SAKSHAT
from sakspins import SAKSPins as PINS
import pdb
 
if __name__ == "__main__"
    try:
         
        #启动pdb服务
        pdb.set_trace()
 
        #Declare the SAKS Board
        SAKS = SAKSHAT()
         
        for i in range(0,8):
            #显示0~7数组
            SAKS.digital_display.show(("%4d" % i).replace(' ','#'))
             
            print("当前显示的i值:" + str(i))
             
            time.sleep(3)
             
        print("程序运行结束")
             
         
    except KeyboardInterrupt:
        print("任务被终止了")

课程 bilibili 视频地址:https://www.bilibili.com/video/av71878718/?p=23

返回课程目录

课程 gitee 地址:https://gitee.com/shirf_taste_raspi/shirf_serial_share