Serial Port Stream / Bash или C Script

У меня есть последовательное устройство (радар скорости), которое выдает данные каждые 250 мс.

Вот информация об устройстве:

1. Speed Packet Protocol
The Radar message packet consists of 7 bytes @ 1200 baud, no parity, 8 data bits, 1 start
bit. Messages are paced at 250mS intervals and are sent whether there is a target or not.
Char Description
1 <STX> Start of message
2 Status Radar status, as defined below
3 Patrol Speeds in binary from 4 to 255, Speed values below 4 are treated as
zero.
4 Target
5 Lock
6 Alt Alternate speed, as defined below
7 <ETX> End of message
1.1. Radar status byte
Bit Description/function if set
0 Low voltage error
1 Radio frequency interference error
2 Front antenna
3 Rear antenna
4 Moving mode
5 Alternate mode (fastest or slow)
6 Opposite direction mode
7 Always set (Indicated a speed packet)
Neither antenna being selected indicates standby.
Both antennae selected indicates a self-test is in progress. Test results are not sent, the
radar either returns to normal operation if successful or ceases communication in the
event of failure.
1.2. Alternate Speed
If the alternate mode bit is set, the alternate speed should be displayed. This will be
fastest if in opposite direction or slow if in same direction.
1.3. Message Receipt
Each message received should be time stamped and removed after 1 second if it has not
been replaced by a new message. The only exception is the self-test message, which
allows 8 seconds for the test to complete.
1.4. Example
Received sequence 02, 244(0xf4), 50, 99, 75, 01, 03.
Front antenna, moving mode, fastest mode, opposite direction mode, patrol speed of 50,
strongest target at 99, a locked speed of 75 and a no fastest target (since the value is
less that 4). 

Использование:

minicom -b 1200 -D /dev/ttyUSB0 -8 -H -w

Данные выглядят так, если читается 0:

02 c8 00 00 00 00 03

И так, если читается что-то другое:

02 c8 00 16 00 00 03

или

02 c8 00 0f 00 00 03

Так что, вероятно, это будет выглядеть так:

//How do I pipe the stream into the script??
"Get String Code Here"
//example string
$string=02 c8 00 16 00 00 03
//remove first part
$prefix=02 c8 00
//remove last part 
$suffix=00 00 03
//result which appears to be a hex value
$result=16 (22MPH)
//convert $result to decimal

echo $result to monitor/file/database

Я также наткнулся на сценарий на 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 */



//Initialize serial port
int initport(int fd)
{
    int portstatus = 0;

    struct termios options;
    // Get the current options for the port...
    tcgetattr(fd, &options);
    // Set the baud rates to 115200...
    cfsetispeed(&options, B1200);
    cfsetospeed(&options, B1200);
    // Enable the receiver and set local mode...
    options.c_cflag |= (CLOCAL | CREAD);

    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    //options.c_cflag |= SerialDataBitsInterp(8);           /* CS8 - Selects 8 data bits */
    options.c_cflag &= ~CRTSCTS;                            // disable hardware flow control
    options.c_iflag &= ~(IXON | IXOFF | IXANY);           // disable XON XOFF (for transmit and receive)
    //options.c_cflag |= CRTSCTS;                     /* enable hardware flow control */


    options.c_cc[VMIN] = 0;     //min carachters to be read
    options.c_cc[VTIME] = 0;    //Time to wait for data (tenths of seconds)


    // Set the new options for the port...
    //tcsetattr(fd, TCSANOW, &options);


    //Set the new options for the port...
    tcflush(fd, TCIFLUSH);
    if (tcsetattr(fd, TCSANOW, &options)==-1)
    {
        perror("On tcsetattr:");
        portstatus = -1;
    }
    else
        portstatus = 1;


    return portstatus;
}


/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int open_port(void)
{
    int fd; /* File descriptor for the port */
    fd = open("/dev/ttyUSB0", O_RDONLY | O_NOCTTY | O_NDELAY);

    if (fd == -1)
    {
        /*
        * Could not open the port.
        */
        perror("open_port: Unable to open /dev/ttyUSB0 --- \n");
    }
    else
        fcntl(fd, F_SETFL, 0);

    return (fd);
}

int main(void)
{

    int serial_fd = open_port();

    if(serial_fd == -1)
        printf("Error opening serial port /dev/ttyUSB0 \n");
    else
    {
        printf("Serial Port /dev/ttyUSB0 is now open \n");

        // READ PORT DATA 


        if(initport(serial_fd) == -1)
        {
            printf("Error Initializing port");
            close(serial_fd);
            return 0;
        }

        sleep(.5);
        //usleep(500000);
        //printf("size of data being sent = %ld", sizeof("~ver~\n\r"));

        sleep(.5);
        //usleep(500000);

        printf("\n\nNow closing Serial Port /dev/ttyUSB0 \n\n");

        close(serial_fd);
    }


    return 0;
}
0
17.04.2019, 01:20
1 ответ

Ладно, думаю, все, что вам нужно, это:

"Get String Code Here"  
string="02 c8 00 16 00 00 03"  
result=$(echo ${string} | cut -w -f 3)  

Но это в шестнадцатеричном формате, так что сравнивать будет сложно:

decimalresult=$(printf "%d" 0x${result})  

Конечно, можно сделать это и в одну строку, но я сделал так для наглядности.

Исправленная версия

Вы говорите, что у вас есть непрерывный поток и вам нужно прочитать его как часть скрипта. Сценарии оболочки обычно читаются строками, поэтому мы делаем это:

  • Вы используете minicomи передаете его вывод в скрипт. Обязательно используйте параметр -Hдля получения шестнадцатеричного вывода, поскольку сценарии оболочки плохо работают с двоичным кодом. Вот так:
    minicom -b 1200 -D /dev/ttyUSB0 -8 -H -w | this_script
  • Ввод передается через ddдля разделения на фрагменты, составляющие одно сообщение, с добавлением новой строки в конце.
  • Это входит в цикл, который считывает семь элементов в каждой строке, игнорируя все, кроме четвертого.
  • Шестнадцатеричный формат преобразуется в десятичный и выводится.

    dd conv=unblock cbs=21 | while read x x x hexresult x x x ; do decimalresult=$(printf "%d" 0x${hexresult}) echo ${decimalresult} done

Я не пробовал это с непрерывным потоком, так что вам, возможно, придется немного настроить его. В частности, вам может понадобиться добавить obs=21к параметрам dd(может быть даже 20, а не 21 ).

0
28.01.2020, 04:44

Теги

Похожие вопросы