简体   繁体   English

C语言的数据链路层编程

[英]data link layer programming in c

I'm totally newbie about this thing and I want to know where to start.. 我对这件事完全是新手,我想知道从哪里开始。
I have a manual that specifies data link layer that includes command and response frames to access a device connected in /dev/ttyUSB0 . 我有一本指定数据链路层的手册,其中包括命令和响应帧以访问/ dev / ttyUSB0中连接的设备。

Example of the given command frame is setting the baud rate 给定命令帧的示例是设置波特率

Head = 0x0A 磁头 = 0x0A
Address = NULL/blank 地址 = NULL /空白
Length = 0x03 长度 = 0x03
Command = 0x20 命令 = 0x20
Parameter = 0x00 参数 = 0x00
Check = cc 检查 = cc

Where parameter 0x00 is equal to baud rate 9600bps. 其中参数0x00等于波特率9600bps。

My question is how do I use this in programming? 我的问题是如何在编程中使用它? can I use it on C language? 我可以在C语言上使用它吗?
My OS platform is ubuntu 12.04. 我的操作系统平台是ubuntu 12.04。
Any link or idea would be a great help. 任何链接或想法都会有很大帮助。

UPDATE This is the command I used in read() UPDATE这是我在read()中使用的命令

    unsigned char rx_buffer[1024];
    size_t RX_buffer_len;
    ssize_t bytes_read;
    int fd;

    RX_buffer_len = sizeof(rx_buffer);
    bytes_read = read (serial, rx_buffer, RX_buffer_len);

You could start defining the your packet message structure 您可以开始定义您的分组消息结构

// Enable 1 byte alignment
#pragma pack(1)

typedef struct
{
    uint8_t Head;
    uint8_t Address;
    uint8_t Length;
    uint8_t Command;
    uint8_t Parameter;
    uint8_t Check;
}typ_packet;

// Restore the original alignment
#pragma pack()

Then you can access and configure ttyUSB0. 然后,您可以访问和配置ttyUSB0。 A simple example: 一个简单的例子:

struct termios2 t;

int serial, baud;

// Open the uart low level device driver and set up all params
serial_fd = open("/dev/ttyUSB0", O_NOCTTY | O_NDELAY);

if (serial != -1)
{
    baud = 9600;

    if (ioctl(serial, TCGETS2, &t))
    {
        // Fails to read tty pars
        exit(1);
    }

    t.c_cflag &= ~CBAUD;
    t.c_cflag |= BOTHER;
    t.c_cflag |= CSTOPB;
    t.c_ospeed = baud;

    // Noncanonical mode, disable signals, extended
    // input processing, and echoing
    t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);

    // Disable special handling of CR, NL, and BREAK.
    // No 8th-bit stripping or parity error handling.
    // Disable START/STOP output flow control.
    t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR |
                      INPCK | ISTRIP | IXON | PARMRK);

    // Disable all output processing
    t.c_oflag &= ~OPOST;

    if (ioctl(serial, TCSETS2, &t))
    {
        // Failed to set serial parameters
        exit(1);
    }
}
else
{
    // Failed to open ttyUSB0
    exit(1);
}

Then you can simply read from serial line by 然后您可以简单地从串行线读取

res = read (serial_fd, rx_buffer, RX_buffer_len);

And write using 并使用写

typ_packet packet;

packet.Head = 0x0A;
packet.Address = 0x00;
packet.Length = 0x03;
packet.Command = 0x20;
packet.Parameter = 0x00;
packet.Check = 0xCC;

write(serial_fd, &packet, 6);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM