16

ADH8066 (Sparkfun) GSM モジュールを Arduino Uno に接続し、Arduino と GSM モジュールの間で正しいシリアルを取得しようとしています。直接接続すると(USBまたはTTLラインのみで)正常に動作しますが、Arduino経由で制御すると動作しません。一部のテキストは正しく出力され、残りはボーレートが間違っているかのように文字化けしますが、PC から接続するときと同じボー (115200) を使用しているだけです。

私が使用しているArduinoコードは次のとおりです。

#include <SoftwareSerial.h>

#define rxPin 7
#define txPin 8
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

// EN: String buffer for the GPRS shield message
String SmsStorePos = String("");
String msg = String("");
String snTmp = String("");
String snFull = String("");

// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;

// EN: Pin of the LED to turn ON and OFF depending on the received message
int ledPin = 5;
int powerPin = 6;

void setup()
{
  mySerial.begin(115200);               // the GPRS baud rate   
  mySerial.print("\r");
  delay(1000);
  Serial.begin(115200);                 // the Arduino IDE serial
  Serial.println("Started!");

  pinMode( ledPin, OUTPUT ); 
  digitalWrite( ledPin, LOW ); 

  pinMode( powerPin, OUTPUT);
  digitalWrite(powerPin, LOW);    // brings ONKEY low to turn on the modem (aka pressing the ONKEY)
  delay(3000);
  digitalWrite(powerPin, HIGH);    // sets the pin HIGH again (restore 5V)
  delay(5000);

  // test LED pin
  digitalWrite ( ledPin, HIGH);
  delay(1000);
  digitalWrite( ledPin, LOW); 
}

void loop()
{
    char SerialInByte;

    // Send anything we receive from the IDE to the modem
    if(Serial.available())
    {
       mySerial.print((unsigned char)Serial.read());
     }  
    else  if(mySerial.available())
    {
        char SerialInByte;
        SerialInByte = (unsigned char)mySerial.read();
        //SerialInByte = mySerial.read();

        // EN: Relay to Arduino IDE Monitor
        Serial.print( SerialInByte );

        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------

        // EN: If the message ends with <CR> then process the message
        if( SerialInByte == 13 ){
          // EN: Store the char into the message buffer
          ProcessGprsMsg();
         }
         if( SerialInByte == 10 ){
            // EN: Skip Line feed
         }
         else {
           // EN: store the current character in the message string buffer
           msg += String(SerialInByte);
         }
     }   
}

// EN: Make action based on the content of the SMS. 
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
  sms.toLowerCase();
  Serial.print( "ProcessSms for [" );
  Serial.print( sms );
  Serial.println( "]" );

  if( sms.indexOf("on") >= 0 ){
    digitalWrite( ledPin, HIGH );
    Serial.println( "LED IS ON" );
    return;
  }
  if( sms.indexOf("off") >= 0 ){
    digitalWrite( ledPin, LOW );
    Serial.println( "LED IS OFF" );
    return;
  } else {
    mySerial.print("AT+CMGF=1\r");    //Because we want to send the SMS in text mode
    delay(1000);
    mySerial.print("AT+CMGS=\"");
    mySerial.print(snFull);
    mySerial.print("\"\r");
    delay(1000);
    mySerial.print("Unknown Command: ");
    mySerial.print(sms);
    mySerial.print("\r");
    delay(1000);
    mySerial.write(0x1A);  //Equivalent to sending Ctrl+Z     
    return;
  }
}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
  mySerial.println( "AT+CMGF=1" );
}

void GprsReadSmsStore( String SmsStorePos ){
  // Serial.print( "GprsReadSmsStore for storePos " );
  // Serial.println( SmsStorePos ); 
  mySerial.print( "AT+CMGR=" );
  mySerial.println( SmsStorePos );
}

// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
  msg = "";
}

// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
  Serial.println("");
  Serial.print( "GPRS Message: [" );
  Serial.print( msg );
  Serial.println( "]" );

  if( msg.indexOf( "Call Ready" ) >= 0 ){
     Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
     GprsTextModeSMS();
  }

  // EN: unsolicited message received when getting a SMS message
  if( msg.indexOf( "+CMTI" ) >= 0 ){
     Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     SmsStorePos = msg.substring( iPos+1 );
     Serial.print( "SMS stored at " );
     Serial.println( SmsStorePos );

     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }

  // EN: SMS store read via UART (result of GprsReadSmsStore request)  
  if( msg.indexOf( "+CMGR:" ) >= 0 ){
    // get number of sender
    int snPos = msg.indexOf("+1");
    Serial.print("SMS From: ");
    snTmp = msg.substring(snPos+1);
    snFull = "";
    for (int i = 0; i < 11; i++){
      snFull += snTmp[i];    
    }
    Serial.println(snFull);

    // EN: Next message will contains the BODY of SMS
    SmsContentFlag = 1;
    // EN: Following lines are essentiel to not clear the flag!
    ClearGprsMsg();
    return;
  }

  // EN: +CMGR message just before indicate that the following GRPS Shield message 
  //     (this message) will contains the SMS body
  if( SmsContentFlag == 1 ){
    Serial.println( "*** SMS MESSAGE CONTENT ***" );
    Serial.println( msg );
    Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
    delSMS();
  }

  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0; 
}
void delSMS() {
  mySerial.print("AT+CMGD=");
  mySerial.println(SmsStorePos);
}  

以下は、シリアルモニターに表示されているものです。

http://imgur.com/i5jEPds

4

9 に答える 9

2

前に述べたように、問題は「ボー」設定にあります(これはシリアル通信に関係しています-別の答えです)。これは (通常) スケッチの上部にあり、次のように表示されます。

Serial.begin(9600);

またはこれ:

Serial.begin(115200);

Arduino Uno の場合、「ボー」を 9600 以外に設定すると、文字化けが発生します。そのため、スケッチの最初に次の行があることを確認してください。

Serial.begin(9600);

さらにサポートが必要な場合は、お気軽にお問い合わせください。

于 2016-08-22T02:19:59.980 に答える
1

Arduinoを使用してATtiny84をプログラムしていましたが、Arduinoをリセットするまで、出力が文字化けしたり、最初の行の後にシリアルが停止したりしていました。

問題は、Arduino に 'Arduino as ISP' スケッチがロードされていて、ATtiny84 からのシリアル データに干渉していたことです。

空のスケッチをArduinoにアップロードすると修正されました。

于 2016-10-25T21:42:35.780 に答える
0

「ボー」設定を 9600 に変更する必要がありました。

Serial.begin(9600);
于 2015-08-01T12:36:23.153 に答える
0

ソフトウェアシリアルではなく、シリアルライブラリを使用する必要があります。Serial は Arduino IDE の公式のものであり、デフォルトですべてのプログラムに含まれています。モニターとプログラムのボーレートが同じであることを確認してください。これは文字化けの一般的なエラーです。推奨されるボーレートは 9600 で、Serial.begin(9600)、Serial.read()、Serial.print("")、Serial.println("") などのコマンドを使用できます。

于 2020-02-21T20:03:43.983 に答える
0

Raspberry Pi 3b で Arduino からシリアル メッセージを受信しようとすると、同じ問題が発生しました。

あるケースでは、古いバージョンの Arduino IDE を使用していたことが判明しました。Arduino の Web サイトから最新の ARM 32 ビット バージョンをダウンロードしたところ、問題は解決しました。

別のケースでは、Pi で接続されている USB ポートを切り替えると解決したことが判明しました。

于 2021-09-14T23:11:43.793 に答える