简体   繁体   English

从串口读取的标准方法是什么

[英]What is the standard method for reading from Serial Port

I have a test case where I need to read text coming across a serial Bus (RS-232).我有一个测试用例,我需要读取串行总线 (RS-232) 上的文本。 This text is actually the text output by an embedded PC when it boots up.这个文本实际上是嵌入式PC开机时的文本output。 I then need to parse that text output for certain tokens.然后,我需要为某些令牌解析该文本 output。 I am trying to develop a general approach to this problem.我正在尝试开发一种解决此问题的通用方法。 Here is my approach:这是我的方法:

Configure COM port Open/create the file to write the text to Write bytes read from port to the file Any pointers here to help me along, or anything you think is missing?配置 COM 端口 打开/创建文件以将文本写入 将从端口读取的字节写入文件 此处有任何指针可以帮助我,或者您认为缺少什么? For the function ComToFile, I'm not sure what to use for the parameter "termination byte" because I don't know yet what the text looks like in its entirety.对于 function ComToFile,我不确定参数“终止字节”使用什么,因为我还不知道整个文本是什么样的。 Is there a default value to feed into that function to not use that method?是否有默认值可以输入 function 以不使用该方法?

NOTE: A UI is not required, this is for an automated test.注意:不需要 UI,这是用于自动化测试。 Also, I'm debating whether I should write the boot text to a file and then parse the tokens from that file as I've done, Or is it best to just store it in a large buffer within the program and parse that buffer?另外,我正在讨论是否应该将引导文本写入文件,然后像我所做的那样解析该文件中的令牌,或者最好将其存储在程序内的一个大缓冲区中并解析该缓冲区?

#include <formatio.h>
#include<stdio.h>
#include<string.h>
#include<rs232.h>

#define PORT_NUMBER 1
#define BUFFER_SIZE 10000

int main (void)
{
    
    char buffer[BUFFER_SIZE];
    int bytes = 100;
    int fileHandle;
    int status;
    
    if (0  > (OpenComConfig(PORT_NUMBER, "COM1", 115200, 0, 7, 1, 5000, 512)))   //Opens connection to COM port, closes program if error code returned
    {
        printf("Error: COM port could not be opened\n");
        return -1;
    }
    
    fileHandle = OpenFile ("BootText.txt", VAL_READ_WRITE, VAL_TRUNCATE, VAL_ASCII);
    status = SetComTime(PORT_NUMBER, 10);
    bytes =  ComToFile(PORT_NUMBER, fileHandle, 2000, 0x0D);     
    
    return 0;

}

 

I'm debating whether I should write the boot text to a file and then parse the tokens from that file as I've done, Or is it best to just store it in a large buffer within the program and parse that buffer?我正在争论是否应该将启动文本写入文件,然后像我所做的那样解析该文件中的令牌,或者最好将其存储在程序内的一个大缓冲区中并解析该缓冲区?

If your text to parse is not that big, just use that 10000 char buffer that you have created but are not using.如果您要解析的文本不是那么大,只需使用您创建但未使用的 10000 个字符缓冲区。 Plus if this text has no revelant use after being parsed, is changed at every launch and can be discarded then do not use a file.另外,如果此文本在解析后没有相关用途,每次启动时都会更改并且可以丢弃,然后不要使用文件。 file should only be used to store data "permanently"文件只能用于“永久”存储数据

For the function ComToFile, I'm not sure what to use for the parameter "termination byte" because I don't know yet what the text looks like in its entirety.对于 function ComToFile,我不确定参数“终止字节”使用什么,因为我还不知道整个文本是什么样的。

the termination caracter is use to stop the read before count: for anexample if you know that you want to read up to 1000 but only read one line (so to stop on a '\r') or if you don't know the length of the message you'll read (in that case put 0 in 'count').终止字符用于在计数之前停止读取:例如,如果您知道要读取最多 1000 行但只读取一行(因此要在 '\r' 处停止)或者如果您不知道长度您将阅读的消息的数量(在这种情况下,将 0 放入“计数”中)。 So it does not really matter if you know that you will always read 2000 bytes and that ther will alwas be 2000 bytes available.因此,如果您知道您将始终读取 2000 个字节并且始终有 2000 个字节可用,这并不重要。 but using an EOT will never hurt.但使用 EOT 永远不会受到伤害。

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

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