卡在打开串口

时间:2014-11-12 17:11:42

标签: c freeze xbee pc

我有这个代码用于pc-arduino和xbee之间的通信。 问题在于,当我执行此程序时,程序似乎被冻结并且不会从arduino中收到任何内容,我不知道为什么会发生这种情况,但有一次程序在“终端”中显示我的消息想表明通讯是否正在发生:'line:....' 但现在不沟通。 如果代码中有任何错误或为什么会发生这种情况,你能告诉我吗?

我真的需要帮助,这是大学的最终项目,需要在本周解决。

谢谢!

    #include <stdio.h>      // standard input / output functions
#include <stdlib.h>
#include <string.h>
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <termios.h>    // POSIX terminal control definitions
#include <math.h>
#define XBEE "/dev/ttyUSB0"
#define BAUDRATE B9600

double average(int data[],int elem);
double desviation(double m, int data[],int elem);
void  normalize(double data[],int elem);
void store(double data[],int ex);
int main(int argc,char** argv)
{   
    struct termios tio;
    struct termios stdio;
    struct termios old_stdio;
    struct termios options;
    int data[262142];
    int tty_fd = open(XBEE , O_RDWR | O_NOCTTY | O_NDELAY);

    again:
    cfsetospeed(&tio,BAUDRATE);    
    cfsetispeed(&tio,BAUDRATE);            // baudrate is declarated above
    tcsetattr(tty_fd,TCSANOW,&tio);

    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;

 //   write(tty_fd,&c,'1');
    char o='o';
    char a='a';
    FILE *f; 
    char line[100];
    f = fopen("/dev/ttyUSB0", "r+");
    while(line[0]!='o'){
        fgets(line,10,f);
        printf("line: %s\n",line);
    }
    while(line[0]=='o'){
        fputc(a,f);
        fgets(line,10,f);
        printf("line: %s\n",line);
    }
        int count=0;
        int pos=0;
        //----------------------
        while(1){
            printf("dentro: %c\n",line[0]);
            int num;
            char number[3];     
            if(line[0]!=' '){
                number[count]=line[0];
                if(count>3){
                    count=0;
                }
                else{
                count=count+1;
                }
            }
            if(line[0]==' '){
                count=0;
                sscanf(number,"%d",&num);
                printf("%d\n",num);
                data[pos]=num;
            }

            if(line[0]=='\n'){
                printf("\n\nAll the data have been received");
                double av=average(data,262142);
                double desv=desviation(av,data,262142);

                double data2[262142];
                int i;
                for (i = 0; i < num; i++)
                {
                    data2[i]=(data[i]-av)/desv;
                }
                normalize(data2,262142);
                char ex;
                int aux;
                printf("It was steps? y/n\n");
                scanf ("%c",&ex);
                if(ex=='y'){
                    aux=1;
                    }
                else{
                    aux=0;
                }
                store(data2,aux);
                fputc(o,f);
                line[0]='0';
                while(line[0]!='o'){
                    fgets(line,10,f);       
                }
                break;
            }

            write(tty_fd,&o,1);
            line[0]='0';

            while(line[0]!='o'){
                fgets(line,10,f);       
            }
            line[0]='x';
            while(line[0]=='x'){
                fgets(line,10,f);
            }

        }
    goto again;
    close(tty_fd);
    tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);
    return EXIT_SUCCESS;
}

postdata:我不能使用API​​模式,需要在c上执行此操作。

更新

我已经完成了你对我所说的一切。仍然没有工作,调试器卡在第一个fgets(第30行)。

#include <stdio.h>      // standard input / output functions
#include <stdlib.h>
#include <string.h>
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <termios.h>    // POSIX terminal control definitions
#include <math.h>
#define XBEE "/dev/ttyUSB0"
#define BAUDRATE B9600

double average(int data[],int elem);
double desviation(double m, int data[],int elem);
void  normalize(double data[],int elem);
void store(double data[],int ex);
int main(int argc,char** argv)
{
    int data[262142];
    char o='o';
    char a='a';
    char line[100];
    FILE *f;

    again:
    f = fopen("/dev/ttyUSB0", "r+");
    if (f == NULL) {
        printf("Error opening the port\n");
    }

    while(line[0]!='o'){
        fgets(line,10,f);
        printf("line: %s\n",line);
    }
    while(line[0]=='o'){
        fputc(a,f);
        fgets(line,10,f);
        printf("line: %s\n",line);
    }
        int count=0;
        int pos=0;
        //----------------------
        while(1){
            printf("dentro: %c\n",line[0]);
            int num;
            char number[3];
            if(line[0]!=' '){
                number[count]=line[0];
                if(count>3){
                    count=0;
                }
                else{
                count=count+1;
                }
            }
            if(line[0]==' '){
                count=0;
                sscanf(number,"%d",&num);
                printf("%d\n",num);
                data[pos]=num;
            }

            if(line[0]=='\n'){
                printf("\n\nAll the data have been received");
                double av=average(data,262142);
                double desv=desviation(av,data,262142);

                double data2[262142];
                int i;
                for (i = 0; i < num; i++)
                {
                    data2[i]=(data[i]-av)/desv;
                }
                normalize(data2,262142);
                char ex;
                int aux;
                printf("It was steps? y/n\n");
                scanf ("%c",&ex);
                if(ex=='y'){
                    aux=1;
                    }
                else{
                    aux=0;
                }
                store(data2,aux);
                fputc(o,f);
                line[0]='0';
                while(line[0]!='o'){
                    fgets(line,10,f);
                }
                break;
            }

            fputc(o,f);
            line[0]='0';

            while(line[0]!='o'){
                fgets(line,10,f);
            }
            line[0]='x';
            while(line[0]=='x'){
                fgets(line,10,f);
            }

        }
    goto again;
    fclose(f);
    return EXIT_SUCCESS;
}


double average(int data[],int elem){
    int i=0;
    int sum=0;
    for (i = 0; i < elem; i++){
       sum+=data[i];
    }
    return sum/elem;
}

double desviation(double m, int data[],int elem){
    int i=0;
    int j=0;
    double dif=0.0;
    for (i = 0; i < elem; i++){
            dif=pow(data[i]-m,2);

    }

    return sqrt(dif/(18000-1));
}

void normalize(double data[],int elem){

    int i,j;
    double biggest;
    double lowest;
    double aux;
    biggest = 0.0;
    lowest = 9999999999999999.999;
    for (i = 0; i < elem; i++){
            if(data[i]>biggest){
                biggest=data[i];
            }
            if(data[i]<lowest){
                lowest=data[i];
            }
    }
    if (abs(biggest)>abs(lowest)){
        aux=abs(biggest);}
    else{
        aux=abs(lowest);}
    for (i = 0; i < elem; i++){
            data[i]=data[i]/aux;
        }
}

void store(double data[],int ex){
    FILE *out;
    out=fopen("training.data","r");
    char line[262142];
    fgets(line,262142,out);
    char *values;
    values=strtok(line," ");
    int val;
    sscanf(&values[0],"%d",&val);
    FILE *out2;
    out2=fopen("trainingAux.data","w");
    fprintf(out2,"%d 262142 1\n",val+1);
    while(fgets(line,262142,out)!=NULL){
        fprintf(out2,"%s",line);
    }
    int i = 0;
    for(i;i<262141;i++){
        fprintf(out2,"%lf ", data[i]);
    }
    fprintf(out2,"%lf\n", data[i+1]);
    fprintf(out2,"%d\n",ex);
    fclose(out2);
    fclose(out);
    out=fopen("training.data","w");
    out2=fopen("trainingAux.data","r");
    while(fgets(line,262142,out2)!=NULL){
        fprintf(out,"%s",line);
    }
    fclose(out);
    fclose(out2);
}

1 个答案:

答案 0 :(得分:0)

有一些方法可以弄清楚通过串行通信时发生了什么。   首先,为什么在使用'open'到同一路径(/ dev / ttyUSB0)之后才使用'fopen'?   当您使用句柄来建立像'tty_fd'这样的通信时,您必须确保此句柄是成功创建的,您可以尝试:

if ( (tty_fd = open(XBEE , O_RDWR | O_NOCTTY | O_NDELAY)) == -1 ){ IfTraceAvaliable("Error while opening"); return -1; }

此外,您可能正在尝试在termios结构上设置一些属性,例如:

options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8;

但在此之后,您并未将其应用于您的连接,例如: ioctl(tty_fd, TCSETA, &options);

记住你也可以测试ioctl == 0,如果它不相等,那就意味着你无法正确设置它。

[编辑] 由于你使用的是open(),你可以使用read()函数从arduino中检索答案,而不是使用fgets或fread,这可以作为替代。