2.2 串口测试
串行接口(Serial Interface) 是指数据一位一位地顺序传送,其特点是通信线路简单,只要一对传输线就可以实现双向通信(可以直接利用电话线作为传输线),从而大大降低了成本,特别适用于远距离通信,但传送速度较慢。一条信息的各位数据被逐位按顺序传送的通讯方式称为串行通讯。串行通讯的特点是:数据位的传送,按位顺序进行,最少只需一根传输线即可完成;成本低但传送速度慢。串行通讯的距离可以从几米到几千米;根据信息的传送方向,串行通讯可以进一步分为单工、半双工和全双工三种。下面的网址提供了串口的历程。
https://github.com/JetsonHacksNano/UARTDemogit clone
同样我们直接手动把历程下来运行,然后拷贝到任意文件夹。这里串口和GPIO用的并不是一个库,所以我们要先导包。输入如下指令。
sudo apt-get install python3-serial
包成功的被下载了下来,后面我们进行接线。
前面的IO接口图明确的指出了pin8、pin10是串口引脚,所以我们需要用线材链接这两个管角。这里的电平是3.3v的,英伟达的原话如下“One easy way to use the script is to connect the Jetson Nano to a PC/Mac/Linux box via a TTL to USB cable. The Jetson Nano signal is 3.3V. Run a serial tty program on the PC to interface with the serial port, and then interact with the Jetson Nano.”。接线图如下。
在电脑上打开串口助手,配置波特率为115200,直接跑历程指令如下。
sudo python3 uart_example.py
在电脑端可收到通信的信息,电脑端发送也可以及时的回复并且显示出来,效果如下图所示。
下面我们同样分析一下这个历程,前面说过了串口和GPIO没有封装在一个库里,这里调用串口的方式是:Import serial。之后调用函数为串口初始化参数,然后等待了1秒。
import time
import serial
print("UART Demonstration Program")
print("NVIDIA Jetson Nano Developer Kit")
serial_port = serial.Serial(
port="/dev/ttyTHS1",
baudrate=115200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
)
# Wait a second to let the port initialize
time.sleep(1)
try:
# Send a simple header
serial_port.write("UART Demonstration Program\r\n".encode())
serial_port.write("NVIDIA Jetson Nano Developer Kit\r\n".encode())
while True:
if serial_port.inWaiting() > 0:
data = serial_port.read()
print(data)
serial_port.write(data)
# if we get a carriage return, add a line feed too
# \r is a carriage return; \n is a line feed
# This is to help the tty program on the other end
# Windows is \r\n for carriage return, line feed
# Macintosh and Linux use \n
if data == "\r".encode():
# For Windows boxen on the other end
serial_port.write("\n".encode())
except KeyboardInterrupt:
print("Exiting Program")
except Exception as exception_error:
print("Error occurred. Exiting Program")
print("Error: " + str(exception_error))
finally:
serial_port.close()
pass
总结一下本历程中用到的串口函数:
serial_port.write("UART Demonstration Program\r\n".encode())
serial_port.inWaiting()
serial_port.close()
2.3 time库的使用
在前面两节的历程测试中我们都调用到了time库进行延时,这节在电脑上的pycharm环境上测试一下这个库的功能。这里先装上python,下面的指令是对深度学习框架的换源。
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
简单来说time库就是python用来处理时间的标准库。
1.计算机时间的表达。
2.提供获取系统时间并格式化输出的功能。
3.提供系统级精确计时功能,用于程序性能分析。
为了测试pycharm我写了一下的demo,运行结果如图
import time
print("time test start")
a=time.time()
b=time.ctime()
print(a)
print(b)
start = time.perf_counter()
time.sleep(1)
end = time.perf_counter()
c=end - start
print(c)
print("time test end")
在很多程序的尾部都会有if __name__ == '__main__':这个语句,这是因为一个python文件通常有两种使用方法,第一是作为脚本直接执行,第二是 import 到其他的 python 脚本中被调用(模块重用)执行。因此 if __name__ == 'main': 的作用就是控制这两种情况执行代码的过程,在 if __name__ == 'main': 下的代码只有在第一种情况下(即文件作为脚本直接执行)才会被执行。
所以官方的测试用例经常使用如下的结构,防止被错误的引用。
def main():
if __name__ == '__main__':
main()