Go Down

Topic: Send Bitmap on pc to arduino from serial (Read 1 time) previous topic - next topic

ehsanzamani

Hello team members
I use the Orduino Mega Model 2560
But I did everything I could to transfer a photo file from the system to Orduino and it couldn't be shown.
My screen is TFT and my picture is about 3K in size
From the computer I convert the image to a bitmap and then render it to a byte
But it is not possible to read and read it on the Orduino side
Anyone have any suggestions?

UKHeliBob

Please post your best effort

What is sending the file from the PC ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ehsanzamani

I created a program in C # that made a 3 KB Qrcode file

I converted it to bitmap and then converted it to byte via memory stream

Then I sent it through

  PortName = "COM10";
  BaudRate = 9600;
  DataBits = 8;
  Parity = Parity.None;
  StopBits = StopBits.One;

 serial port

From the Orduino I read this code as follows



#include "Adafruit_GFX.h"    // Core graphics library
#include "MCUFRIEND_kbv.h"   // Hardware-specific library
MCUFRIEND_kbv tft;

#define BLACK 0x0000
#define RED 0xF800
#define WHITE 0xFFFF



int index = 0;
char Imagebmp[3062];
char x;



void setup() {
    Serial.begin(9600);
    uint16_t ID = tft.readID();
   if (ID == 0x0D3D3) ID = 0x9481;
  tft.begin(ID);
  tft.invertDisplay(true);
  tft.setRotation(1);
  tft.fillScreen(BLACK);
}

void loop() {
 if (Serial.available() > 0)
  {
    x = Serial.read();
   Imagebmp[index] = x;
   Serial.flush();
   index++;
  }

}


UKHeliBob

Quote
it is not possible to read and read it on the Orduino side
What happens when you try ?

Have you tried printing the "bytes" as they are received ?
Do they look as you expect ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ehsanzamani

#4
Feb 23, 2020, 12:20 pm Last Edit: Feb 23, 2020, 12:23 pm by Ehsanzamani
No Exactly the problem here is the information I send
It is called incomplete

for example

After printing the index on the page I realized that this loop was for 317
The load worked and then the connection to receive the information was disconnected from the serial
And that means 317 bytes are sent but we haven't sent the rest and the data is incomplete

Do you think there is a way I can upload a bitmap photo from my computer and display it on the TFT screen after it has been uploaded?

sterretje

#5
Feb 23, 2020, 12:24 pm Last Edit: Feb 23, 2020, 04:50 pm by sterretje
To my knowledge the Mega resets when you open the serial port; you will have to wait a little till the Mega's boot loader has finished before you can send the data from the PC.

I would advise to implement a protocol; e.g. send 64 bytes from PC, wait for a reply from the Mega, send the next 64 bytes, wait for reply and so on. You can expand the protocol (e.g if the image size is not a multiple of 64 bytes you will need a length indication to tell the Mega how many bytes to expect).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

ehsanzamani

I tried this way
I sent the data to 5 bytes and then printed the result on the page again the data was not fully sent and read about 2 bytes and 2 bytes and then no other data was sent and the serial connection was disconnected

sterretje

When you modify things, you should post your updated code. And this time, please use code tags as described in e.g. https://forum.arduino.cc/index.php?topic=148850.0.

We will also need to see your C# code that does the setup if the serial port and the transfer of the data.

There is a more suitable section for this on the forum, I will ask a moderator to move it.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

ieee488

I tried this way
I sent the data to 5 bytes and then printed the result on the page again the data was not fully sent and read about 2 bytes and 2 bytes and then no other data was sent and the serial connection was disconnected
printed result to what page ?

Is this on the C# code or the Arduino code?

sterretje

To my knowledge the Mega resets when you open the serial port; you will have to wait a little till the Mega's boot loader has finished before you can send the data from the PC.
Just checked and if the C# port settings that you provided are the only ones that you set, the board will not reset.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

sterretje

Below (over two posts) example code for Arduino and example code for a C# console application that demonstrates the idea of a protocol. I've mostly concentrated on the C# side of things; the Arduino code was quickly hacked together.

Arduino
Code: [Select]
byte buffer[4096];
byte chunkSize = 32;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(57600);
}

void loop()
{
  receiveConfirmEcho();
}


/*
  receive data from PC
  send a message back every chunkSize (32) bytes
  when all data is received, echo the data back to PC
*/
void receiveConfirmEcho()
{
  static uint16_t index;
  static uint32_t lastUpdateTime;

  // read data if available
  while (Serial.available() > 0)
  {
    // update the time that a byte was received
    lastUpdateTime = millis();
    // save in buffer
    buffer[index++] = Serial.read();
    // confirm
    if (index % chunkSize == 0)
    {
      Serial.println(index);
    }
  }

  // on time out, echo the received data back
  if (index != 0 && (millis() - lastUpdateTime > 5000))
  {
    for (uint16_t cnt = 0; cnt < 4096; cnt += chunkSize)
    {
      Serial.write(&buffer[cnt], chunkSize);
      delay(200);
    }

    // reset variables
    index = 0;
    memset(buffer, 0, sizeof(buffer));
  }
}
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

sterretje

C# console application
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO.Ports;


namespace ArduinoProtocol
{
    class Program
    {

        static SerialPort _serialPort = new SerialPort();

        static int bufferSize = 4096;
        static byte chunkSize = 32;

        static byte[] dataToSend = new byte[bufferSize];
        static byte[] receiveBuffer = new byte[bufferSize];

        static Random random = new Random((int)DateTime.Now.Ticks);


        static void Main(string[] args)
        {
            // create some data
            for (int cnt = 0; cnt < dataToSend.Length; cnt++)
            {
                //dataToSend[cnt] = (byte)(cnt % 0x100);
                dataToSend[cnt] = (byte)random.Next(256);
            }

            Console.WriteLine("Opening port");
            if (connect("COM19", false) == false)
            {
                return;
            }

            if (_serialPort.IsOpen == true)
            {
                Console.WriteLine("Removing outstanding data");
                while (_serialPort.BytesToRead != 0)
                {
                    _serialPort.Read(receiveBuffer, 0, bufferSize);
                }

                Console.WriteLine("Sending data");
                if (sendData() == false)
                {
                    return;
                }
                Console.WriteLine("Receiving data back");
                if (receiveData() == false)
                {
                    return;
                }
                Console.WriteLine("Comparing data");
                compareData();

                _serialPort.Close();
            }
        }


        /// <summary>
        /// Open serial port
        /// </summary>
        /// <param name="portName">COMxx</param>
        /// <param name="reset">reset arduino if true</param>
        /// <returns></returns>
        static bool connect(string portName, bool reset)
        {
            try
            {
                _serialPort.PortName = portName;
                _serialPort.BaudRate = 57600;
                _serialPort.DataBits = 8;
                _serialPort.Parity = Parity.None;
                _serialPort.StopBits = StopBits.One;
                _serialPort.Handshake = Handshake.None;
                _serialPort.ReadTimeout = 2000;
                _serialPort.DtrEnable = reset;

                _serialPort.Open();
                Console.WriteLine("{0} opened", portName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }

            return true;
        }

        /// <summary>
        /// send data in chuncks of 32 bytes; wait for confirmation after 32 bytes
        /// </summary>
        /// <returns>false on error, else true</returns>
        static bool sendData()
        {
            for (int offset = 0; offset < dataToSend.Length; offset += chunkSize)
            {
                _serialPort.Write(dataToSend, offset, chunkSize);

                try
                {
                    string message = _serialPort.ReadLine();
                    Console.WriteLine(message);
                }
                catch (TimeoutException tex)
                {
                    Console.WriteLine(tex.Message);
                    return false;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// receive data back from arduino
        /// </summary>
        /// <returns>false on error, else true</returns>
        static bool receiveData()
        {
            int numBytes = 0;
            int numTries = 5;
            while (numBytes < 4096)
            {
                try
                {
                    int n = _serialPort.Read(receiveBuffer, numBytes, bufferSize - numBytes);
                    numBytes += n;
                    Console.WriteLine("numBytes = {0}", numBytes);
                }
                catch (TimeoutException tex)
                {
                    Console.WriteLine(tex.Message);
                    numTries--;
                    if (numTries == 0)
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// compare outgoing and incoming data
        /// </summary>
        /// <returns>returns true if match, else false</returns>
        static bool compareData()
        {
            if (receiveBuffer.SequenceEqual(dataToSend) == true)
            {
                Console.WriteLine("Match");
                return true;
            }
            else
            {
                Console.WriteLine("No match");
                return false;
            }
        }
    }
}
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

ehsanzamani

Thanks, I'm testing and announced the result here

09ifes

If you're just trying to send the bytes of a BMP file from a computer to the arduino, all you need is a software like 'realterm' that literally sends the data byte by byte to your microcontroller via serial connection, there's literally know need for java or c# coding on a PC application, people like to overcomplicate things.

Go Up