使用Python和Arduin从SIM900D GSM模块发送的消息不完整

2024-06-26 18:01:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python从文本文件中读取行,然后通过串行端口将字符串发送到arduino。然后字符串被传递到SIM900D GSM模块,该模块将这些字符串作为01合并文本消息发送给我选择的多个用户。问题是,对于文本消息的前06行,代码工作正常。但是,如果我添加第7行,那么我不会收到短信。你知道吗

python代码如下:

import os, time, shutil, serial, sys, glob                  
source_fld = "C:\Users\TOSHIBA\Desktop\Processing";                 #source folder
dest_fld = "C:\Users\TOSHIBA\Desktop\Processed";                    #destination folder

os.chdir('C:\Users\TOSHIBA\Desktop\Processing')                     #change to current working directory
filenames = os.listdir(os.curdir)                                   #put all the names of the files in a string

lines_counter = 0                                                  #for all the lines of the message
my_body = [lines_counter]                                              #declaring an array

for filename in filenames:                                          #use for loop for opening the files by extension
    arduino = serial.Serial("COM3",9600,timeout = 5)                #initializing the arduino
    time.sleep(2)                                                   #2 seconds of sleep time
    arduino.flush()                                                 #clear the internal buffer with flush
    text_file = open(filename, "r")                                 #opening the text file
    my_string = text_file.readline()                                #reading the lines from the text file
    st_string = my_string.strip("\n")                               #strip the new line character from the end of the string   
    numbers = st_string.split(",")                                  #split the comma seperated values in the contact numbers field  
    size = len(numbers)                                             #find out the length of the array
    sizerange = 0                                                   #To start the counter from zero

    while (sizerange != size):                                      #while loop for printing out the values of the contact numbers array one by one

        sum_num = numbers                                           #declaring a seperate array for manipulation
        sum_num[sizerange] = numbers[sizerange].strip("\n") + "\r"  #stripping the new line character from each element of the array and adding carriage return
        print(sum_num[sizerange])                                   #array elements printing okay
        #arduino.write(sum_num[sizerange])                           #write the data to arduino
        sizerange = sizerange + 1                                   #increment the value of sizerange

    trip = str(size)
    print(trip)
    arduino.write(trip)                                             #sending the size of array
    time.sleep(1)

    line1 = text_file.readline()
    arduino.write(line1)
    time.sleep(2)

    line2 = text_file.readline()                                      
    arduino.write(line2)
    time.sleep(2)

    line3 = text_file.readline()                                      
    arduino.write(line3)
    time.sleep(2)

    line4 = text_file.readline()                                      
    arduino.write(line4)
    time.sleep(2)

    line5 = text_file.readline()                                      
    arduino.write(line5)
    time.sleep(2)

    line6 = text_file.readline()
    arduino.write(line6)
    time.sleep(2)


    arduino.close()

    text_file.close()                                               #closing the text file
    time.sleep(2)                                                   #sleep for 2 seconds    

for txt_file in glob.glob(source_fld+"\\*.txt"):                    #moving all the text files to destination folder
    shutil.move(txt_file, dest_fld)
print("All files have been moved")    

sys.exit()                                                              #kill the python function

Arduino代码如下:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(7, 8);
String msg_counter;
String msg_line1,msg_line2,msg_line3,msg_line4,msg_line5,msg_line6,msg_line7,msg_line8,msg_line9,msg_line10,msg_line11,msg_line12,msg_line13,msg_line14,msg_line15;
int counter;
int do_it = 1;

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  mySerial.begin(9600);
  Serial.begin(9600);

}

void loop() {

  while(!Serial.available()){}      //stay here until there is data available on the Serial port
  while(Serial.available()){
    if(Serial.available()>0){
      msg_counter = Serial.readString();
      delay(2000);
      msg_line1 = Serial.readString();
      delay(2000);
      msg_line2 = Serial.readString();
      delay(2000);
      msg_line3 = Serial.readString();
      delay(2000);
      msg_line4 = Serial.readString();
      delay(2000);
      msg_line5 = Serial.readString();
      delay(2000);
      msg_line6 = Serial.readString();
      delay(2000);
    }
  }
counter = msg_counter.toInt();
if(counter == 2){ 
  SendMessage();
  digitalWrite(LED_BUILTIN, HIGH); 
}


}

void SendMessage()
{
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"+9XXXXXXXXXXX\"\r"); 
  delay(1000);
  mySerial.println(msg_line1);
  mySerial.println(msg_line2);
  mySerial.println(msg_line3);
  mySerial.println(msg_line4);
  mySerial.println(msg_line5);
  mySerial.println(msg_line6);
  //mySerial.println(msg_line7);
  delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}

我正在阅读的文本文件包含以下内容:

AT+CMGS="0xxxxxxxxxx",AT+CMGS="0xxxxxxxxxx"
Dear Customer,
Following Call has been logged:
Call No:   1151
Call Created : 8/30/2017 4:07:02 PM
Customer: INOR
Equipment Name: SOMATOM Emotion
Equipment Serial No: 93851
Issue Description: Commissioning data, PPM.
CSE: Imran Khan
Contract Status: Warranty

Regards,
ABCD Customer Care Center
UAN:  +92 42 111 74 36 36
Email:  hc.customercare.pk@abcd.com

有人能提出问题所在吗?SIM900D在SMS中可以发送的行数和字符数方面是否有任何限制?你知道吗


Tags: ofthetexttimecounterserialmsgsleep