0个粉丝

3

问答

0

专栏

0

资料

 发布于  2024-12-16 10:50:25
采纳率 34%
3个问答
112

有没有rv1126 38主控板串口收发例程?单号:2401711033755726096

 

在板子和电脑互通的串口例程

我来回答
回答3个
时间排序
认可量排序

Stranger

13个粉丝

0

问答

5

专栏

8

资料

Stranger 2024-12-16 11:21:39
认可0
易百纳技术社区该回答已被题主采纳为最佳答案
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <pthread.h>

char searial_parameter[32];
int fd;
char exitFlag = 1;
char send_buffer[512]="uart send test\n";

int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
    speed_t speed;
    struct termios newtio, oldtio;
    if (tcgetattr(fd, &oldtio) != 0)
    {
        perror("SetupSerial 1");
        return -1;
    }
    bzero(&newtio, sizeof(newtio));
    newtio.c_cflag |= CLOCAL | CREAD;
    newtio.c_cflag &= ~CSIZE;

    switch (nSpeed)
    {
    case 9600:
        speed = B9600;
        strcat(searial_parameter," 9600");
        break;
    case 19200:
        speed = B19200;
        strcat(searial_parameter," 19200");
        break;
    case 38400:
        speed = B38400;
        strcat(searial_parameter," 38400");
        break;
    case 57600:
        speed = B57600;
        strcat(searial_parameter," 57600");
        break;
    case 115200:
        speed = B115200;
        strcat(searial_parameter," 115200");
        break;
       case 230400:
        speed = B230400;
        strcat(searial_parameter," 230400");
        break;
    case 460800:
        speed = B460800;
        strcat(searial_parameter," 460800");
        break;
    case 500000:
        speed = B500000;
        strcat(searial_parameter," 500000");
        break;
    case 576000:
        speed = B576000;
        strcat(searial_parameter," 576000");
        break;
    case 921600:
        speed = B921600;
        strcat(searial_parameter," 921600");
        break;
    case 1000000:
        speed = B1000000;
        strcat(searial_parameter," 1000000");
        break;
    case 1152000:
        speed = B1152000;
        strcat(searial_parameter," 1152000");
        break;
    case 1500000:
        speed = B1500000;
        strcat(searial_parameter," 1500000");
        break;
    default:
        speed = B115200;
        strcat(searial_parameter," 115200");
        break;
    }

    switch (nBits)
    {
    case 7:
        newtio.c_cflag |= CS7;

        break;
    case 8:
        newtio.c_cflag |= CS8;
        break;
    }

    switch (nEvent)
    {
    case 'O': //奇校验
        newtio.c_cflag |= PARENB;
        newtio.c_cflag |= PARODD;
        newtio.c_iflag |= (INPCK | ISTRIP);
        strcat(searial_parameter," O");
        break;
    case 'E': //偶校验
        newtio.c_iflag |= (INPCK | ISTRIP);
        newtio.c_cflag |= PARENB;
        newtio.c_cflag &= ~PARODD;
        strcat(searial_parameter," E");
        break;
    case 'N': //无校验
        newtio.c_cflag &= ~PARENB;
        strcat(searial_parameter," N");
        break;
    }

    cfsetispeed(&newtio, speed);
    cfsetospeed(&newtio, speed);
    if (nStop == 1)
    {
        newtio.c_cflag &= ~CSTOPB;
        strcat(searial_parameter," 1");
    }
    else if (nStop == 2)
    {
        newtio.c_cflag |= CSTOPB;
        strcat(searial_parameter," 2");
    }
    newtio.c_cc[VTIME] = 0;
    newtio.c_cc[VMIN] = 0;
#if 0
    switch(flow_ctrl)
    {    
       case 0 ://不使用流控制
              newtio.c_cflag &= ~CRTSCTS;
              break;   
       case 1 ://使用硬件流控制
              newtio.c_cflag |= CRTSCTS;
              break;
       case 2 ://使用软件流控制
              newtio.c_cflag |= IXON | IXOFF | IXANY;
              break;
    }
#endif
    tcflush(fd, TCIFLUSH);
    if ((tcsetattr(fd, TCSANOW, &newtio)) != 0)
    {
        perror("com set error");
        return -1;
    }

    return 0;
}

int open_port(int fd, char *port)
{
    long vdisable;

    fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
    if (-1 == fd)
    {
        perror("Can't Open Serial Port");
        return (-1);
    }

    if (fcntl(fd, F_SETFL, 0) < 0)
    {
        printf("fcntl failed!\n");
    }

    if (isatty(STDIN_FILENO) == 0)
    {
        printf("standard input is not a terminal device\n");
    }
    return fd;
}

void *RecvThread(void )  //参数的值为123
{
    int nread, i;
    char recv_buffer[512];
    char temp[32];

    while (1)
    {
        memset(recv_buffer, 0, sizeof(recv_buffer));
        nread = read(fd, recv_buffer, sizeof(recv_buffer));
        if (nread > 0)
        {
            while(1){
                usleep(2000);
                i = read(fd, temp, sizeof(temp));
                if(i > 0){
                    memcpy(&recv_buffer[nread],temp,i);
                    nread += i;
                }else{
                    break;
                }
            }
            printf("recv %u byte: %s\n", nread, recv_buffer);
            memset(send_buffer, 0, sizeof(send_buffer));
            memcpy(send_buffer, recv_buffer, strlen(recv_buffer)+1);
                        usleep(2000);
            if((0 == strncmp(recv_buffer, "exit",4))||(0 == strncmp(recv_buffer, "EXIT",4)))
                break;
        }else{
            usleep(20000);
        }
    }
    pthread_exit(NULL);
}

void *SendThread(void )  //参数的值为123
{
    while (exitFlag > 0)
    {
        //write(fd, send_buffer, strlen(send_buffer)+1);
        write(fd, "12345", strlen("12345")+1);
        printf("send: %s\n", "12345");
        sleep(1);
    }
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    pthread_t sendId, recvId;

    if(argc != 3){
        printf("paramter error\nSample:%s /dev/ttyS0 115200\n",argv[0]);
        return -1;
    }
    memset(searial_parameter,'\0',sizeof(searial_parameter));
    memcpy(searial_parameter,argv[1],strlen(argv[1]));

    if ((fd = open_port(fd, argv[1])) < 0)
    {
        perror("open_port error");
        return -1;
    }
    if (set_opt(fd, atol(argv[2]), 8, 'N', 1) < 0)
    {
        perror("set_opt error");
        return -1;
    }
    printf("%s\n",searial_parameter);
    printf("After receiving 'exit' or EXIT', exit!\n");

    pthread_create(&sendId, NULL, SendThread, NULL );
    pthread_create(&recvId, NULL, RecvThread, NULL );

    pthread_join(sendId,NULL);
    pthread_join(recvId,NULL);

    close(fd);
    return 0;
}
  回复   Stranger  2024-12-17 15:25:59
0



程序都能跑通,但是串口调试助手出不来数据是啥原因?(已知串口跑例程uart_demo能跑通)

Stranger

13个粉丝

0

问答

5

专栏

8

资料

Stranger 2024-12-19 09:50:49
认可0

1.检查一下tx rx交叉没有
2.串口是不是没有打开,检查一下dts配置

或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
+ 添加网盘链接/附件

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

易百纳技术社区