Ask Your Question
0

How to send value with serial port (C Language)

asked 2013-08-13 09:56:20 -0500

Jack Chung gravatar image

updated 2013-08-13 09:58:12 -0500

Hello guys,

I am doing a project where I get the pixel value from a binary image (black and white - 0 and 255). Now I need to get this pixel value and send it with serial port, where a xBee is connected, but I don't know how can I do that.

I would appreciate it if someone could help me to send the pixel value to the xBee with serial port.

My code to get the pixel value from a binary image is:

#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    uchar binario, valor;

    FILE *fp;
    fp = fopen("Pixel.txt", "w");

    if(fp == NULL){
        printf("\nNão encontrei arquivo\n");
        exit(EXIT_FAILURE);
    }

    cvNamedWindow( "original", CV_WINDOW_AUTOSIZE );
    cvNamedWindow( "new", CV_WINDOW_AUTOSIZE );

    IplImage* src = cvLoadImage("Koala.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    cvShowImage("original", src);

    CvSize size = cvSize(100, 100); 
    IplImage* resize = cvCreateImage(size, src->depth, src->nChannels); 

    cvResize(src, resize, CV_INTER_LINEAR);

    IplImage* img_binario = cvCreateImage(cvGetSize(resize), IPL_DEPTH_8U, 1);
    cvThreshold(resize, img_binario, 100, 255, CV_THRESH_BINARY);

    for(int i = 0; i < img_binario->height; i++){
        for(int j = 0; j < img_binario->width; j++){
            binario = ((uchar *)(img_binario->imageData + i*img_binario->widthStep))[j];

            if(binario == 255)
                valor = 1;
            else
                valor = 0;
            fprintf(fp, "Valor = %i\n", valor);
        }
    }

    fclose(fp);

    cvShowImage("new", img_binario);

    cvWaitKey(0);
    return 0;
}

Thank you.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2013-08-14 02:02:59 -0500

updated 2013-08-14 02:42:05 -0500

That is one cool lib for serial communication http://www.teuniz.net/RS-232/

  1. RS232_OpenComport(cport_number, byte_rate);
  2. RS232_SendByte(cport_number, data); //data is unsigned char, for every pixel
  3. void RS232_CloseComport(cport_number);

I have not used it, but looks very well ;)

p.p. In c# all that is just 1 component and is 100 times easier ;)

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2013-08-13 09:56:20 -0500

Seen: 1,943 times

Last updated: Aug 14 '13