Bash 带有 LCD 错误消息错误的 ping 脚本

Bash ping script with LCD error message errors

我在 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: In function 'main': lcdSignal.c:190:10: error: expected '(' before 'true'

lcdSignal.c:190:10: error: 'true' undeclared (first use in this function)

lcdSignal.c:190:10: note: each undeclared identifier is reported only once for each function it appears in

lcdSignal.c:191:3: error: 'ping' undeclared (first use in this function)

lcdSignal.c:191:12: error: too many decimal points in number

lcdSignal.c:191:9: error: 'c1' undeclared (first use in this function)

lcdSignal.c:191:12: error: expected ';' before numeric constant

lcdSignal.c:192:3: error: expected 'while' before 'lcd_writechars'

lcdSignal.c:193:7: error: expected '(' before '[' token

lcdSignal.c:193:12: error: '$' undeclared (first use in this function)

lcdSignal.c:193:19: error: unknown type name 'then'

lcdSignal.c:194:18: error: expected declaration specifiers or '...' before string constant

lcdSignal.c:195:3: error: 'fi' undeclared (first use in this function)

lcdSignal.c:196:9: error: expected ';' before numeric constant

如果有人能帮助我,我将不胜感激!!!

#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;
}

您程序中的以下代码行根本不是 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() 函数可能会有帮助。)