SIM7020C之TCP/IP通信

文章目录

  • 一、简介
  • 二、详细内容
    • 1、硬件连接
    • 2、串口初始化
    • 3、树莓派minicom调试
    • 4、C程序编译
    • 5、TCP/IP通信


一、简介

  本文是对SIM7020C的初步使用说明,通过连接至树莓派,通过python代码控制,SIM7020C是一款具有NB-IoT(窄带物联网)功能的树莓派扩展板,具有低功耗、低成本、广覆盖等优点,适用于新型的智能仪表和远程控制等物联网应用。
SIM7020C之TCP/IP通信_第1张图片

二、详细内容

1、硬件连接

SIM7020C之TCP/IP通信_第2张图片
  直接摁上就可以,然后将天线转出
SIM7020C之TCP/IP通信_第3张图片

2、串口初始化

SIM7020C之TCP/IP通信_第4张图片
  关于串口的配置,这里我并没有使用官网提供的方法,而是通过树莓派使用mini串口与外部进行通信该篇博客,将/dev/ttyAMA0/dev/ttyS0替换了位置
SIM7020C之TCP/IP通信_第5张图片

3、树莓派minicom调试

SIM7020C之TCP/IP通信_第6张图片
  所以这里minicom调试也相应地改变了,输入下列指令,然后正常操作即可

minicom -D /dev/ttyAMA0

SIM7020C之TCP/IP通信_第7张图片

4、C程序编译

SIM7020C之TCP/IP通信_第8张图片
SIM7020C之TCP/IP通信_第9张图片
  按照以上步骤,我并没有完成编译,一开始是缺少aclocal-1.13,这个问题通过执行:autoreconf -vfi ,再重新编译,就可以解决,但是会报出其他问题,于是就放弃了这种方式,采用了python测试
SIM7020C之TCP/IP通信_第10张图片

5、TCP/IP通信

#!/usr/bin/python

import serial
import time

ser = serial.Serial('/dev/ttyAMA0',115200)

# Open Serial 
def ser_init():
	if ser.isOpen == False:
		ser.open()                # 打开串口
	ser.flushInput()
	rec_buff = ''

# Execute AT Command by SIM7020C
def send_at(command,back,timeout):
	rec_buff = ''
	ser.write((command+'\r\n').encode())
	time.sleep(timeout)
	if ser.inWaiting():
		time.sleep(0.1 )
		rec_buff = ser.read(ser.inWaiting())
	if rec_buff != '':
		if back not in rec_buff.decode():
			print(command + ' ERROR')
			print(command + ' back:\t' + rec_buff.decode())
			return 0
		else:
			print(rec_buff.decode())
			return 1
	else:
		print(command + ' no responce')
		return 0

# Creat a Socket Connection
def send_init():
	if True == send_at('AT+CSOC=1,1,1','OK',1):
		print('Created TCP socket id 0 Successfully!')
		conn = send_at('AT+CSOCON=0,2222,\"192.168.1.1\"','OK',2)
	return conn

# Close Socket Connection
def send_close():
	send_at('AT+CSOCL=0','OK',1)
	send_at('AT+CSOCON?','OK',1)
	print('Close Socket')
    

if __name__ == "__main__":
	try:
		ser_init()  
		conn = send_init()
		while conn == True:
			send_at('AT+CSOSEND=0,0,\"Waveshare Send to Socket id 0 using TCP\"','OK',2)
		send_close()
		
	except KeyboardInterrupt:
		if ser != None:
			ser.close()
			
	if ser != None:
		ser.close() 

你可能感兴趣的:(树莓派,SIM7020C,NBIOT,Python)