Search code examples
cserial-portuart

Sending BMP image to serial port using C


I work with EDC mpos to get user signature

now I save customer signature as image like "sign.bmp" then I want to send this image to serial port (I have my C# app to received data). So I can not send image to serial port. Here is what I have try :

void ReadImageToBuffer(char fileName[],char szTemp[])
{
    FILE *file;
    long fileLength;
    char *buffer;

    lcdCls();
    SignatureHeaderBar();
    lcdDisplay(0,3,DISP_CFONT|DISP_CLRLINE,"file name: %s",fileName);
    lcdFlip();
    kbGetKey();

    // open file
    file = fopen(fileName,"r");

    if (!file)
    {
        lcdCls();
        SignatureHeaderBar();
        lcdDisplay(0,3,DISP_CFONT|DISP_CLRLINE,"Unable to open file %s",fileName);
        lcdFlip();
        kbGetKey();
    }
    else
    {
        lcdCls();
        SignatureHeaderBar();
        lcdDisplay(0,3,DISP_CFONT|DISP_CLRLINE,"file opened");
        lcdFlip();
        kbGetKey();

        // Get file length
        fseek(file, 0, SEEK_END); // Jumpt to the end of file
        fileLength = ftell(file); // Get current byte offset
        fseek(file, 0, SEEK_SET); // Jump back to begin at file

        // allocate memory
        buffer = (char *)malloc((fileLength+1)*sizeof(char));

        if (!buffer)
        {
            lcdCls();
            SignatureHeaderBar();
            lcdDisplay(0,3,DISP_CFONT|DISP_CLRLINE,"Memory error");
            lcdFlip();
            kbGetKey();
        }
        else
        {
            // read file content
            fread(buffer, fileLength, 1, file);

            lcdCls();
            SignatureHeaderBar();
            lcdDisplay(0,3,DISP_CFONT|DISP_CLRLINE,"readed content");
            lcdFlip();
            kbGetKey();
        }

        // close file
        fclose(file);

        lcdCls();
        SignatureHeaderBar();
        lcdDisplay(0,3,DISP_CFONT|DISP_CLRLINE,"szTemp size %d",strlen(szTemp));
        lcdDisplay(0,5,DISP_CFONT|DISP_CLRLINE,"buffer %d",buffer ? 1 : 0);
        lcdDisplay(0,7,DISP_CFONT|DISP_CLRLINE,"signResult size %d",strlen(signResult));
        lcdFlip();
        kbGetKey();

        //void ExternalSerialWrite(char dataByte[]);
        ExternalSerialWrite(buffer);
    }
}

I'm not C developer I only know basic C and googling "How to..." on internet

So my problem is How to send image to serial port (in my case I think I can not get image data as byte)

Here is my ExternalSerialWrite()

void ExternalSerialWrite(char dataByte[])
{
    write(*uartIfd, dataByte, strlen(dataByte));
}

Solution

  • Images are binary files.

    So first of all you need to open it as such. Secondly, since the data are arbitrary bytes of any value, including embedded zeros (which happens to be equal to the string null terminator) you can't use string functions such as strlen.

    You need to keep track of the size of the data you read (which should not include the +1 you add when allocating), and pass that size to the ExternalSerialWrite function as an argument.