Serial port 从ELB-REV4上的UART2读取的UART代码示例

Serial port 从ELB-REV4上的UART2读取的UART代码示例,serial-port,Serial Port,我需要样本c代码从UART2读取,在ELB-REV4设备上的波特率为3Mbps 我尝试了一些示例代码,但没有成功 我希望数据波特率或UART2为3Mbps,我可以用示波器看到 问题是一旦我将UART2波特率设置为3000000。在示波器上,我看到波特率仍然是115200。在ELB-REV4上/dev/ttyS2代表UART2,在UART0上使用/dev/ttyS0,使用下面的代码输出101010,您可以在示波器上看到并测量波特率 #include <stdio.h> #inclu

我需要样本c代码从UART2读取,在ELB-REV4设备上的波特率为3Mbps

我尝试了一些示例代码,但没有成功

我希望数据波特率或UART2为3Mbps,我可以用示波器看到


问题是一旦我将UART2波特率设置为3000000。在示波器上,我看到波特率仍然是115200。

在ELB-REV4上/dev/ttyS2代表UART2,在UART0上使用/dev/ttyS0,使用下面的代码输出101010,您可以在示波器上看到并测量波特率

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <termios.h>
 #include <ctype.h>
 #include <errno.h>

 struct termios options;
 int fd;
 int status;
 char buf[10];
 unsigned char rx_buffer[10];
 int main(void) 
        {
           fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);



      if (fd < 0) 
        {
           printf("Error opening serial port\n");
           exit(1);
        }



      bzero( & options, sizeof(options));
        options.c_cflag = B3000000 | CS8 | CLOCAL | CREAD | IGNPAR | PARODD | PARENB; //8bit, Odd parity, 
        tcflush(fd, TCIFLUSH);
        tcsetattr(fd, TCSANOW, & options);

    while (1) 
    {
buf[0] = 0x55; //pattern
status = write(fd, & buf[0], 1);

    if (status < 0) 
    {


     printf("Write error - %s \n", strerror(errno));
        exit(1);
    }


     usleep(1000000);
        printf("Sending 0x55 to /dev/ttyS2 port\n");
    }

    }
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构termios选项;
int-fd;
智力状态;
char-buf[10];
无符号字符rx_缓冲区[10];
内部主(空)
{
fd=开放(“/dev/ttyS2”,O|RDWR | O|NOCTTY | O|NDELAY);
如果(fd<0)
{
printf(“打开串行端口时出错\n”);
出口(1);
}
bzero(期权和期权),sizeof(期权));
options.c|cflag=B3000000 | CS8 | CLOCAL | CREAD | IGNPAR | PARODD | PARENB;//8位奇数校验,
tcflush(fd,TCIFLUSH);
tcsetattr(fd、TCSANOW和选项);
而(1)
{
buf[0]=0x55;//模式
状态=写入(fd和buf[0],1);
如果(状态<0)
{
printf(“写入错误-%s\n”,strerror(errno));
出口(1);
}
美国LEEP(1000000);
printf(“将0x55发送到/dev/ttyS2端口\n”);
}
}

您尝试了哪些示例代码?当它不起作用时发生了什么,它没有做任何事情,或者做了一些超出你预期的事情?您应该编辑您的问题以包含这些要点。如果你把你的问题保持原样,这对这个网站的格式来说太宽泛了。谢谢saurabh,让我试试。我想我是将这个值设置为options.c_cflag=3000000,但我不知道我需要附加B。我想它正在工作,我能够在示波器上测量波特率,再次感谢