Minggu, 25 Mei 2014

acr120.h //acr2

#ifndef _ACR120_H
#define _ACR120_H

#include <termios.h>

#define ACR120_SUCCESS  0
#define ACR120_ERROR   -1

#define ACR120_REGISTER_STATIONID  0x4
#define ACR120_REGISTER_PROTOCOL   0x5
#define ACR120_REGISTER_BAUDRATE   0x6
#define ACR120_REGISTER_LED        0x7
#define ACR120_REGISTER_BUZZER     0x8

typedef enum {
    MIFARE_KEY_AA =0,
    MIFARE_KEY_BB,
    MIFARE_KEY_FF
} mifare_key;

struct acr120_conn {
    int fd;
    int error;
    unsigned char proto_mode;
};

extern int acr120_errno;

const char* acr120_strerror(void);



int acr120_open(const char *dev, unsigned char sid, speed_t speed);

int acr120_close(int fd);

int acr120_reset(int fd, unsigned char sid, int reply, int timeout);

int acr120_get_id(int fd, unsigned char *id, int timeout);

int acr120_select(int fd, unsigned char sid, unsigned int *uid, int timeout);

int acr120_login(int fd, unsigned char sid, unsigned char sector,
                mifare_key type, unsigned char *key, int timeout);
               
int acr120_write_block(int fd, unsigned char sid, unsigned char block,
                unsigned char *data, int timeout);
               
int acr120_write_value(int fd, unsigned char sid, unsigned char block,
                unsigned int value, int timeout);

int acr120_write_register(int fd, unsigned char sid, unsigned char reg,
                unsigned char value, int timeout);

int acr120_read_block(int fd, unsigned char sid, unsigned char block,
                unsigned char *data, int timeout);
               
int acr120_read_value(int fd, unsigned char sid, unsigned char block,
                unsigned int *value, int timeout);

int acr120_read_register(int fd, unsigned char sid, unsigned char reg,
                unsigned char *value, int timeout);

int acr120_copy_block(int fd, unsigned char sid, unsigned char source,
                unsigned char dest, int timeout);
               
int acr120_inc_value(int fd, unsigned char sid, unsigned char block,
                unsigned int inc, int timeout);
               
int acr120_dec_value(int fd, unsigned char sid, unsigned char block,
                unsigned int dec, int timeout);
               
int acr120_power_on(int fd, unsigned char sid, int timeout);

int acr120_power_off(int fd, unsigned char sid, int timeout);

#endif

acr120.c //acr

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "acr120.h"

#define STX 0x2
#define ETX 0x3

unsigned char *nib;

int acr120_errno;
static int acr120_proto_mode;
static struct termios old;

static void dec2nibble(unsigned int, unsigned char*);
static void nibble2dec(unsigned int*,unsigned char*);
static int check_bin_error(const unsigned char*, size_t);
static int check_ascii_error(const unsigned char*, size_t);
static int acr120_timeout(int, int);

static const char const *acr120_error[] = {
    "success", // 1
    "open device failure", // 2
    "close device failure",// 3
    "device setting failure",// 4
    "write device failure",// 5
    "read device failure",//6
    "select device failure",//7
    "operation timeout",//8
    "no TAG",//9
    "no value block",//10      
    "login fail, key wrong",//11
    "invalid key format (stored key)",//12
    "read failure",//13
    "unable to read after write",//14
    "read after write error",//15
    "write failure",//16
    "unable to read after increment",//17
    "increment failure",//18
    "unable to read after decrement",//19
    "decrement failure",//20
    "empty value decrement",//21
    "unable to read after copy",//22
    "copy failure",//23
    "data error"//24
};

static void dec2nibble(unsigned int value, unsigned char *nibble)
{
    nibble[3] = value & 0xff;
    nibble[2] = value >> 8 & 0xff;
    nibble[1] = value >> 16 & 0xff;
    nibble[0] = value >> 24 & 0xff;
}


static void nibble2dec(unsigned int *val,unsigned char *nibble)
{


int valoo = (nibble[3] & 0xff) | ((nibble[2] & 0xff) << 8) | ((nibble[1] & 0xff) << 16) | ((nibble[0] & 0xff) << 24);

*val = (nibble[0] & 0xff) | ((nibble[1] & 0xff) << 8) | ((nibble[2] & 0xff) << 16) | ((nibble[3] & 0xff) << 24);


printf("id *= %d\n",*val);
printf("id = %d\n",valoo);


}

static int check_bin_error(const unsigned char *ans, size_t len)
{
    unsigned char check = 0;
    int i, nc = len - 2 * sizeof(unsigned char );
    size_t bytes = 0;

    for (i = 1; i < nc; i++)
        check ^= ans[i];
    return (check == ans[nc]) ? 0 : 1;
}

static int check_ascii_error(const unsigned char *ans, size_t len)
{
    return (ans[len - 2] == 0xd && ans[len -1] == 0xa) ? 0 : 1;
}

const char* acr120_strerror(void)
{
    return acr120_error[acr120_errno - 1];
}

inline int acr120_open(const char *dev, unsigned char sid, speed_t speed)
{
    int fd, ret;
    unsigned char cmd[8], ans[6], val, check = 0;
    size_t bytes = 0;
    struct termios opt;

   // fd = open(dev, O_RDWR | O_NOCTTY, 0644);
   fd = open(dev, O_RDWR | O_NOCTTY, 0110);
    if (fd == -1) {
        acr120_errno = 2;
        return ACR120_ERROR;
    }

    ret = tcgetattr(fd, &opt);
    if (ret == -1) {
        close(fd);
        acr120_errno = 4;
        return ACR120_ERROR;
    }

    /* save previous setting to restore on close */
    memcpy(&old, &opt, sizeof(struct termios));

    cfsetispeed(&opt, speed);
    cfsetospeed(&opt, speed);
    /* set to canonical mode */
    opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    /* disable flow control both input and output */
    opt.c_iflag &= ~(IXON | IXOFF);
    /* disable CR-NL or NL-CR translation both input and output */
    opt.c_iflag &= ~(INLCR | ICRNL);
    opt.c_oflag &= ~(ONLCR | OCRNL);
    ret = tcsetattr(fd, TCSANOW, &opt);
    if (ret == -1) {
        close(fd);
        acr120_errno = 4;
        return ACR120_ERROR;
    }

    /* probe in ASCII mode */
    snprintf((char *) cmd, 5 * sizeof(unsigned char), "re%.2x",
             ACR120_REGISTER_PROTOCOL);
    ret = write(fd, cmd, 4 * sizeof(unsigned char));
    if (ret <= 0) {
        acr120_close(fd);
        acr120_errno = 5;
        return ACR120_ERROR;
    }

    ret = acr120_timeout(fd, 100);
    if (ret == ACR120_ERROR) {
        if (acr120_errno != 8) {
            close(fd);
            return ret;unsigned char cmd[8], ans[6], val, check = 0;
    size_t bytes = 0;
        }
        goto bin;
    } else {
          while (bytes < 4 * sizeof(unsigned char)) {
              ret = read(fd, ans + bytes, 4 * sizeof(unsigned char) - bytes);
              if (ret <= 0) {
                  close(fd);
                  acr120_errno = 6;
                  return ACR120_ERROR;
              }
              bytes += ret;
          }
          if (check_ascii_error(ans, 4)) {
              acr120_errno = 24;
              goto bin;
          }
          ans[2] = 0;
          sscanf((char*) ans, "%x", (unsigned int*) &val);
          goto done;
    }

bin:
    /* read protocol register to obtain protocol mode */
    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 0x3;
    cmd[3] = 'r';
    cmd[4] = 'e';
    cmd[5] = ACR120_REGISTER_PROTOCOL;
    cmd[6] = cmd[1] ^ cmd[2] ^ cmd[3] ^ cmd[4] ^ cmd[5];
    cmd[7] = ETX;

    /* probe in binary mode */
    ret = write(fd, cmd, sizeof(cmd));
    if (ret <= 0) {
        acr120_close(fd);
        acr120_errno = 5;
        return ACR120_ERROR;
    }

    bytes = 0;
    while (bytes < sizeof(ans)) {
        ret = acr120_timeout(fd, 100);
        if (ret == ACR120_ERROR) {
            close(fd);
            return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);
//printf("%x %x %x %x %x %x %x %x %x %x \n",ans[0],ans[1],ans[2],ans[3],ans[4],ans[5],ans[6],ans[7],ans[8],ans[9]);

        if (ret <= 0) {
            acr120_close(fd);
            acr120_errno = 6;

            return ACR120_ERROR;
        }
        bytes += ret;
        /* eventually there are no error code satisfied for
           reading register value, only check here for
           compability */
        if (bytes > 4 && !check) {
            switch (ans[3]) {
                case 'N':
                    acr120_close(fd);
                    acr120_errno = 9;
                    return ACR120_ERROR;
                case 'I':
                    acr120_close(fd);
                    acr120_errno = 10;
                    return ACR120_ERROR;
                case 'F':
                    acr120_close(fd);
                    acr120_errno = 13;
                    return ACR120_ERROR;
            }
            check ^= 1;
        }
    }

    if (check_bin_error(ans, sizeof(ans))) {
        acr120_close(fd);
        acr120_errno = 24;
        return ACR120_ERROR;
    }
    val = ans[3];

done:
    /* set protocol mode */
    acr120_proto_mode = val >> 1 & 0x1;
    acr120_errno = 1;
    return fd;
}

int acr120_close(int fd)
{
    int ret;

    /* restore previous setting */
    tcsetattr(fd, TCSANOW, &old);
    ret = close(fd);
    if (ret == -1) {
        acr120_errno = 3;
        return ACR120_ERROR;
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}          

static int acr120_timeout(int fd, int timeout)
{
    int ret;
    fd_set rset;
    struct timeval val = { .tv_sec = 0, .tv_usec = 1000 * timeout };

    FD_ZERO(&rset);
    FD_SET(fd, &rset);
    ret = select(fd + 1, &rset, NULL, NULL, &val);
    if (ret == 0) {
        acr120_errno = 8;
        return ACR120_ERROR;
    }
    if (ret == -1) {
        acr120_errno = 7;
        return ACR120_ERROR;
    }
    if (FD_ISSET(fd, &rset)) {
        acr120_errno = 1;
        return ACR120_SUCCESS;
    }
    /* should never reach here */
    acr120_errno = 7;
    return ACR120_ERROR;
}

int acr120_reset(int fd, unsigned char sid, int reply, int timeout)
{
    int ret;
    unsigned char cmd[6], ans[12];
    size_t bytes = 0, nw, nr;

    if (acr120_proto_mode) {
        cmd[0] = STX;
        cmd[1] = sid;
        cmd[2] = 0x1;
        cmd[3] = 'x';
        cmd[4] = cmd[1] ^ cmd[2] ^ cmd[3];
        cmd[5] = ETX;
        nw = sizeof(cmd);
        /* no answer on binary mode */
        nr = 0;
    } else {
        cmd[0] = 'x';
        nw = sizeof(unsigned char);unsigned int *val,
        nr = reply ? sizeof(ans) : 0;
    }

    ret = write(fd, cmd, nw);
    if (ret <= 0) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }

    while (bytes < nr) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
       
        ret = read(fd, ans + bytes, nr - bytes);
        if (ret <= 0) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
    }

    if (!acr120_proto_mode && reply) {
        if (check_ascii_error(ans, 12)) {
            acr120_errno = 24;
            return ACR120_ERROR;
        }
    }
   
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_get_id(int fd, unsigned char *id, int timeout)
{
    int ret;
    unsigned char cmd[6], ans[6];
    size_t bytes = 0;

    cmd[0] = STX;
    cmd[1] = 0xff;
    cmd[2] = 0x1;
    cmd[3] = 'g';
    cmd[4] = cmd[1] ^ cmd[2] ^ cmd[3];
    cmd[5] = ETX;

    ret = write(fd, cmd, sizeof(cmd));
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }

    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);

        if (ret == -1) {
            acr120_errno = 6;

            return ACR120_ERROR;
        }
        bytes += ret;
    }

    if (id)
      *id = ans[3];

    acr120_errno = 1;

    return ACR120_SUCCESS;
}

int acr120_select(int fd, unsigned char sid, unsigned int *uid, int timeout)
{
    int ret;
    unsigned char cmd[6], ans[9], uids[4];
    size_t bytes = 0;

    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 0x1;
    cmd[3] = 's';
    cmd[4] = cmd[1] ^ cmd[2] ^ cmd[3];
    cmd[5] = ETX;

    ret = write(fd, cmd, sizeof(cmd));
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
        if (bytes > 3) {
            if (ans[3] == 'N') {
                acr120_errno = 9;
                return ACR120_ERROR;
            }
        }
    }


   if (uid){
memcpy(uids, ans + 3, sizeof(uids));
        nibble2dec(uid, uids);
}

    acr120_errno = 1;
    return ACR120_SUCCESS;
}

/*
 * key ff binary sequence: 02 01 04 6c 01 ff 0d 9a 03
 * key aa or bb binary sequence: 02 01 09 6c 01 aa a0 a1 a2 a3 a4 a5 ce 03
 */
int acr120_login(int fd, unsigned char sid, unsigned char sector,
                mifare_key type, unsigned char *key, int timeout)
{
    int ret, i;
    unsigned char cmd[14], ans[6];
    size_t bytes = 0, len;

printf("sector = %x\n",sector);
printf("key= %c\n",&key);
printf("keyType= %x\n",type);
    cmd[0] = STX;
    cmd[1] = sid;
    cmd[3] = 'l';
    cmd[4] = sector;

   if (type == MIFARE_KEY_FF) {
        cmd[2] = 0x04;
        cmd[5] = 0xff;
        cmd[6] = 0xd;
        for (i = 1, cmd[7] = 0; i < 7; i++)
            cmd[7] ^= cmd[i];
        len = 9;


    } else {
        cmd[2] = 9;
        cmd[5] = (type == MIFARE_KEY_AA) ? 0xaa : 0xbb;

        memcpy(cmd + 6, key, 6 * sizeof(unsigned char));
        for (i = 1, cmd[12] = 0; i < 12; i++)
            cmd[12] ^= cmd[i];
        len = 14;
    }

    cmd[len - 1] = ETX;

printf("%x %x %x %c %x %x %x %x %x %x %x %x %x %X\n",cmd[0],cmd[1],cmd[2],cmd[3],cmd[4],cmd[5],cmd[6],cmd[7],cmd[8],cmd[9],cmd[10],cmd[11],cmd[12],cmd[13]);
   
    ret = write(fd, cmd, len);

    if (ret == -1) {

        acr120_errno = 5;

        return ACR120_ERROR;
    }
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);
        if (ret == -1) {
            acr120_errno = 6;

            return ACR120_ERROR;
        }
printf("%c \n",ans[3]);

        bytes += ret;
        if (bytes > 3) {
            switch (ans[3]) {
                case 'L':
                    acr120_errno = 1;
printf("lOGIN = %s\n",acr120_strerror());
                    return ACR120_SUCCESS;
                case 'N':
                    acr120_errno = 9;


                    return ACR120_ERROR;
                case 'F':
                    acr120_errno = 11;

                    return ACR120_ERROR;
                case 'E':
                    acr120_errno = 12;

                    return ACR120_ERROR;
            }
        }
    }

    acr120_errno = 11;
    return ACR120_ERROR;
}

int acr120_write_block(int fd, unsigned char sid, unsigned char block,
                unsigned char *data, int timeout)
{
    int ret, i;
    unsigned char cmd[23], ans[21];
    size_t bytes = 0;

    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 18;
    cmd[3] = 'w';
    cmd[4] = block;
    memcpy(cmd + 5, data, 16);
    cmd[22] = ETX;

    for (i = 1, cmd[21] = 0; i < 21; i++)
        cmd[21] ^= cmd[i];

    ret = write(fd, cmd, sizeof(cmd));
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }

    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
        if (bytes > 3) {
            switch (ans[3]) {
                case 'X':
                    acr120_errno = 14;
                    return ACR120_ERROR;
                case 'U':
                    acr120_errno = 15;
                    return ACR120_ERROR;
                case 'N':
                    acr120_errno = 9;
                    return ACR120_ERROR;
                case 'F':
                case 'I':
                    acr120_errno = 16;
                    return ACR120_ERROR;
            }
        }
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_write_value(int fd, unsigned char sid, unsigned char block,
                unsigned int value, int timeout)
{
    int ret, i;
    unsigned char cmd[12], data[4], ans[9];
    size_t bytes = 0;

printf("value write = %d \n",value);  

    dec2nibble(value, data);
    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 0x7;
    cmd[3] = 'w';
    cmd[4] = 'v';
    cmd[5] = block;
    cmd[6] = data[0];
    cmd[7] = data[1];
    cmd[8] = data[2];
    cmd[9] = data[3];
    cmd[11] = ETX;

printf("data = %x\n",data);
printf("data write = %x %x %x %x \n",data[0],data[1],data[2],data[3]);
   
printf("cmd_write = %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",cmd[0],cmd[1],cmd[2],cmd[3],cmd[4],cmd[5],cmd[6],cmd[7],cmd[8],cmd[9],cmd[10],cmd[11],cmd[12],cmd[13]);
    for (i = 1, cmd[10] = 0; i < 10; i++)
        cmd[10] ^= cmd[i];

    ret = write(fd, cmd, sizeof(cmd));
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }
   
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
        if (bytes > 3) {
            switch (ans[3]) {
                case 'X':
                   acr120_errno = 14;
                   return ACR120_ERROR;
                case 'U':
                    acr120_errno = 15;
                    return ACR120_ERROR;
                case 'N':
                    acr120_errno = 9;
printf("ASIK\n");
                    return ACR120_ERROR;
                case 'F':
                case 'I':
                    acr120_errno = 16;
                    return ACR120_ERROR;
            }
        }
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_write_register(int fd, unsigned char sid, unsigned char reg,
                unsigned char value, int timeout)
{
    unsigned char cmd[9], ans[6], check = 0;
    size_t bytes = 0, nr, nw;
    int ret;

    if (acr120_proto_mode) {
        cmd[0] = STX;
        cmd[1] = sid;
        cmd[2] = 0x4;
        cmd[3] = 'w';
        cmd[4] = 'e';
        cmd[5] = reg;
        cmd[6] = value;
        cmd[7] = cmd[1] ^ cmd[2] ^ cmd[3] ^ cmd[4] ^ cmd[5] ^ cmd[6];
        cmd[8] = ETX;
        nw = sizeof(cmd);
        nr = sizeof(ans);
    } else {
        snprintf((char*) cmd, 7 * sizeof(unsigned char),
                 "we%.2x%.2x", reg, value);
        printf("cmd is: %s\n", (char*) cmd);
        nw = 6 * sizeof(unsigned char);
        nr = 4 * sizeof(unsigned char);
    }

    ret = write(fd, cmd, nw);

    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }

    while (bytes < nr) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }

        ret = read(fd, ans + bytes, nr - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
       
        bytes += ret;
        if (acr120_proto_mode && bytes > 4 * sizeof(unsigned char) && !check) {
            switch (ans[3]) {
                case 'X':
                    acr120_errno = 14;

                    return ACR120_ERROR;
                case 'U':
                    acr120_errno = 15;

                    return ACR120_ERROR;
                case 'N':
                    acr120_errno = 9;

                    return ACR120_ERROR;
                case 'I':
                    acr120_errno = 16;

                    return ACR120_ERROR;
            }
            check ^= 1;
        }
    }

    if (acr120_proto_mode) {
        if (check_bin_error(ans, sizeof(ans))) {
            acr120_errno = 24;
            return ACR120_ERROR;
        }
    } else {
        if (check_ascii_error(ans, 4)) {
            acr120_errno = 24;
            return ACR120_ERROR;
        }
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_read_block(int fd, unsigned char sid, unsigned char block,
                unsigned char *data, int timeout)
{
    int ret;
    unsigned char cmd[7], ans[21];
    size_t bytes = 0;

    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 2;
    cmd[3] = 'r';
    cmd[4] = block;
    cmd[5] = cmd[1] ^ cmd[2] ^ cmd[3] ^ cmd[4];
    cmd[6] = ETX;

    ret = write(fd, cmd, sizeof(cmd));
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR )
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
        if (bytes > 3) {
            switch (ans[3]) {
                case 'N':
                    acr120_errno = 9;
                    return ACR120_ERROR;
                case 'F':
                    acr120_errno = 13;
                    return ACR120_ERROR;
            }
        }
    }
    if (data)
        memcpy(data, ans + 3, 16);
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_read_value(int fd, unsigned char sid, unsigned char block,
                unsigned int *value, int timeout)
{
    int ret;
    unsigned char cmd[8], ans[9], data[4];
    size_t bytes = 0;
   
    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 0x3;
    cmd[3] = 'r';
    cmd[4] = 'v';
    cmd[5] = block;
    cmd[6] = cmd[1] ^ cmd[2] ^ cmd[3] ^ cmd[4] ^ cmd[5];

    cmd[7] = ETX;

printf("%x %x %x %c %c %x %x %x\n",cmd[0],cmd[1],cmd[2],cmd[3],cmd[4],cmd[5],cmd[6],cmd[7]);

    ret = write(fd, cmd, sizeof(cmd));
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }
   
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);

        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;

        if (bytes > 3) {
            switch (ans[3]) {
                case 'N':
                    acr120_errno = 9;
                    return ACR120_ERROR;
                case 'I':
                    acr120_errno = 10;
                    return ACR120_ERROR;
                case 'F':
                    acr120_errno = 13;
                    return ACR120_ERROR;
            }
        }
    }

printf("%i\n",ret);
printf("%c %c %c\n",ans[3],cmd[3],cmd[4]);
printf("%x %x %x %x %x %x %x %x %x %x\n",cmd[0],cmd[1],cmd[2],cmd[3],cmd[4],cmd[5],cmd[6],cmd[7],cmd[8],cmd[9]);
    if (value) {
        memcpy(data, ans + 3, sizeof(data));  
        nibble2dec(value, data);

    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_read_register(int fd, unsigned char sid, unsigned char reg,
                unsigned char *value, int timeout)
{
    int ret;
    unsigned char cmd[8], ans[6], check = 0;
    size_t bytes = 0, nw, nr;

    if (acr120_proto_mode) {
        cmd[0] = STX;
        cmd[1] = sid;
        cmd[2] = 0x3;
        cmd[3] = 'r';
        cmd[4] = 'e';
        cmd[5] = reg;
        cmd[6] = cmd[1] ^ cmd[2] ^ cmd[3] ^ cmd[4] ^ cmd[5];
        cmd[7] = ETX;
        nw = sizeof(cmd);
        nr = sizeof(ans);
    } else {
        snprintf((char *) cmd, 5 * sizeof(unsigned char), "re%.2x", reg);
        nw = 4 * sizeof(unsigned char);
        nr = 4 * sizeof(unsigned char);
    }

    ret = write(fd, cmd, nw);
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }

    while (bytes < nr) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, nr - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
        if (acr120_proto_mode && bytes > 4 && !check) {
            switch (ans[3]) {
                case 'N':
                    acr120_errno = 9;
                    return ACR120_ERROR;
                case 'I':
                    acr120_errno = 10;
                    return ACR120_ERROR;
                case 'F':
                    acr120_errno = 13;
                    return ACR120_ERROR;
            }
            check ^= 1;
        }
    }

    if (acr120_proto_mode) {
        if (check_bin_error(ans, sizeof(ans))) {
            acr120_errno = 24;
            return ACR120_ERROR;
        }
        *value = ans[3];
    } else {
        if (check_ascii_error(ans, 4)) {
            acr120_errno = 24;
            return ACR120_ERROR;
        }
        ans[2] = 0;
        sscanf((char*) ans, "%x", (unsigned int*) value);
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_copy_block(int fd, unsigned char sid, unsigned char source,
                unsigned char dest, int timeout)
{
    int ret;
    unsigned char cmd[8], ans[9];
    size_t bytes = 0;

    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 0x3;
    cmd[3] = '=';
    cmd[4] = source;
    cmd[5] = dest;
    cmd[6] = cmd[1] ^ cmd[2] ^ cmd[3] ^ cmd[4] ^ cmd[5];
    cmd[7] = ETX;
   
    ret = write(fd, cmd, sizeof(cmd));
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
        if (bytes > 3) {
            switch (ans[3]) {
                case 'X':
                    acr120_errno = 22;
                    return ACR120_ERROR;
                case 'N':
                    acr120_errno = 9;
                    return ACR120_ERROR;
                case 'I':
                    acr120_errno = 10;
                    return ACR120_ERROR;
                case 'F':
                    acr120_errno = 23;
                    return ACR120_ERROR;
            }
        }
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_inc_value(int fd, unsigned char sid, unsigned char block,
                unsigned int inc, int timeout)
{
    int ret, i;
    unsigned char cmd[11], ans[9], data[4];
    size_t bytes = 0;

    dec2nibble(inc, data);
    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 6;
    cmd[3] = '+';
    cmd[4] = block;
    cmd[5] = data[0];
    cmd[6] = data[1];
    cmd[7] = data[2];
    cmd[8] = data[3];
    cmd[10] = ETX;

    for (i = 1, cmd[9] = 0; i < 9; i++)
        cmd[9] ^= cmd[i];

    ret = write(fd, cmd, sizeof(cmd));
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
        if (bytes > 3) {
            switch (ans[3]) {
                case 'X':
                    acr120_errno = 17;
                    return ACR120_ERROR;
                case 'N':
                    acr120_errno = 9;
                    return ACR120_ERROR;
                case 'I':
                    acr120_errno = 10;
                    return ACR120_ERROR;
                case 'F':
                    acr120_errno = 18;
                    return ACR120_ERROR;
            }
        }
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_dec_value(int fd, unsigned char sid, unsigned char block,
                unsigned int dec, int timeout)
{
    int ret, i;
    unsigned char cmd[11], ans[9], data[4];
    size_t bytes = 0;
   
    dec2nibble(dec, data);
    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 6;
    cmd[3] = '-';
    cmd[4] = block;
    cmd[5] = data[0];
    cmd[6] = data[1];
    cmd[7] = data[2];
    cmd[8] = data[3];
    cmd[10] = ETX;
   
    for (i = 1, cmd[9] = 0; i < 9; i++)
        cmd[9] ^= cmd[i];

    ret = write(fd, cmd, sizeof(cmd));
    if (ret == -1) {
        acr120_errno = 5;
        return ACR120_ERROR;
    }
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, ans + bytes, sizeof(ans) - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
        if (bytes > 3) {
            switch(ans[3]) {
                case 'X':
                    acr120_errno = 19;
                    return ACR120_ERROR;
                case 'N':
                    acr120_errno = 9;
                    return ACR120_ERROR;
                case 'I':
                    acr120_errno = 10;
                    return ACR120_ERROR;
                case 'F':
                case 'E':
                    acr120_errno = 20;
                    return ACR120_ERROR;
            }
        }
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_power_on(int fd, unsigned char sid, int timeout)
{
    int ret;
    unsigned char cmd[8], ans[6];
    size_t bytes = 0;

    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 3;
    cmd[3] = 'p';
    cmd[4] = 'o';
    cmd[5] = 'n';
    cmd[6] = cmd[1] ^ cmd[2] ^ cmd[3] ^ cmd[4] ^ cmd[5];
    cmd[7] = ETX;
   
    ret = write(fd, cmd, sizeof(cmd));
    if (ret == ACR120_ERROR) {
        acr120_errno = 5;
        return ret;
    }

    /*
     * actually no error condition answer here, but the
     * reader will send 6 bytes of characters reply, so we
     * only receive the buffer to make sure it is empty
     */
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, cmd + bytes, sizeof(cmd) - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

int acr120_power_off(int fd, unsigned char sid, int timeout)
{
    int ret;
    unsigned char cmd[9], ans[6];
    size_t bytes = 0;

    cmd[0] = STX;
    cmd[1] = sid;
    cmd[2] = 4;
    cmd[3] = 'p';
    cmd[4] = 'o';
    cmd[5] = 'f';
    cmd[6] = 'f';
    cmd[7] = cmd[1] ^ cmd[2] ^ cmd[3] ^ cmd[4] ^ cmd[5] ^ cmd[6];
    cmd[8] = ETX;

    ret = write(fd, cmd, sizeof(cmd));
    if (ret == ACR120_ERROR) {
        acr120_errno = 5;
        return ret;
    }
    while (bytes < sizeof(ans)) {
        if (timeout) {
            ret = acr120_timeout(fd, timeout);
            if (ret == ACR120_ERROR)
                return ret;
        }
        ret = read(fd, cmd + bytes, sizeof(cmd) - bytes);
        if (ret == -1) {
            acr120_errno = 6;
            return ACR120_ERROR;
        }
        bytes += ret;
    }
    acr120_errno = 1;
    return ACR120_SUCCESS;
}

man.c //acr1

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "acr120.h"

#define STX 0x2
#define ETX 0x3
#define sid 0x1

unsigned char cmd[8], ans[6], val, check = 0;
size_t bytes = 0;

int fd,ret;
//unsigned int val;
unsigned int u[4];
unsigned int man,men;
unsigned char id;
unsigned char key[]={0XFF,0XFF,0XFF,0XFF,0XFF,0XFF};
unsigned char data;
unsigned int valo=78;





int main(){


//static const char const *acr120_error[]={};

//acr120_reset(fd, 1, 1,500);

//while(1){
fd=acr120_open ("/dev/ttyUSB0", 1, 38400);
//acr120_power_off(fd, 1, 500);
//acr120_power_on(fd, 1, 1500);


acr120_select(fd,1,&man,5000);

//acr120_get_id(fd,&id, 500);
acr120_login(fd, 1,1,MIFARE_KEY_AA,key, 500);
//usleep(20000);

//acr120_write_value(fd,1, 4,valo, 500);
//acr120_read_block(fd,1, 01,
   //             &data,5000);

acr120_read_value(fd,1,4,&men, 500);
//printf("coba= %i\n",coba);

//acr120_write_value(fd, 1, 01, 123, 500);

//acr120_write_block(fd,1, 01, id, 500);


//acr120_write_register (fd, 1, ACR120_REGISTER_BUZZER , 20, 250);




//printf("%d\n",&val);

//nibble2dec(unsigned int *val, unsigned char *nibble);

//printf("acr120_errno = %d\n",acr120_errno);
printf("acr120_strerror = %s\n",acr120_strerror());
//usleep(2000);
//}

}

/*gcc man.c acr120.c -o man */

Minggu, 04 Mei 2014


setting network eth0

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
name Ethernet LAN card
address 192.168.1.1
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.254

#>/etc/init.d/networking stop

#>/etc/init.d/networking start

lts.c

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <stdlib.h>
#include <ctype.h>
#include <curses.h>
#include <time.h>

int current_getch;
int doloop = 1,no=1;
static WINDOW *mainwnd;
static WINDOW *screen;
WINDOW *my_win;

char serialport[]="/dev/ttyUSB0";
char pilih,pil,back;
int fd;
int n,w;
int ch,i,b;
char buf[255];

int now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
time_t now;
struct tm *now_tm;

void screen_init(void)
{
   mainwnd = initscr();
   noecho();
   cbreak();
   nodelay(mainwnd, TRUE);
   refresh();
   wrefresh(mainwnd);
   screen = newwin(5, 80, 0, 0);
   box(screen, ACS_VLINE, ACS_HLINE);
}

static void update_display(void) {
curs_set(0);
init_pair(2,COLOR_BLUE, COLOR_WHITE);
wbkgd(screen,COLOR_PAIR(2));
attrset(COLOR_PAIR(1));
attron(A_BOLD);


   mvwprintw(screen,1,15,"TIME: %d:%d:%d", now_hour, now_min, now_sec);
   mvwprintw(screen,2,15,"DATE: %d-%d-%d", now_day, now_month, now_year);
   mvwprintw(screen,1, 1, " LTS 1.4 ");
   attroff(A_BOLD);
   wrefresh(screen);
   refresh();

     
}

void screen_end(void)
{
   endwin();
}

void maketime(void)
    {
        // Get the current date/time
        now = time (NULL);
        now_tm = localtime (&now);
        now_sec = now_tm->tm_sec;
        now_min = now_tm->tm_min;
        now_hour = now_tm->tm_hour;
        now_day = now_tm->tm_mday;
        now_wday = now_tm->tm_wday;
        now_month = now_tm->tm_mon + 1;
        now_year = now_tm->tm_year + 1900;
    }


int main ( int argc, char *argv[] )
{
    if (strcmp(argv[2], "0")==0) strcpy(serialport, "/dev/ttyUSB0");
    if (strcmp(argv[2], "1")==0) strcpy(serialport, "/dev/ttyS1");
    if (strcmp(argv[2], "2")==0) strcpy(serialport, "/dev/ttyUSB0");
    if (strcmp(argv[2], "3")==0) strcpy(serialport, "/dev/ttyS3");


if( argc == 3 ){
if(strcmp(argv[1], "L")==0){
initscr();
start_color();
init_pair(1,COLOR_WHITE, COLOR_BLACK);
bkgd(COLOR_PAIR(1));

//clear ( ); refresh ( );

if(!has_colors()){endwin();fprintf(stderr,"Error- no color support on this terminal\n");exit(1);}
if(start_color() != OK){endwin();fprintf(stderr,"Error- tidak dapat mewarnai\n");exit(2);}
//
LOOP:while(doloop)
{
//start_color();
screen_init();
maketime();
update_display();


    mvprintw ( 6, 1, " 1. Test Komunikasi Lts ");
mvprintw ( 7, 1, " 2. Test Firmware Lts");      
    mvprintw ( 8, 1, " 3. Test VCD");
mvprintw ( 9, 1, " 4. Test LLA");
mvprintw ( 10, 1, " 5. Test VIA");
mvprintw ( 11, 1, " 6. Test LLB");    
mvprintw ( 12, 1, " 7. Test LCB");
mvprintw ( 13, 1, " 8. Runing LED Indikator TCT ");
mvprintw ( 14, 1, " 9. LVD");

mvprintw ( 15, 1, " q. EXIT ");

    noecho ( ); pilih=getch( ); echo ( );

       if (pilih=='1')
        {
  clear (); refresh ();
  screen_init();


while(1){clear();
usleep(200000);
screen_init();
maketime();
update_display();
mainwnd;
mvprintw ( 13, 1, " q. Back << ");
refresh ( );
wrefresh(mainwnd);
refresh ();
open_serial();

w=write(fd,"\x36",1);
//usleep(200000);
n=read(fd, &buf, sizeof (buf));
buf[n]=0;

if (n == -1)
{
mvprintw(16,1, "open_port: Unable to open /dev/ttyUSB0 - ");
}

if(n == 1){mvprintw(16,1, "open_port: Open /dev/ttyUSB0 - ");}


if (getch()=='q'){
usleep(2000);
close(fd);
        clear();
        refresh();
        goto LOOP;}
}
 }


else if(pilih=='2')
{
clear (); refresh ();

screen_init();
open_serial();
w=write(fd,"?",1);
usleep(20000);
    mvprintw(8,1, "Write = %i",w);

clear (); refresh ();
n=read(fd, &buf, sizeof (buf));
buf[n]=0;
mvprintw (9, 1, "Buf = %i", n);
mvprintw (10,1, "Reply = %s", buf);

//close(fd);


while(1){

screen_init();
maketime();
update_display();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

 if (getch()=='q'){
        clear();
        refresh();
        goto LOOP;}}
 }

else if(pilih=='3'){

clear (); refresh ();
open_serial();


while(1)
{
screen_init();
maketime();
update_display();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

        mvprintw ( 6, 1, " a. Lamp All on ");        mvprintw ( 6, 26, " 1. Lamp All off ");
mvprintw ( 7, 1, " b. Lamp Gol 1 on ");      mvprintw ( 7, 26, " 2. Lamp Gol 1 off ");
mvprintw ( 8, 1, " c. Lamp Gol 2 on ");      mvprintw ( 8, 26, " 3. Lamp Gol 2 off ");
mvprintw ( 9, 1, " d. Lamp Gol 3 on ");      mvprintw ( 9, 26, " 4. Lamp Gol 3 off ");
mvprintw ( 10, 1, " e. Lamp Gol 4 on ");      mvprintw ( 10, 26, " 5. Lamp Gol 4 off ");
mvprintw ( 11, 1, " f. Lamp Gol 5 on ");      mvprintw ( 11, 26, " 6. Lamp Gol 5 off ");
mvprintw ( 12, 1, " g. Lamp Gol Dinas on ");  mvprintw ( 12, 26, " 7. Lamp Gol Dinas off ");

mvprintw ( 13, 1, " q. Back << ");

noecho ( ); back=getch( );echo ( );
if (back== 'a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x1f",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
//close(fd);
}



else if(back== 'b')
{
clear (); refresh ();
screen_init();
open_serial();


w=write(fd,"\x21",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);

       }
     

else if (back=='c')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x23",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back=='d')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x25",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='e')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x27",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back=='f')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x29",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='g')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x2b",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='1')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x20",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='2')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x22",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='3')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x24",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='4')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x26",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='5')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x28",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='6')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x2A",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='7')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x2C",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}
}}




else if(pilih=='4'){

clear (); refresh ();
open_serial();


while(1)
{
screen_init();
maketime();
update_display();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();


mvprintw ( 6, 1, " a. LLA Merah On, Hijau Off ");
mvprintw ( 7, 1, " b. LLA Merah Off,Hijau On ");  

mvprintw ( 9, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();


w=write(fd,"\x2d",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);

}


else if(back=='b')
{
clear (); refresh ();
screen_init();
open_serial();


w=write(fd,"\x2e",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}

else if(pilih=='5'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
maketime();
update_display();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();


mvprintw ( 6, 1, " a. VIA On");
mvprintw ( 7, 1, " b. VIA Off ");  

mvprintw ( 9, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x2f",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='b')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x30",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}

else if(pilih=='6'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
maketime();
update_display();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

mvprintw ( 6, 1, " a. LLB Merah On, Hijau Off ");
mvprintw ( 7, 1, " b. LLB Merah Off,Hijau On ");  

mvprintw ( 9, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x31",1);
        usleep(50000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back=='b')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x32",1);
        usleep(50000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17, 1, "Reply = %s", buf);
}

else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}


else if(pilih=='7'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
maketime();
update_display();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

mvprintw ( 6, 1, " a. LMS LCB Close Aktif ");
mvprintw ( 7, 1, " b. LMS LCB Open Aktif ");
mvprintw ( 8, 1, " c. LMS LCB Close & LMS LCB Open tidak Aktif ");

mvprintw ( 10, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x70",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back== 'b')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x71",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='c')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x72",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}

else if(pilih=='8'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
maketime();
update_display();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

mvprintw ( 6, 1, "a. Runing LED Indikator TCT ");

mvprintw ( 8, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x35",1);
        usleep(20000);
        mvprintw(12,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (13, 1, "Buf = %i", n);
mvprintw (14,1, "Reply = %s", buf);
}

        else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}

else if(pilih=='9'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
maketime();
update_display();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

mvprintw ( 6, 1, " a. Deteksi LVD Ch 1 On ");      mvprintw ( 6, 32, " 1. Deteksi LVD Ch 1 Off ");
mvprintw ( 7, 1, " b. Deteksi LVD Ch 2 On ");      mvprintw ( 7, 32, " 2. Deteksi LVD Ch 2 Off" );
mvprintw ( 8, 1, " c. Deteksi LVD Ch 3 On ");      mvprintw ( 8, 32, " 3. Deteksi LVD Ch 3 Off ");

mvprintw ( 10, 1, " d. Status Deteksi LVD Ch 1 ");  mvprintw ( 9, 32, " 4. Reset LVD Aktif( Ch 1, Ch 2, Ch 3 ) ");
mvprintw ( 11, 1, " e. Status Deteksi LVD Ch 2 ");  
mvprintw ( 12, 1, " f. Status Deteksi LVD Ch 3 ");  


mvprintw ( 14, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if (back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

//w=write(fd,"\x79",1);
        //usleep(20000);
        //mvprintw(12,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if (back=='b')
{
clear (); refresh ();
screen_init();
open_serial();

//w=write(fd,"\x7b",1);
        //usleep(20000);
       // mvprintw(12,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='c')
{
clear (); refresh ();
screen_init();
open_serial();

//w=write(fd,"\x7d",1);
       // usleep(20000);
       // mvprintw(12,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;

mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back=='1')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x7a",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if (back=='2')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x7c",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='3')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x7e",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='d')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x49",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back== 'e')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x4b",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back== 'f')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x4d",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='4')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x37",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}
else if (pilih=='q'){
        doloop=0;}


}

endwin();}}

else if(argc > 3)
{
printf("Command Line Error ./menu Cmd COM\n\n ");
printf("cmd      -> 'L' lts-test\n");
printf("          -> 'I' ivs-test\n");
printf("          -> 'A' alb-test\n");
printf("          -> 'C' cdp-test\n");
printf("          -> 'P' lpr-test\n\n");
printf(" port     -> '0'-'11'  Com1-Com12\n");
}


else if( argv[2] != 0 ){
printf("Command Line Error ./menu Cmd COM\n\n ");
printf("cmd      -> 'L' lts-test\n");
printf("          -> 'I' ivs-test\n");
printf("          -> 'A' alb-test\n");
printf("          -> 'C' cdp-test\n");
printf("          -> 'P' lpr-test\n\n");
printf(" port     -> '0'-'11'  Com1-Com12\n");
}

else if( argc == 2 ){
printf("Command Line Error ./menu Cmd COM\n\n ");
printf("cmd      -> 'L' lts-test\n");
printf("          -> 'I' ivs-test\n");
printf("          -> 'A' alb-test\n");
printf("          -> 'C' cdp-test\n");
printf("          -> 'P' lpr-test\n\n");
printf(" port     -> '0'-'11'  Com1-Com12\n");
}


else
{
printf("\033[2J");
//printf("\033[XA");


printf("Command Line Error ./menu Cmd COM\n\n ");
printf("cmd      -> 'L' lts-test\n");
printf("          -> 'I' ivs-test\n");
printf("          -> 'A' alb-test\n");
printf("          -> 'C' cdp-test\n");
printf("          -> 'P' lpr-test\n\n");
printf(" port     -> '0'-'11'  Com1-Com12\n");

}


}

int open_serial(void)
{

fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
mvprintw(21,35, "open_port: Unable to open /dev/ttyUSB0 - ");

}

else{mvprintw(21,35, "open_port: Open /dev/ttyUSB0 - ");}

fcntl(fd, F_SETFL, FNDELAY);
struct termios options;
  tcgetattr( fd, &options );
  cfsetispeed( &options, B19200 );
  cfsetospeed( &options, B19200 );
  options.c_cflag |= ( CLOCAL | CREAD );
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  options.c_iflag &= ~(IXON | IXOFF | IXANY);
  options.c_oflag &= ~OPOST;

if ( tcsetattr( fd, TCSANOW, &options ) == -1 )
mvprintw (22,35,"Error with tcsetattr = %s\n", strerror ( errno ) );
else
mvprintw (22,35,"%s\n", "tcsetattr succeed" );
return 0;
}


/* gcc menu.c -o menu -lncurses */

menu lts.c

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <stdlib.h>
#include <ctype.h>
#include <curses.h>
#include <time.h>
#include <form.h>




int current_getch;
int doloop = 1,no=1;
static WINDOW *mainwnd;
static WINDOW *screen;
WINDOW *my_win;

char serialport[]="/dev/ttyUSB0";
char pilih,pil,back;
int fd;
int n,w;
int ch,i,b;
char buf[255];

int now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
time_t now;
struct tm *now_tm;

//int wmove(WINDOW *my_win, int y, int x)

void screen_init(void)
{

now = time (NULL);
        now_tm = localtime (&now);
        now_sec = now_tm->tm_sec;
        now_min = now_tm->tm_min;
        now_hour = now_tm->tm_hour;
        now_day = now_tm->tm_mday;
        now_wday = now_tm->tm_wday;
        now_month = now_tm->tm_mon + 1;
        now_year = now_tm->tm_year + 1900;


  mainwnd = initscr();
  //noecho();
  //cbreak();
  nodelay(mainwnd, TRUE);
  refresh();
  wrefresh(mainwnd);
  //screen = newwin(5, 80, 0, 0);
//box(screen, ACS_VLINE,ACS_HLINE);
//wmove(screen, y, x);


curs_set(0);
init_pair(2,COLOR_BLUE, COLOR_WHITE);
wbkgd(screen,COLOR_PAIR(2));
//attrset(COLOR_PAIR(1));
attron(COLOR_PAIR(2));
attron(A_BOLD);

mvprintw(0,0,"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
mvprintw(1,0,"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//mvprintw(1,0,"                                                                                      ");
mvprintw(2,0,"                                                                                      ");
mvprintw(3,0,"                                                                                      ");
mvprintw(4,0,"                                                                                      ");
//mvprintw(5,0,"                                                                                      ");
mvprintw(2,15,"TIME: %d:%d:%d", now_hour, now_min, now_sec);
mvprintw(3,15,"DATE: %d-%d-%d", now_day, now_month, now_year);
mvprintw(2, 1, " LTS 1.4 ");
mvprintw(4,0,"______________________________________________________________________________________");
attroff(A_BOLD);
attroff(COLOR_PAIR(2));
//wrefresh(screen);
refresh();
}

void screen_end(void)
{
   endwin();
}


int main (int argc, char *argv[] )
{
    if (strcmp(argv[2], "0")==0) strcpy(serialport, "/dev/ttyUSB0");
    if (strcmp(argv[2], "1")==0) strcpy(serialport, "/dev/ttyS1");
    if (strcmp(argv[2], "2")==0) strcpy(serialport, "/dev/ttyUSB0");
    if (strcmp(argv[2], "3")==0) strcpy(serialport, "/dev/ttyS3");


if( argc == 3 ){
if(strcmp(argv[1], "L")==0){
initscr();
start_color();
init_pair(1,COLOR_WHITE, COLOR_BLACK);
//bkgd(COLOR_PAIR(1));

//clear ( ); refresh ( );

if(!has_colors()){endwin();fprintf(stderr,"Error- no color support on this terminal\n");exit(1);}
if(start_color() != OK){endwin();fprintf(stderr,"Error- tidak dapat mewarnai\n");exit(2);}
//
LOOP:while(doloop)
{
start_color();
screen_init();


mvprintw ( 6, 1, " 1. Test Komunikasi Lts ");
mvprintw ( 7, 1, " 2. Test Firmware Lts");      
mvprintw ( 8, 1, " 3. Test VCD");
mvprintw ( 9, 1, " 4. Test LLA");
mvprintw ( 10, 1, " 5. Test VIA");
mvprintw ( 11, 1, " 6. Test LLB");    
mvprintw ( 12, 1, " 7. Test LCB");
mvprintw ( 13, 1, " 8. Runing LED Indikator TCT ");
mvprintw ( 14, 1, " 9. LVD");

mvprintw ( 15, 1, " q. EXIT ");

//addch(ACS_HLINE);

//keypad(stdscr, TRUE);
   // noecho ( );
 pilih=getch( );
//echo ( );
//cbreak();
//nonl();
       if (pilih=='1')
        {
  clear (); refresh ();
  screen_init();


while(1){clear();
usleep(200000);
screen_init();
mainwnd;
mvprintw ( 13, 1, " q. Back << ");
refresh ( );
wrefresh(mainwnd);
refresh ();
open_serial();

w=write(fd,"\x36",1);
//usleep(200000);
n=read(fd, &buf, sizeof (buf));
buf[n]=0;

if (n == -1)
{
mvprintw(16,1, "open_port: Unable to open /dev/ttyUSB0 - ");
}

if(n == 1){mvprintw(16,1, "open_port: Open /dev/ttyUSB0 - ");}


if (getch()=='q'){
usleep(2000);
close(fd);
        clear();
        refresh();
        goto LOOP;}
}
 }


else if(pilih=='2')
{
clear (); refresh ();

screen_init();
open_serial();
w=write(fd,"?",1);
usleep(20000);
    mvprintw(8,1, "Write = %i",w);

clear (); refresh ();
n=read(fd, &buf, sizeof (buf));
buf[n]=0;
mvprintw (9, 1, "Buf = %i", n);
mvprintw (10,1, "Reply = %s", buf);

//close(fd);


while(1){

screen_init();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

 if (getch()=='q'){
        clear();
        refresh();
        goto LOOP;}}
 }

else if(pilih=='3'){

clear (); refresh ();
open_serial();


while(1)
{
screen_init();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

        mvprintw ( 6, 1, " a. Lamp All on ");        mvprintw ( 6, 26, " 1. Lamp All off ");
mvprintw ( 7, 1, " b. Lamp Gol 1 on ");      mvprintw ( 7, 26, " 2. Lamp Gol 1 off ");
mvprintw ( 8, 1, " c. Lamp Gol 2 on ");      mvprintw ( 8, 26, " 3. Lamp Gol 2 off ");
mvprintw ( 9, 1, " d. Lamp Gol 3 on ");      mvprintw ( 9, 26, " 4. Lamp Gol 3 off ");
mvprintw ( 10, 1, " e. Lamp Gol 4 on ");      mvprintw ( 10, 26, " 5. Lamp Gol 4 off ");
mvprintw ( 11, 1, " f. Lamp Gol 5 on ");      mvprintw ( 11, 26, " 6. Lamp Gol 5 off ");
mvprintw ( 12, 1, " g. Lamp Gol Dinas on ");  mvprintw ( 12, 26, " 7. Lamp Gol Dinas off ");

mvprintw ( 13, 1, " q. Back << ");

noecho ( ); back=getch( );echo ( );
if (back== 'a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x1f",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
//close(fd);
}



else if(back== 'b')
{
clear (); refresh ();
screen_init();
open_serial();


w=write(fd,"\x21",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);

       }
     

else if (back=='c')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x23",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back=='d')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x25",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='e')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x27",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back=='f')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x29",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='g')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x2b",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='1')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x20",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='2')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x22",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='3')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x24",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='4')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x26",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='5')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x28",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='6')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x2A",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='7')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x2C",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}
}}




else if(pilih=='4'){

clear (); refresh ();
open_serial();


while(1)
{
screen_init();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();


mvprintw ( 6, 1, " a. LLA Merah On, Hijau Off ");
mvprintw ( 7, 1, " b. LLA Merah Off,Hijau On ");  

mvprintw ( 9, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();


w=write(fd,"\x2d",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);

}


else if(back=='b')
{
clear (); refresh ();
screen_init();
open_serial();


w=write(fd,"\x2e",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}

else if(pilih=='5'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();


mvprintw ( 6, 1, " a. VIA On");
mvprintw ( 7, 1, " b. VIA Off ");  

mvprintw ( 9, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x2f",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='b')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x30",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}

else if(pilih=='6'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

mvprintw ( 6, 1, " a. LLB Merah On, Hijau Off ");
mvprintw ( 7, 1, " b. LLB Merah Off,Hijau On ");  

mvprintw ( 9, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x31",1);
        usleep(50000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back=='b')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x32",1);
        usleep(50000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17, 1, "Reply = %s", buf);
}

else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}


else if(pilih=='7'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

mvprintw ( 6, 1, " a. LMS LCB Close Aktif ");
mvprintw ( 7, 1, " b. LMS LCB Open Aktif ");
mvprintw ( 8, 1, " c. LMS LCB Close & LMS LCB Open tidak Aktif ");

mvprintw ( 10, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x70",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back== 'b')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x71",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='c')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x72",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}

else if(pilih=='8'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

mvprintw ( 6, 1, "a. Runing LED Indikator TCT ");

mvprintw ( 8, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if(back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x35",1);
        usleep(20000);
        mvprintw(12,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (13, 1, "Buf = %i", n);
mvprintw (14,1, "Reply = %s", buf);
}

        else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}

else if(pilih=='9'){
clear (); refresh ();
open_serial();

while(1)
{
screen_init();
mainwnd;
refresh ( );
wrefresh(mainwnd);
refresh ();

mvprintw ( 6, 1, " a. Deteksi LVD Ch 1 On ");      mvprintw ( 6, 32, " 1. Deteksi LVD Ch 1 Off ");
mvprintw ( 7, 1, " b. Deteksi LVD Ch 2 On ");      mvprintw ( 7, 32, " 2. Deteksi LVD Ch 2 Off" );
mvprintw ( 8, 1, " c. Deteksi LVD Ch 3 On ");      mvprintw ( 8, 32, " 3. Deteksi LVD Ch 3 Off ");

mvprintw ( 10, 1, " d. Status Deteksi LVD Ch 1 ");  mvprintw ( 9, 32, " 4. Reset LVD Aktif( Ch 1, Ch 2, Ch 3 ) ");
mvprintw ( 11, 1, " e. Status Deteksi LVD Ch 2 ");  
mvprintw ( 12, 1, " f. Status Deteksi LVD Ch 3 ");  


mvprintw ( 14, 1, " q. Back << ");
noecho ( ); back=getch( );echo ( );

if (back=='a')
{
clear (); refresh ();
screen_init();
open_serial();

//w=write(fd,"\x79",1);
        //usleep(20000);
        //mvprintw(12,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if (back=='b')
{
clear (); refresh ();
screen_init();
open_serial();

//w=write(fd,"\x7b",1);
        //usleep(20000);
       // mvprintw(12,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='c')
{
clear (); refresh ();
screen_init();
open_serial();

//w=write(fd,"\x7d",1);
       // usleep(20000);
       // mvprintw(12,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;

mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if(back=='1')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x7a",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if (back=='2')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x7c",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='3')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x7e",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='d')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x49",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back== 'e')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x4b",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back== 'f')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x4d",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}

else if(back=='4')
{
clear (); refresh ();
screen_init();
open_serial();

w=write(fd,"\x37",1);
        usleep(20000);
        mvprintw(15,1, "Write = %i",w);
n=read(fd, &buf, sizeof (buf));
        buf[n]=0;
mvprintw (16, 1, "Buf = %i", n);
mvprintw (17,1, "Reply = %s", buf);
}


else if (back=='q'){
        clear();
        refresh();
        goto LOOP;}


}}
else if (pilih=='q'){
        doloop=0;}


}

endwin();}}

else if(argc > 3)
{
printf("Command Line Error ./menu Cmd COM\n\n ");
printf("cmd      -> 'L' lts-test\n");
printf("          -> 'I' ivs-test\n");
printf("          -> 'A' alb-test\n");
printf("          -> 'C' cdp-test\n");
printf("          -> 'P' lpr-test\n\n");
printf(" port     -> '0'-'11'  Com1-Com12\n");
}


else if( argv[2] != 0 ){
printf("Command Line Error ./menu Cmd COM\n\n ");
printf("cmd      -> 'L' lts-test\n");
printf("          -> 'I' ivs-test\n");
printf("          -> 'A' alb-test\n");
printf("          -> 'C' cdp-test\n");
printf("          -> 'P' lpr-test\n\n");
printf(" port     -> '0'-'11'  Com1-Com12\n");
}


else if( argc == 2 ){
printf("Command Line Error ./menu Cmd COM\n\n ");
printf("cmd      -> 'L' lts-test\n");
printf("          -> 'I' ivs-test\n");
printf("          -> 'A' alb-test\n");
printf("          -> 'C' cdp-test\n");
printf("          -> 'P' lpr-test\n\n");
printf(" port     -> '0'-'11'  Com1-Com12\n");
}


else
{
printf("\033[2J");
//printf("\033[XA");


printf("Command Line Error ./menu Cmd COM\n\n ");
printf("cmd      -> 'L' lts-test\n");
printf("          -> 'I' ivs-test\n");
printf("          -> 'A' alb-test\n");
printf("          -> 'C' cdp-test\n");
printf("          -> 'P' lpr-test\n\n");
printf(" port     -> '0'-'11'  Com1-Com12\n");

}

return 0;
}

int open_serial(void)
{

fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
mvprintw(21,35, "open_port: Unable to open /dev/ttyUSB0 - ");

}

else{mvprintw(21,35, "open_port: Open /dev/ttyUSB0 - ");}

fcntl(fd, F_SETFL, FNDELAY);
struct termios options;
  tcgetattr( fd, &options );
  cfsetispeed( &options, B19200 );
  cfsetospeed( &options, B19200 );
  options.c_cflag |= ( CLOCAL | CREAD );
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  options.c_iflag &= ~(IXON | IXOFF | IXANY);
  options.c_oflag &= ~OPOST;

if ( tcsetattr( fd, TCSANOW, &options ) == -1 )
mvprintw (22,35,"Error with tcsetattr = %s\n", strerror ( errno ) );
else
mvprintw (22,35,"%s\n", "tcsetattr succeed" );
return 0;
}


/* gcc menu.c -o menu -lncurses */