Announcement

Collapse
No announcement yet.

Image is not readable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Image is not readable

    My RS232 device is working very fine with demo software even I am getting the pixel
    data also with my own code but the resulting image is not readable. below is my code




    #include <windows.h>
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <dos.h>
    #include <stdio.h>
    #include <conio.h>



    using namespace std;

    BYTE sync[6] = { 0xAA, 0x0D, 0x00, 0x00, 0x00, 0x00 };
    BYTE Synchronize_ack[6] = { 0xAA, 0x0E, 0x0D, 0x00, 0x00, 0x00 };

    // Preview Picture (80*160 resolution,8-bit colour,uncompressed RAW picture)
    BYTE Initial[6] = { 0xAA, 0x01, 0x00, 0x04, 0x01, 0x00 };

    //Picture type preview raw picture
    BYTE GetPicture[6] = { 0xAA, 0x04, 0x02, 0x00, 0x00, 0x00 };
    BYTE last_ack[6] = { 0xAA, 0x0E, 0x00, 0x00, 0xF0, 0xF0 };


    int c[6];
    int d[6];
    int e[6];
    int f[6];
    int g[6];
    int h[6];

    int wait = 1;


    int main(int argc, char *argv[])
    {

    /* ================== Windows Serial Port Initialzation ====== */

    /*BaudRate = 57600
    ByteSize = 8
    Parity = NOPARITY
    StopBits = ONESTOPBIT */



    HANDLE handlePort_;
    DCB config_;
    COMMTIMEOUTS comTimeOut;
    DWORD bytesWritten = 0;
    DWORD dwBytesTransferred = 0;
    DWORD Mask;

    BOOL fSuccess;
    char *pcCommPort = "COM1";
    handlePort_ = CreateFile( pcCommPort,
    GENERIC_READ | GENERIC_WRITE,
    0, // must be opened with exclusive-access
    NULL, // no security attributes
    OPEN_EXISTING, // must use OPEN_EXISTING
    0, // not overlapped I/O
    NULL // hTemplate must be NULL for comm devices
    );
    if (handlePort_ == INVALID_HANDLE_VALUE)
    {
    // Handle the error.
    printf ("CreateFile failed with error %d.\n", GetLastError());
    return (1);
    }

    fSuccess = GetCommState(handlePort_, &amp;config_);
    if (!fSuccess)
    {
    // Handle the error.
    printf ("GetCommState failed with error %d.\n", GetLastError());
    return (2);
    }

    config_.BaudRate = 57600; // set the baud rate
    config_.ByteSize = 8; // data size, xmit, and rcv
    config_.Parity = NOPARITY; // no parity bit
    config_.StopBits = ONESTOPBIT; // one stop bit
    fSuccess = SetCommState(handlePort_, &amp;config_);
    if (!fSuccess)
    {
    // Handle the error.
    printf ("SetCommState failed with error %d.\n", GetLastError());
    return (3);
    }
    int gelung;
    printf ("Serial port %s successfully reconfigured.\n", pcCommPort);

    /* ======== End of Serial Port Initialization ============================== */

    int i;

    /* ======= SYNC Command ========================= */
    for (i = 0; i < 6; i++)WriteFile(handlePort_, &amp;sync[i], 1, &amp;bytesWritten, NULL);
    Sleep(300);
    cout<<"Reading ACK for SYNC "<<endl;
    for (i= 0; i < 6; i++) ReadFile (handlePort_, &amp;c[i], 1, &amp;dwBytesTransferred, 0);
    for (i = 0; i < 6; i++) cout<<std::hex<<c[i]<<endl;
    Sleep(300);



    cout<<"Reading Sync from cam "<<endl;
    for (i= 0; i < 6; i++) ReadFile (handlePort_, &amp;c[i], 1, &amp;dwBytesTransferred, 0);
    for (i = 0; i < 6; i++) cout<<std::hex<<c[i]<<endl;
    Sleep(300);

    for (i = 0; i < 6; i++)
    WriteFile(handlePort_, &amp;Synchronize_ack[i], 1, &amp;bytesWritten, NULL);
    Sleep(300);

    /* ======= End of SYNC Command ========================= */


    cout<<"******** Initialization ***************"<<endl;
    for (i = 0; i < 6; i++) WriteFile(handlePort_, &amp;Initial[i], 1, &amp;bytesWritten, NULL);
    Sleep(300);
    cout<<"Reading ACK for Initialization "<<endl;
    for (i = 0; i < 6; i++) ReadFile (handlePort_, &amp;d[i], 1, &amp;dwBytesTransferred, 0);
    for (i = 0; i < 6; i++) cout<<std::hex<<d[i]<<endl;


    cout<<"********GET Picture Command ************"<<endl;
    for (i = 0; i < 6; i++) WriteFile(handlePort_, &amp;GetPicture[i], 1, &amp;bytesWritten, NULL);
    Sleep(300);
    cout<<"Reading ACK for GetPicture "<<endl;
    for (i = 0; i < 6; i++) ReadFile (handlePort_, &amp;e[i], 1, &amp;dwBytesTransferred, 0);
    for (i = 0; i < 6; i++) cout<<std::hex<<e[i]<<endl;


    cout<<"*******Reading Image Size ***********"<<endl;
    Sleep(300);
    cout<<"Reading ACK for Image Size "<<endl;
    for (i = 0; i < 6; i++) ReadFile (handlePort_, &amp;f[i], 1, &amp;dwBytesTransferred, 0);
    for (i = 0; i < 6; i++) cout<<std::hex<<f[i]<<endl;
    cout<<"*****************************************"<<endl;


    Sleep(300);
    int k=0;


    FILE *ima=fopen("c:\\preview.bmp","wb");


    cout<<"******************* Reading The Pixel Data ***************"<<endl;

    //Since we are taking 80*60 resolution so total bytes to read would be 4800.

    for( int j = 0; j < 4800; j++) {

    ReadFile (handlePort_, &amp;c[0], 1, &amp;dwBytesTransferred, 0);
    fprintf(ima,"%c",c[0]);
    printf("\n DATA of Pixel=== %d",c[0]);
    k=k+1;
    }


    cout<<"******************* Last Acknowledgment from Host ***************"<<endl;

    for ( i = 0; i < 6; i++) WriteFile(handlePort_, &amp;last_ack[i], 1, &amp;bytesWritten, NULL);






    fclose(ima);


    CloseHandle(handlePort_);

    printf("\n Data is successfully written.\n");
    for (i = 0; i < 6; i++) cout<<"The Image Size is"<<std::hex<<f[i]<<endl;
    system("pause");
    return (0);
    }



    Your help in this regard will be appreciated.

    with regards,
    Muhammad Faisal.


  • #2


    I'm sorry, I'm not really in a position to try your code, but at first glance it looks OK.

    Is the problem that you can't figure out what to do with the 4800 bytes you get back?

    You have set the camera to 8 bit color so the data will be in 332 format as per the manual, bit(0,0) till be folowed by bit(1,0) etc until the first row is transferred, then the second row, etc.

    If you are running on windows you will need to convert each byte to a 24 bit RGB value (probably) and use PutPixel (or similar) to put it in the current x,y position.

    Converting 8bit 332 color to 24bit BGR is roughly

    colb := trunc(((incolor & 3) / 4) * 256) ;
    colg := trunc((((incolor >> 2) & 7) / 8) * 256) ;
    colr := trunc((((incolor >> 5) & 7) / 8) * 256) ;
    colbgr := colb
    Mark

    Comment


    • #3


      Hello,
      ESPsupport can you give me more explanation on this 4 lines please :

      "colb := trunc(((incolor & 3) / 4) * 256) ;
      colg := trunc((((incolor >> 2) & 7) / 8) * 256) ;
      colr := trunc((((incolor >> 5) & 7) / 8) * 256) ;
      colbgr := colb

      Comment


      • #4


        Gee that's a while back.



        The poster needed to convert an 8 bit RGB 332 color into a 24bit BGR 888 color.



        So in depth



        8 bit RGB 332 is



        0bRRRGGGBB



        24bit BGR 888 is



        0bBBBBBBBBGGGGGGGGRRRRRRRR



        so you need to move the bits of the 332 color into the ighest bits of the 888 color and that is what the code is attampting to do.
        Mark

        Comment


        • #5


          thanks for your help but i would like to show the code in the post work .

          Comment

          Working...
          X