我在Debian工作,我正在尝试编写一个脚本,每隔10秒就会ping一个主机(192.168.10.30)。
每当ping不成功,我想在我的LCD屏幕上显示消息“VPN Lost”。一旦ping再次成功,“VPN Lost”消息将从我的LCD屏幕消失,屏幕将显示“STS300”。
当我尝试编译它时,下面的代码一直给我一个错误,我不明白为什么。 (代码编译得很好,但是一旦我在ping代码中添加它就不会编译)。给我带来麻烦的代码在lcd_command(LINE_3);
和lcd_command(LINE_4);
我对此很新...
我得到的错误是......
lcdSignal.c:在函数'main'中:lcdSignal.c:190:10:错误:预期 '('在'真'之前'
lcdSignal.c:190:10:错误:'true'unclaclared(首次使用此处 功能)
lcdSignal.c:190:10:注意:仅报告每个未声明的标识符 每个功能一次出现在
中lcdSignal.c:191:3:错误:'ping'unclaclared(首先在此使用 功能)
lcdSignal.c:191:12:错误:数字中的小数点太多
lcdSignal.c:191:9:错误:'c1'未声明(首次使用此功能)
lcdSignal.c:191:12:错误:预期';'在数字常量之前
lcdSignal.c:192:3:错误:在'lcd_writechars'之前预期'while'
lcdSignal.c:193:7:错误:预期'(''''''令牌
lcdSignal.c:193:12:错误:'$'未声明(首次使用此功能)
lcdSignal.c:193:19:错误:未知类型名称'然后'
lcdSignal.c:194:18:错误:预期的声明说明符或'...' 在字符串常量之前
lcdSignal.c:195:3:错误:'fi'未声明(首次使用此功能)
lcdSignal.c:196:9:错误:预期';'在数字常量之前
如果有人可以帮助我,我会非常感激!!! {/ p>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include "evgpio.h"
#include "STSConfig.h" // This file is where the strings are modified
void lcd_init(void);
void lcd_wait(void);
void lcd_command(uint16_t);
void lcd_writechars(unsigned char *dat);
// These are microsecond delays
#define SETUP 1
#define PULSE 2
#define HOLD 1
#define LINE_1 0x80 // beginning position of LCD line 1
#define LINE_2 0xC0 // beginning position of LCD line 2
#define LINE_3 0x94 // beginning position of LCD line 3
#define LINE_4 0xD4 // beginning position of LCD line 4
#define LCD_D0 74
#define LCD_D1 75
#define LCD_D2 72
#define LCD_D3 73
#define LCD_D4 70
#define LCD_D5 71
#define LCD_D6 68
#define LCD_D7 69
#define LCD_EN 64
#define LCD_WR 65
#define LCD_RS 66
#define LCD_BIAS 67
void lcd_write(uint8_t out)
{
evsetdata(LCD_D0, out & (1 << 0));
evsetdata(LCD_D1, out & (1 << 1));
evsetdata(LCD_D2, out & (1 << 2));
evsetdata(LCD_D3, out & (1 << 3));
evsetdata(LCD_D4, out & (1 << 4));
evsetdata(LCD_D5, out & (1 << 5));
evsetdata(LCD_D6, out & (1 << 6));
evsetdata(LCD_D7, out & (1 << 7));
}
void lcd_ddr(uint8_t out)
{
evsetddr(LCD_D0, out & (1 << 0));
evsetddr(LCD_D1, out & (1 << 1));
evsetddr(LCD_D2, out & (1 << 2));
evsetddr(LCD_D3, out & (1 << 3));
evsetddr(LCD_D4, out & (1 << 4));
evsetddr(LCD_D5, out & (1 << 5));
evsetddr(LCD_D6, out & (1 << 6));
evsetddr(LCD_D7, out & (1 << 7));
}
uint8_t lcd_read()
{
uint8_t out = 0;
out |= (evgetin(LCD_D0) << 0);
out |= (evgetin(LCD_D1) << 1);
out |= (evgetin(LCD_D2) << 2);
out |= (evgetin(LCD_D3) << 3);
out |= (evgetin(LCD_D4) << 4);
out |= (evgetin(LCD_D5) << 5);
out |= (evgetin(LCD_D6) << 6);
out |= (evgetin(LCD_D7) << 7);
return out;
}
void lcd_enpulse()
{
usleep(SETUP);
evsetdata(LCD_EN, 1);
usleep(PULSE);
evsetdata(LCD_EN, 0);
usleep(HOLD);
}
void lcd_init(void)
{
evgpioinit();
// Data lines to inputs, control lines to outputs
lcd_ddr(0x0);
evsetddr(LCD_EN, 1);
evsetddr(LCD_RS, 1);
evsetddr(LCD_WR, 1);
// Set LCD_EN and LCD_RS low
evsetdata(LCD_EN, 0);
evsetdata(LCD_RS, 0);
// Set LCD_WR high
evsetdata(LCD_WR, 1);
usleep(15000);
lcd_command(0x38); // two rows, 5x7, 8 bit
usleep(4100);
lcd_command(0x38); // two rows, 5x7, 8 bit
usleep(100);
lcd_command(0x38); // two rows, 5x7, 8 bit
lcd_command(0x6); // cursor increment mode
lcd_wait();
//lcd_command(0x1); // clear display
// lcd_wait();
// lcd_command(0xc); // display on, blink off, cursor off
// lcd_wait();
lcd_command(0x2); // return home
evsetddr(LCD_BIAS, 1);
evsetdata(LCD_BIAS, 0);
}
void lcd_wait(void)
{
uint8_t in;
int i, dat, tries = 0;
lcd_ddr(0x0);
do {
// step 1, apply only RS & WR
evsetdata(LCD_RS, 0);
evsetdata(LCD_WR, 1); // low for write
lcd_enpulse();
usleep(1);
} while (in & 0x80 && tries++ < 5);
}
void lcd_command(uint16_t cmd)
{
lcd_ddr(0xff);
lcd_write(cmd);
evsetdata(LCD_WR, 0);
evsetdata(LCD_RS, 0);
lcd_enpulse();
}
void lcd_writechars(unsigned char *dat)
{
int i;
do {
lcd_wait();
lcd_ddr(0xff);
evsetdata(LCD_RS, 1);
evsetdata(LCD_WR, 0); // active low
lcd_write(*dat++);
lcd_enpulse();
} while (*dat);
}
int main(int argc, char *argv[])
{
char *temp = argv[1];
float sigStn = atof(temp);
float numBuf = (((sigStn-(-120))/(-70-(-120)))*100);
char buf[48];
if (numBuf >=9 && numBuf<=99) {
snprintf(buf, sizeof(buf), " Signal= %0.0f",numBuf);
strcat(buf,"% ");
} else {
snprintf(buf, sizeof(buf), " Signal= %0.0f",numBuf);
strcat(buf,"% ");
}
lcd_init();
lcd_command(LINE_1);
lcd_writechars(STS);
lcd_command(LINE_2);
if (numBuf > 100)
lcd_writechars(" Signal= 100% ");
else if(numBuf<0){
lcd_writechars(" Reconnecting ");
// lcd_writechars(" Signal = 0% ");
} else
lcd_writechars(buf);
lcd_command(LINE_3);
{while true; do
ping -c1 192.168.10.30 2>&1 /dev/null; //VPN IP
lcd_writechars("STS300");}
{if [[ ! $? ]]; then
lcd_writechars("VPN Lost");
fi;
sleep 10;
}
lcd_command(LINE_4);
lcd_writechars(BBPN);
return;
while (!feof(stdin)) {
unsigned char buf[512];
int i = 0;
lcd_wait();
if (i) {
// XXX: this seek addr may be different for different
// LCD sizes! -JO
lcd_command(0xa8); // set DDRAM addr to second row
} else
lcd_command(0x2); // return home
i = i ^ 0x1;
if (fgets(buf, sizeof(buf), stdin) != NULL) {
unsigned int len;
buf[0x27] = 0;
len = strlen(buf);
if (buf[len - 1] == '\n') buf[len - 1] = 0;
lcd_writechars(buf);
}
}
return 0;
}
答案 0 :(得分:1)
程序中的以下代码行根本不是C.它们看起来像是shell脚本的一个片段:
lcd_command(LINE_3);
{while true; do
ping -c1 192.168.10.30 2>&1 /dev/null; //VPN IP
lcd_writechars("STS300");}
{if [[ ! $? ]]; then
lcd_writechars("VPN Lost");
fi;
sleep 10;
}
这不起作用。在C中重写此代码。system()
函数可能会有所帮助。)