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_, &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_, &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_, &sync[i], 1, &bytesWritten, NULL);
Sleep(300);
cout<<"Reading ACK for SYNC "<<endl;
for (i= 0; i < 6; i++) ReadFile (handlePort_, &c[i], 1, &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_, &c[i], 1, &dwBytesTransferred, 0);
for (i = 0; i < 6; i++) cout<<std::hex<<c[i]<<endl;
Sleep(300);
for (i = 0; i < 6; i++)
WriteFile(handlePort_, &Synchronize_ack[i], 1, &bytesWritten, NULL);
Sleep(300);
/* ======= End of SYNC Command ========================= */
cout<<"******** Initialization ***************"<<endl;
for (i = 0; i < 6; i++) WriteFile(handlePort_, &Initial[i], 1, &bytesWritten, NULL);
Sleep(300);
cout<<"Reading ACK for Initialization "<<endl;
for (i = 0; i < 6; i++) ReadFile (handlePort_, &d[i], 1, &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_, &GetPicture[i], 1, &bytesWritten, NULL);
Sleep(300);
cout<<"Reading ACK for GetPicture "<<endl;
for (i = 0; i < 6; i++) ReadFile (handlePort_, &e[i], 1, &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_, &f[i], 1, &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_, &c[0], 1, &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_, &last_ack[i], 1, &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.
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_, &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_, &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_, &sync[i], 1, &bytesWritten, NULL);
Sleep(300);
cout<<"Reading ACK for SYNC "<<endl;
for (i= 0; i < 6; i++) ReadFile (handlePort_, &c[i], 1, &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_, &c[i], 1, &dwBytesTransferred, 0);
for (i = 0; i < 6; i++) cout<<std::hex<<c[i]<<endl;
Sleep(300);
for (i = 0; i < 6; i++)
WriteFile(handlePort_, &Synchronize_ack[i], 1, &bytesWritten, NULL);
Sleep(300);
/* ======= End of SYNC Command ========================= */
cout<<"******** Initialization ***************"<<endl;
for (i = 0; i < 6; i++) WriteFile(handlePort_, &Initial[i], 1, &bytesWritten, NULL);
Sleep(300);
cout<<"Reading ACK for Initialization "<<endl;
for (i = 0; i < 6; i++) ReadFile (handlePort_, &d[i], 1, &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_, &GetPicture[i], 1, &bytesWritten, NULL);
Sleep(300);
cout<<"Reading ACK for GetPicture "<<endl;
for (i = 0; i < 6; i++) ReadFile (handlePort_, &e[i], 1, &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_, &f[i], 1, &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_, &c[0], 1, &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_, &last_ack[i], 1, &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.
Comment