TestEngineer11
TestEngineer11

Reputation: 61

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). This text is actually the text output by an embedded PC when it boots up. I then need to parse that text output for certain tokens. 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? 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. Is there a default value to feed into that function to not use that method?

NOTE: A UI is not required, this is for an automated test. 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;

}

 

Upvotes: 1

Views: 416

Answers (1)

Ianis Martin
Ianis Martin

Reputation: 13

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. 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.

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'). 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. but using an EOT will never hurt.

Upvotes: 1

Related Questions