Go Down

Topic: Save photos of serial cam to hard disk (Read 2 times) previous topic - next topic

dhunt

Why don't you add some debug prints to your C# code so you can see what you are receiving and if it makes sense for what you are doing with the received data.

PaulS

Code: [Select]
        private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

How have you defined that this method should be called? If this method is triggered as soon as ANY data arrives on the serial port, then the code in it is suspect.

Code: [Select]
            List<byte> recibido = new List<byte>(); //byte receive
Make an empty List.

Code: [Select]
            int bytes = serialPort2.BytesToRead;
The name is atrocious. It is not an array as implied. It is a count of the number of bytes that are available to read, which will certainly not equal the number of bytes in the picture.

Code: [Select]
            byte[] buffer = new byte[bytes];
Now, we create an array to hold the serial data.

Code: [Select]
            serialPort2.Read(buffer, 0, bytes);
            foreach (byte elem in buffer)
            {
                recibido.Add(elem); //here "it receives the file"
            }

Then, we read all the data that we knew about earlier (more may have arrived) from the serial port, storing the data in the array. Then, we loop, moving each character from the array to the list.

Code: [Select]
        }
Then, the function ends, and the list goes out of scope without ever having been persisted anywhere. So, all the serial data just read, valuable or not, just hit the bit bucket.

Code: [Select]
        private void button1_Click(object sender, EventArgs e)
        {
            List<byte> recibido = new List<byte>();
            serialPort2.Open();
            recibido.Clear();

        }

Create a List, close the serial port, clear the empty list, and let the empty, cleared list go out of scope. What was the purpose of the List?

Code: [Select]
        private void button2_Click(object sender, EventArgs e) //with this button I pretend to show the image after the computer receive it.
        {
            List<byte> recibido = new List<byte>(); //list receive
            serialPort2.Close();

            //Guarda todo en archivo, here It suppose to save everything inside the archive
            if (File.Exists("Prueba.jpg"))
                File.Delete("Prueba.jpg");
            FileStream archivoP = new FileStream("Prueba.jpg",
                FileMode.OpenOrCreate, FileAccess.Write);
            BinaryWriter escribirP = new BinaryWriter(archivoP);
            foreach (byte elem in recibido)
            {
                escribirP.Write(elem);
            }

Create a new, empty List. If the file exists, delete it. Then, open the file. Write the data in the empty list to the file, and close it. Then, try to display the empty picture, and wonder why the picture is empty.

Do you have a clue, now, how to fix your problem?

maveck

Hi People;

I modified the c #2010 code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) // It receives the data when I click the button

        {
            List<byte> recibido = new List<byte>();

            int bytes = serialPort2.BytesToRead;

            byte[] buffer = new byte[bytes];

            serialPort2.Read(buffer, 0, bytes);
            foreach (byte elem in buffer)
            {
                recibido.Add(elem);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<byte> recibido = new List<byte>();
            serialPort2.Open();         
//            recibido.Clear();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<byte> recibido = new List<byte>();
            //serialPort2.Close();
            if (File.Exists("Pruebados.jpg")) ;
            //Guarda todo en archivo                           
            FileStream archivoP = new FileStream("Pruebados.jpg",
                FileMode.OpenOrCreate, FileAccess.Write);
            BinaryWriter escribirP = new BinaryWriter(archivoP);
            foreach (byte elem in recibido)
            {
                escribirP.Write(elem);
            }
            //escribirP.Close();
            //archivoP.Close();
            Process.Start("Pruebados.jpg");
        }
    }
}



And I also modified the arduino code to send only the image data and once:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            List<byte> recibido = new List<byte>();

            int bytes = serialPort2.BytesToRead;

            byte[] buffer = new byte[bytes];

            serialPort2.Read(buffer, 0, bytes);
            foreach (byte elem in buffer)
            {
                recibido.Add(elem);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<byte> recibido = new List<byte>();
            serialPort2.Open();         
//            recibido.Clear();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<byte> recibido = new List<byte>();
            //serialPort2.Close();
            if (File.Exists("Pruebados.jpg")) ;
            //Guarda todo en archivo                           
            FileStream archivoP = new FileStream("Pruebados.jpg",
                FileMode.OpenOrCreate, FileAccess.Write);
            BinaryWriter escribirP = new BinaryWriter(archivoP);
            foreach (byte elem in recibido)
            {
                escribirP.Write(elem);
            }
            //escribirP.Close();
            //archivoP.Close();
            Process.Start("Pruebados.jpg");
        }
    }
}


When I run the C program; the arduino shield doesn't show the TX led transmitting, so I suppose that the program doesn't run correctly the serial port, because when I do the test with the VCO 706 Comm tool; the tx led runs, and starts to show the data (same happens with the serial monitor of the arduino soft):


FF D8 FF FE 00 24 59 00 5B 0D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 00 40 01 1B 00 32 12 0B 51 04 51 04 00 00 FF DB 00 84 00 07 05 05 06 05 04 07 06 05 06 08 07 07 08 0A 11 0B 0A 09 09 0A 14 0E 0F 0C 10 18 15 19 18 17 15 17 16 1A 1D 25 20 1A 1C 23 1C 16 17 21 2C 21 23 27 28 2A 2A 2A 19 1F 2E 31 2E 29 31 25 29 2A 28 01 07 07 (.....more and more hexa caracters........)77 37 F1 7E 55 71 87 73 29 4C 6F 96 07 6A 5D B5 64 5C 4D B4 9B B6 93 6D 2D 80 42 B4 D2



I appreciate your help.




PaulS

I don't think that that "Arduino code" will actually compile for the Arduino.

The C# code is still wrong. All serial data received still ends up in the bit bucket.

maveck

Oh sorry, here you have the arduino code that I compiled to transmit the image data once.

#include <SoftwareSerial.h>
  #include <NewSoftSerial.h>
#include <VC0706.h>

int  incomingbyte=0;

NewSoftSerial cameraconnection = NewSoftSerial(2, 3);
VC0706 cam = VC0706(&cameraconnection);

void setup () {

   
       Serial.begin(9600);
               if (cam.begin()) {
        cam.takePicture() ;
               }
  delay(3000);
  uint16_t jpglen = cam.frameLength();
//  Serial.print(jpglen, DEC);
//  Serial.println(" byte image");
 
      while (jpglen != 0) {
      // read 64 bytes at a time;
      uint8_t *buffer;
      uint8_t bytesToRead = min(32, jpglen);   // change 32 to 64 for a speedup but may not work with all setups!
      buffer = cam.readPicture(bytesToRead);
      Serial.write(buffer, bytesToRead);
      //Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");
      jpglen -= bytesToRead;
      }
 
  }
 

void loop (){
 

}

Go Up