2014-02-05 29 views
-5

我需要做一个.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 } 
+0

你在函数末尾缺少'}'。请不要发布行号。 –

+0

您需要正确缩进。看起来你在函数外有一个'if'。 – tadman

+6

从第69行开始的所有代码都不在函数内部。 (他不会错过任何括号。)当你盲目地从互联网上获取代码,把它扔到你的项目中,并且不注意发生了什么的时候,会发生什么。 –

回答

2

你不能在函数外写代码。也许某些函数定义是从69行开始意外删除的。

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()...

0

它看起来像你有一些格式问题。您的卷曲({})括号不匹配,并且您有几个超出任何函数的命令。

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

相关问题