我今天将GSM/GPRS RSPI module用于一个项目。我的工作是使用AT命令将文件发送到FTP服务器(通过使用Python的简单程序或通过逐个发送所有AT命令的腻子来工作)。
今天,为了简化我的代码,我选择了将代码翻译为object。 另外,我使用所有方法(例如发送SMS,connectGPRS,sendFTP ...)创建类(这些方法似乎并没有简化代码)
但是当我启动程序时,我没有收到模块的确认答复。
isReady()
启动时,程序将发送串行命令以测试模块。但是我没有任何回复。我的串行端口配置似乎正确(debug()
返回ttyAMA0),并且我可以使用Putty控制模块。但是当我与Tx和Rx短路时,我看不到Putty上程序的请求。
然后我的程序在sys.exit(0)
行中停止,ser.isReady()
返回false。
所以我的问题是:可以像在对象编程中一样使用串行端口吗?还是我的代码有误?
关于。 (顺便说一句,我的法语)
import serial
print("Reset du module")
resetModem()
time.sleep(5)
ser = ConnexionModule(SERIAL_PORT, 9600, 5)
if not ser.isReady():
print("Failed reboot, maybe a another program connected on serial, or the device isn't lauched")
sys.exit(0)
#debug() is a print function
def debug(text):
if VERBOSE:
print("Debug:---", text)
# This class is in reality in a another file imported in main
class ConnexionModule():
def __init__(self,serial_port,baudrate,timeout):
self.ser = serial.Serial(serial_port, baudrate, timeout)
# Testing if the module is ready to be used
def isReady(self):
# Resetting to defaults
cmd = 'ATZ\r'
# When i send 'ATZ' the module return 'OK'
debug("Cmd: " + cmd)
self.serialwrite(cmd,2)
reply = self.ser.read(self.ser.inWaiting())
reply = reply.decode("utf-8")
time.sleep(8) # Waiting for a reply
debug("Reply: " + reply)
return ("OK" in reply)
def serialwrite(self,cmd,slp):
debug("Sending:")
debug(self.ser.port)
debug(cmd)
self.ser.write(cmd.encode())
time.sleep(slp)
此代码有效:
import serial
print("Reset du module")
resetModem()
ser = serial.Serial(SERIAL_PORT, baudrate = 9600, timeout = 5)
if not isReady(ser):
print("Fail reboot")
sys.exit(0)
def isReady(pserial):
# Resetting to defaults
cmd = 'ATZ\r'
debug("Cmd: " + cmd)
serialwrite(pserial,cmd,2)
reply = pserial.read(pserial.inWaiting())
reply = reply.decode("utf-8")
time.sleep(8)
debug("Reply: " + reply)
return ("OK" in reply)
def debug(text):
if VERBOSE:
print("Debug:---", text)
def resetModem():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(P_RESET, GPIO.OUT)
GPIO.output(P_RESET, GPIO.LOW)
time.sleep(0.5)
GPIO.output(P_RESET, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(P_RESET, GPIO.LOW)
time.sleep(3)