IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Programme C, µCLinux, RS232


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    96
    Détails du profil
    Informations personnelles :
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 96
    Par défaut Programme C, µCLinux, RS232
    Bonjour à tous,

    Je poste sur le forum parce que je suis désespéré.
    Voilà 3 jours que je cherche une solution à mon problème mais je ne l'ai toujours pas trouvé, peut être pourrez vous m'aider.

    Je suis en stage et je suis en train de développer un serverSMS.

    Pour cela, je suis en train de faire un programme en C tout simple qui récupère 2 arguments, le numéro et le message texte.

    Dans le protocole SMS, pour envoyer un SMS par un module GSM il suffit de faire les commandes AT+CMGS ="mon num" [ENTER]
    un prompt apparait ">" taper son SMS et le finir par [CTRL Z]

    Je fais ça sur un µCLinux sur un BlackFin.

    Je mets mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
     
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/select.h>
    #include <sys/ioctl.h>
    #include <bfin_pflags.h>
    #include <edevice.h>
     
    char * pProgName; /* Variable used to store program name availble in argv[0] in main function. */
    char * numDes;
    char * mySMS;
    char * myCommandCMGS;
     
    /********************************************************************
     * Display on stdout how to call the program.
     *
     * ******************************************************************/
    static void usage ()
    {
    	printf("Usage: %s <uartSpeed>\n", pProgName);
    }
     
    /********************************************************************
     * Envoi d'un SMS
     * ******************************************************************/
     
    static int sendSMS(int tty)
    {
    	int ttyCmd_result;
    	char buf[128];
    	char tmpstr[32];
    	char * cmgs = NULL;
     
    	/* Send "AT\r" command to GPRS module ("OK" answer expected). */
    	if((edCommand_tty(tty, "AT\r" , "OK", buf, sizeof buf,5)) != 0)
    	{	/* No OK answer received. */
    		sleep(1);	/* Wait for 1 sec. */
    		/* Send +++ in case the GPRS module still be in data mode. */
    		edCommand_tty(tty, "+++", "", buf, sizeof buf,5);
    		sleep(10);	/* Wait 10 seconds. */
    		/* Send a second "\r" command to GPRS module (no answer expected). */
    		edCommand_tty(tty, "\r","", buf, sizeof buf,5);
    		/* Send a second "AT\r" command to GPRS module ("OK" answer expected). */
    		if((ttyCmd_result = (edCommand_tty(tty, "AT\r" , "OK", buf, sizeof buf,5))) != 0)
    		{	/* No OK answer received. */
    			return ttyCmd_result;	/* return edCommand_tty() result. */
    		}
    	}
     
    	if((edCommand_tty (tty, "AT+CMGF=1\n\r", "OK", buf, sizeof(buf), 5))!=0)
    	{
    		edLog("Passage en mode Texte");
    	}
    	else edLog("Deja en mode texte");
     
     
    	/* Construction de la commande AT */
    	strcat(myCommandCMGS, "AT+CMGS=\"");
    	strcat(myCommandCMGS, numDes);
    	strcat(myCommandCMGS, "\"");
    	strcat(myCommandCMGS, "\n\r");
     
    	if(edCommand_tty(tty, myCommandCMGS , ">", buf, sizeof buf,5)!=0);
    	{
    			edLog("SMSSend buf 0 : %s", buf);
     
    		edCommand_tty(tty, "titi\x1A" , "+CMGS: ", buf, sizeof buf,10);
     
    			edLog("SMSSend buf 1 : %s", buf);
     
    		cmgs = strstr(buf, "+CMGS: ");
     
    		if(cmgs != NULL)
    		{	/* "+WMBS: " is found in AT+WMBS? cmd answer. */
    			if(strlen(cmgs) >= 8)	/* 10 = length of "+WMBS: X,0" */
    			{	/* GSM band value is present in the answer. */
    				cmgs = cmgs + 7;
    				edLog("SMSSend : AT+CMGS?=%s", cmgs);
    			}
    			else
    			{	/* No correct value can be retrieved from answer. */
    				edLog("SMSSend : AT+CMGS?=%s", " 1 NO Correct Value");
    				return 2;
    			}
    		}
    		else
    		{	/* No correct value can be retrieved from answer. */
    			edLog("SMSSend : AT+CMGS?=%s", " 2 NO Correct Value");
    			edLog("SMSSend buf 2 : %s", buf);
    			return 2;
    		}
    	}
     
    	return 0;	/* return OK. */
    }
     
     
    int main (int argc, char **argv)
    {
    	int tty;
    	int retValue = 0;
     
     
    	pProgName = argv[0];
     
    	/* Open edevice Log with module_ed_Server name. */
    	edOpenLog(argv[0], LOG_PID, LOG_LOCAL1);
     
    	mySMS = malloc (sizeof(char) * 160);
    	myCommandCMGS = malloc( sizeof(char) * 23);
    	numDes = malloc( sizeof(char) * 10);
     
    	if (argc != 4)
    	{
    		edLog("Main%c Bad arguments on command line", ':');
    		usage();
    		edCloseLog();	/* Close edevice Log. */
    		return 1;
    	}
    	else
    	{
    		if((tty = edOpenChangeAttr(atoi(argv[1]))) < 0)
    		{	/* Impossible to open /dev/ttyBF1. */
    			if(tty == -1)
    			{	/* No baud field of baudtable[] is equal to the parameter given to the program. */
    				usage(argv[0]);	/* Display on stdout how to use this program. */
    			}
    			edCloseLog();	/* Close edevice Log. */
    			return 1;	/* Return Error. */
    		}
    		strcpy(numDes,argv[2]);
    		strcpy(mySMS,argv[3]);	
    		strcat(mySMS,"\032");
    	}
     
    	retValue = sendSMS(tty);
    	/* Close tty and reinitialize Termio with value saved in edOpenChangeAttr() call. */
    	edCloseResetAttr(tty);
    	edCloseLog();	/* Close edevice Log. */
    	return(retValue);
    }
    edCommand_tty communique avec le port RS232, avec le num du port, la commande, la réponse de la commande, un buffer qui récupère la réponse, la taille du buf, et un timeout d'attente.

    edLog est utilisé pour récupéré dans un fichier texte les logs...

    Donc quand je lance mon application je reçois ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: send AT^M
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: wait for answer OK to AT^M
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: got AT^M^M OK^M 
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: send AT+CMGF=1 ^M
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: wait for answer OK to AT+CMGF=1 ^M
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: got AT+CMGF=1 ^M^M OK^M 
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: Deja en mode texte
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: send AT+CMGS="lenuméro" ^M
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: wait for answer > to AT+CMGS="lenuméro" ^M
    Jan  1 04:56:42 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: got AT+CMGS="lenuméro" ^M^M > ^M 
    Jan  1 04:56:43 routeD230 local1.debug ./bin/serverSMS[230]: SMSSend buf 0 : AT+CMGS="lenuméro" ^M^M > ^M 
    Jan  1 04:56:43 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: send titi^Z
    Jan  1 04:56:43 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: wait for answer +CMGS:  to titi^Z
    Jan  1 04:56:43 routeD230 local1.debug ./bin/serverSMS[230]: edCommand_tty: got ERROR^M 
    Jan  1 04:56:43 routeD230 local1.debug ./bin/serverSMS[230]: SMSSend buf 1 : ERROR^M 
    Jan  1 04:56:43 routeD230 local1.debug ./bin/serverSMS[230]: SMSSend : AT+CMGS?= 2 NO Correct Value
    Jan  1 04:56:43 routeD230 local1.debug ./bin/serverSMS[230]: SMSSend buf 2 : ERROR^M
    j'ai mis mon SMS en dur pour les tests.

    Sauriez vous comment envoyer le contrôle Z autrement ?

    J'ai même modifié la communication avec le RS232 : quand on tape stty -a < /dev/ttyBF1

    il mets que le SUSP = <undef> donc que le CTRL Z n'est plus pris comme une commande système qui pourrait bloquer mon application.

    Je perds énormément de temps la dessus. Si quelqu'un peut m'aider, je l'en remercierai de 100 000 bisous.

    Sylvanocry

  2. #2
    Membre Expert Avatar de jabbounet
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juin 2009
    Messages
    1 909
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 909
    Par défaut
    Tu peux essayer des sorties formatée a base de sprintf.


    ensuite essaie peu etre de séparer l'envoi du ^Z par rapport au message.

    car j'ai l'impression que tu fais

    alors qu'il faut peu etre faire

  3. #3
    Membre confirmé
    Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    96
    Détails du profil
    Informations personnelles :
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 96
    Par défaut
    Bonsoir jabbounet

    J'ai fait ces deux tests différents, cela ne change rien.

    Merci quand même !

  4. #4
    Membre Expert Avatar de jabbounet
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Juin 2009
    Messages
    1 909
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49

    Informations professionnelles :
    Activité : Consultant informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 1 909
    Par défaut
    Citation Envoyé par Sylvanocry Voir le message
    Bonsoir jabbounet

    J'ai fait ces deux tests différents, cela ne change rien.

    Merci quand même !
    quelques liens utiles
    http://www.developershome.com/sms/ho...dSMSFromPC.asp
    http://www.developershome.com/sms/ho...erTerminal.asp
    http://www.control.com.sg/at_commands_sms.aspx
    http://bytes.com/topic/c/answers/529...em-interfacing


    après je ne sais pas trop, tu n'as pas un doc fournit avec ta lib?

  5. #5
    Nouveau candidat au Club
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2010
    Messages : 2
    Par défaut
    et en remplacant le \032 par un \x1a parfois ca passe mieux ?

    Sinon essaie d'autres indicateurs de fin comme EOT ou ETX
    http://www.agroparistech.fr/mmip/mc/intdocs/ascii.html

  6. #6
    Invité
    Invité(e)
    Par défaut
    Ctrl-z envoie un signal d'interruption, en utilisant la fonction signal tu pourras déclencher une série d'actions quand ton programme recevra ce signal.

  7. #7
    Membre confirmé
    Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    96
    Détails du profil
    Informations personnelles :
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 96
    Par défaut
    Bonjour à tous,

    Je me suis déjà inspiré de ces sites pour mon apprentissage au protocole SMS, et la solution donnée dans ton dernier site web ne fonctionne pas. Néanmoins je te remercie beaucoup pour ton aide.

    Citation Envoyé par erebeh Voir le message
    et en remplacant le \032 par un \x1a parfois ca passe mieux ?

    Sinon essaie d'autres indicateurs de fin comme EOT ou ETX
    http://www.agroparistech.fr/mmip/mc/intdocs/ascii.html
    Erebeh, j'ai déjà fait les tests avec \x1A, \032, en décimale, et même essayé en mode texte en récupérant le caractère spécial NUB grâce à note pad ++.

    Cela n'a pas changé mon problème, et c'est bien dommage mais je te remercie.

    Citation Envoyé par ironzorg Voir le message
    Pour le C-Z, man signal/sigaction (ca depend si ton prog tournera sur solaris ou pas, si oui utilise sigaction ).

    Sinon, avant de poster sur le forum et de faire du debug, teste le retour des fonctions qui retournent des pointeurs (malloc...). Le probleme ne vient pas tout le temps de la, mais ca reste une habitude a prendre.
    Citation Envoyé par feydaykyn Voir le message
    Ctrl-z envoie un signal d'interruption, en utilisant la fonction signal tu pourras déclencher une série d'actions quand ton programme recevra ce signal.
    feydaykyn, ironzorg, je coupe le signal SUSP grâce à un drivers fait par ma boite je me suis inspiré de ce site : http://guidespratiques.traduc.org/vf...ing-HOWTO.html

    Néanmoins je n'ai pas encore tester en utilisant la fonction signal, je vais le faire à la fin de la rédaction de cette réponse, ainsi que les tests des retours de malloc (je n'avais jamais entendu dire qu'elle pouvait poser problème)

    Merci du temps que vous me consacrez, ne vous inquiétez pas, on va trouver

    Sylvanocry

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment programmer une communication RS232 enSsiemens
    Par Rodroy dans le forum Automation
    Réponses: 1
    Dernier message: 28/03/2014, 17h19
  2. Réponses: 1
    Dernier message: 26/04/2007, 15h23
  3. programmer une RS232 en Matlab et en Visual C++
    Par tabbas dans le forum Windows
    Réponses: 2
    Dernier message: 26/04/2007, 09h37
  4. Programmer un automate par RS232
    Par wael khalil dans le forum Langage
    Réponses: 6
    Dernier message: 25/08/2005, 16h02
  5. [Programmation] port série / adaptateur RS232
    Par benhua dans le forum Windows
    Réponses: 8
    Dernier message: 15/09/2004, 20h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo