C程序不断向串口发送相同的数据

标签 c serial-port arm embedded-linux

我正在开发一个运行 ARM 嵌入式 Linux 的 SOM 板,并且正在开发一个 C 程序以通过串行 (RS232) 端口与外部设备进行通信。不过,我遇到了奇怪的行为。我还使用板上的另一个串行端口与板上运行的 Linux 进行通信。

该软件结构简单:是一个类似控制台的纯文本程序,主菜单为:

Possible commands:
1 - 4: Select serial device (pump should be on 1)
m - pump op. mode configuration
r -  reads from the serial device
w -  writes to the serial device
>>>>>>>>>>>>>>Current device is /dev/ttymxc1
>>>>>>>>>>>>>>Enter input (q quits):

和一个二级菜单(通过上面的“m”选项打开)

SPEED:
r - rpm (sends 1M<CR>) //<CR> stands for carriage return
f - flow rate (sends 1N<CR>)
QUANTITY:
v - volume (sends 1H<CR>)
t - time (sends 1O<CR>)
DIRECTION:
c - clockwise (sends 1T<CR>)
a - c-clockwise (sends 1K<CR>)
>>>>>>>>>>>>>>Enter input (q quits):

使用主菜单选项“r”和“w”进行通信工作正常(从而消除了我对波特率等串行设置可能存在的任何疑问):“w”调用一个发送单个数据的例程(下面的“serial_write”)用户输入的字符,而“r”则在数据到达时立即返回读取的数据(使用下面的“serial_read”)。无论我重复“w”和“r”循环多少次,我发送的字符都会正确到达,并且答案会正确显示在控制台上。

二级菜单中的选项应该以相同的方式运行:它们只是调用一个例程(下面的“sendSimpleCmd”),该例程以常量字符作为参数(每个选项不同)调用“serial_write”,然后调用“串行_读取”。

问题是,这仅适用于所选的第一个选项:此后,无论我选择哪个选项,程序都会不断发送链接到所选第一个选项的数据。它会一直这样做,直到我返回主菜单,然后再次选择“m”:此时发送的数据是我期望的数据,但后续选择将被忽略,直到我返回主菜单(或关闭)软件,如果重要的话)。

最奇怪的是,我在与板通信的同一个串行端口上收到了预期的数据,而在“右侧”串行端口上我不断收到第一条消息。这是在选择“f”作为第一个选项后,当我选择“a”作为第二个选项时从控制台粘贴的文本(我添加的注释):

SPEED:
r - rpm (sends 1M<CR>)   
f - flow rate (sends 1N<CR>)
QUANTITY:
v - volume (sends 1H<CR>)
t - time (sends 1O<CR>)
DIRECTION:
c - clockwise (sends 1T<CR>)
a - c-clockwise (sends 1K<CR>)
>>>>>>>>>>>>>>Enter input (q quits):
a                   //second option
1Knding 1M          //mixup of data 
wrote 4 characters on fs 4
serial_read: *

由软件输出(“发送数据1M”)和选择选项“a”(1K)后应发送的数据进行混合。由于在“正确”端口上我一遍又一遍地收到相同的消息,而在“错误”端口上我收到正确的消息,似乎软件以某种方式自动更改端口。

问题是: 此行为可能是由我的编码引起的,还是与其他内容(例如某些内核配置)绑定(bind)?如果需要更多信息,请询问。

提前谢谢

<小时/>

Serial_write

void serial_write(char text[], int length){

    if (selectedDevice == 0){
        printf("Select device first!\r\n");
        return;
    }

    int n;
    length = length +1 + 2; 
    char toBeSent[length];
    strcat(toBeSent, PUMP_CMD_MSG_START); //header, "1"
    strcat(toBeSent, text);
    strcat(toBeSent, PUMP_CMD_MSG_END); //footer, "<CR>"
    printf("Sending %s\r\n", toBeSent);
    n = write (fd, toBeSent, length);
    if (n<0){
        printf("writing failed on /dev/ttymxc%i\r\n", selectedDevice);
    } else {
        printf("wrote %i characters on fs %i\r\n", n, fd);

    }
}

Serial_read

int serial_read(char *buffer, int size){
    int bytes = 0;
    int n;
    int i = 0;
    char tmp_buffer[size];

    while(1){
        ioctl(fd, FIONREAD, &bytes); 
        if (bytes > 0){
            break;
        }
        i++;
        if(i> 1000){
            printf("FIONREAD tries exceeded 1000, aborting read\r\n");
            return;
        }
        usleep(1000);
    }

    n=read(fd, tmp_buffer, sizeof(tmp_buffer));
    for(i=0;i<n;i++) {
        buffer[i]=tmp_buffer[i];
    }
    printf("serial_read: %s\r\n", buffer);
    return 0;
}

sendSimpleCmd

void sendSimpleCmd(char text[]){
    int bufSize= 20;
    char answer[bufSize];
    serial_write(text,1);

    if (serial_read(answer, bufSize) == 0) {
        printf("Ricevuto da pompa \"%s\":", answer);

        //handling of possible answers, doesn't do anything relevant since it always receives "*" as answer
        if (strcmp(answer, PUMP_ANS_OK) == 0){ //PUMP_ANS_OK is "*"
                printf("ok!\r\n");
            } else if (strcmp(answer, PUMP_ANS_NOK) == 0){
                printf("errore!\r\n");
            } else {
                printf("sconosciuto!\r\n");
            }
    } else {
//        printf("read failed\r\n");
    }
}

最佳答案

在使用 strcat 之前,您应该初始化 toBeSent 的内容。

您的编译器可能会通过用 0 而不是垃圾初始化数组来拯救您,但如果不是,则可能会导致缓冲区溢出。没有代码可以防止这种情况,因此这可能是导致意外程序行为的原因。如果不查看代码的其余部分并了解其他一些细节,就很难知道问题到底是什么。如果这作为其余代码的示例,那么解决方案很可能是修改代码来修复这些问题。

考虑使用安全字符串函数来帮助防止缓冲区溢出。

关于C程序不断向串口发送相同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29169633/

相关文章:

node.js <-> 带串口的 arduino。错误 资源暂时不可用 无法锁定端口

c++ - OpenCV v3.3.1 ARM 交叉编译失败 : No CMAKE_CXX_COMPILER could be found

c - 使用C计数器向上和向下进行图形编程

c - 在 C 中为 typedef 枚举添加预定义数据

c - 如何区分字符和数字?

c# - 判断串口是普通COM还是SPP

C 套接字范围 header 无效

c# SerialPort PinChanged 事件处理

ios - iOS 上编译成功但执行汇编 MRC 指令失败?

c - Cortex-M 3 4 7 的 CMSIS 中有类似 ATOMIC_INC 的东西吗?