我有错误,但我无法识别

时间:2014-02-05 20:19:59

标签: c++

我需要做.c与tty进行通信,我找到了这段代码,但是当我尝试使用arm-none-linux-gnueabi-c++进行编译时,我遇到了这样的错误:

example2.c:73: error: expected unqualified-id before 'if'
example2.c:78: error: expected unqualified-id before '{' token 

这是代码

  1 #include <errno.h>
  2 #include <termios.h>
  3 #include <unistd.h>
  4 #include <string.h>
  5 #include <stdio.h>
  6 #include <iostream>
  7 #include <fcntl.h>
  8 
  9 using namespace std;
 10 
 11 int set_interface_attribs (int fd, int speed, int parity)
 12 {
 13         struct termios tty;
 14         memset (&tty, 0, sizeof tty);
 15         if (tcgetattr (fd, &tty) != 0)
 16         {
 17                 cout<< "error" << endl;
 18                 return -1;
 19         }
 20 
 21         cfsetospeed (&tty, speed);
 22         cfsetispeed (&tty, speed);
 23 
 24         tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
 25         // disable IGNBRK for mismatched speed tests; otherwise receive break
 26         // as \000 chars
 27         tty.c_iflag &= ~IGNBRK;         // ignore break signal
 28         tty.c_lflag = 0;                // no signaling chars, no echo,
 29                                         // no canonical processing
 30         tty.c_oflag = 0;                // no remapping, no delays
 31         tty.c_cc[VMIN]  = 0;            // read doesn't block
 32         tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
 33 
 34         tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
 35 
 36         tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
 37                                         // enable reading
 38         tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
 39         tty.c_cflag |= parity;

 40         tty.c_cflag &= ~CSTOPB;
 41         tty.c_cflag &= ~CRTSCTS;
 42 
 43         if (tcsetattr (fd, TCSANOW, &tty) != 0)
 44         {
 45                 cout<< "error" << endl;
 46                 return -1;
 47         }
 48         return 0;
 49 }
 50 
 51 void
 52 set_blocking (int fd, int should_block)
 53 {
 54         struct termios tty;
 55         memset (&tty, 0, sizeof tty);
 56         if (tcgetattr (fd, &tty) != 0)
 57         {
 58                 cout<< "error" << endl;
 59                 return;
 60         }
 61 
 62         tty.c_cc[VMIN]  = should_block ? 1 : 0;
 63         tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
 64 
 65         if (tcsetattr (fd, TCSANOW, &tty) != 0)
 66                 cout<< "error" << endl;
 67 }
 68 
 69 const char *portname = "/dev/ttyUSB1";
 70 
 71 int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
 72 
 73 if (fd < 0)
 74 {
 75         cout<< "error" << endl;

 76         return;
 77 }
 78 {
 79 set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
 80 set_blocking (fd, 0);                // set no blocking
 81 
 82 write (fd, "hello!\n", 7);           // send 7 character greeting
 83 char buf [100];
 84 int n = read (fd, buf, sizeof buf);
 85 }

3 个答案:

答案 0 :(得分:2)

您不能在函数之外编写代码。也许从第69行开始意外删除了一些函数定义。

答案 1 :(得分:0)

const char *portname = "/dev/ttyUSB1";
 70 
 71 int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
 72 
 73 if (fd < 0)
 74 {
 75         cout<< "error" << endl;

 76         return;
 77 }
 78 {
 79 set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
 80 set_blocking (fd, 0);                // set no blocking
 81 
 82 write (fd, "hello!\n", 7);           // send 7 character greeting
 83 char buf [100];
 84 int n = read (fd, buf, sizeof buf);
 85 }

这部分代码超出了任何不允许的功能。也许你想在main()...

中写这个

答案 2 :(得分:0)

看起来你有一些格式问题。您的卷曲({})括号不匹配,并且您有多个命令在任何函数之外。

您可能在某处删除了一行代码。我建议将第68行之后的所有代码放入函数中。