Results 1 to 13 of 13

Thread: [RESOLVED] Corrupted data from arduino on my app

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    9

    Resolved [RESOLVED] Corrupted data from arduino on my app

    Hello guys, i have a CAM module ov7670 (arduino) which makes this:
    1- converts the image taken to bytes and saves on SD card ... working... 38kb file size, .yuv format.
    2- converts the image taken to bytes and I receive on a richtextbox on my VS2010 app...not working 20~50KB filesize.
    My app: A combobox which show all available Serial Ports., a RichTextBox which shows the received data and a button to save the content into a .YUV file.

    Code:
    If (comPORT <> "") Then
                    SerialPort1.Close()
                    SerialPort1.PortName = comPORT
                    SerialPort1.BaudRate = 9600
                    SerialPort1.DataBits = 8
                    SerialPort1.Parity = Parity.None
                    SerialPort1.StopBits = StopBits.One
                    SerialPort1.Handshake = Handshake.None
                    SerialPort1.Encoding = System.Text.Encoding.Default
                    SerialPort1.ReadTimeout = 10000
    
                    SerialPort1.Open()
                    Button1.Text = "Desconectar"
                    Timer1.Enabled = True
                    ComboBox1.Enabled = False
    Code:
     Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
            receivedData = ReceiveSerialData()
            richtextbox1.Text &= receivedData
        End Sub
    
    
        Function ReceiveSerialData() As String
            Dim Incoming As String
            Try
                Incoming = SerialPort1.ReadExisting()
                If Incoming Is Nothing Then
                    Return "nothing" & vbCrLf
                Else
                    Return Incoming
                End If
            Catch ex As TimeoutException
                Return "Error: Serial Port read timed out."
            End Try
    
        End Function
    On SD card the file have exact 38kb (correct size) but on my richtextbox i receive between 20kb~50kb but never 38kb :/
    What i can do?

  2. #2
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: Corrupted data from arduino on my app

    Try this example and see if it will capture your incoming data. In the example there is one Form containing one RichTextBox

    I did not show the serial port configuration so that is left for you to do, just make sure the port is open before the Arduino transmits.

    Code:
      Public Delegate Sub myDelegate(ByVal sData As Byte)
    
        Private Sub Text_Out(ByVal sData As Byte)
    
            RichTextBox1.AppendText(sData.ToString & " ")
           
        End Sub
    
     Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim sData As Byte
            SerialPort1.ReadTimeout = 20
          
            Do While SerialPort1.BytesToRead > 0
                Try
                    sData = CByte(SerialPort1.ReadByte)
                    Me.BeginInvoke((New myDelegate(AddressOf Text_Out)), sData)
    
                Catch ex As Exception
    
                End Try
            Loop
        End Sub

  3. #3
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: Corrupted data from arduino on my app

    What I didn't mention was that the example above will receive the transmitted bytes and display those bytes as a byte value which is a value between 0 and 255. I am thinking that the byte value is what you require yet your example is trying to capture and display an ASCII string of characters. If I am wrong the conversion from byte value to ASCII character is an easy process, so providing the example captures all the byte values then all will be good.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    9

    Re: Corrupted data from arduino on my app

    Hello, i tried this and results numbers...ok, but i treid to converty byte into ANSI (System.text.encoding.default) and returns a error, "value of type 1-dimensional array of byte cannot be converted to string". Can you help with decoding?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Corrupted data from arduino on my app

    Quote Originally Posted by arcyde View Post
    i treid to converty byte into ANSI (System.text.encoding.default) and returns a error
    Then you did it wrong. If you show us what you did, we would have a much better chance of showing you what's wrong with it.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Corrupted data from arduino on my app

    If the arduino is sending an image WHY are you putting the received data in a string???? Try it this way(you'll need to get rid of the timer code)

    Code:
        Private buffer As New List(Of Byte)
        Private Sub SerialPort1_DataReceived(sender As Object,
                                             e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    
            Dim BytesRead As Integer = 0
    
            Dim numberOfBytesToRead As Integer = SerialPort1.BytesToRead
            ' Create a byte array large enough to hold the bytes to be read.
            Dim newReceivedData(numberOfBytesToRead - 1) As Byte
            ' Read the bytes into the byte array.
            BytesRead = SerialPort1.Read(newReceivedData, 0, numberOfBytesToRead)
            If BytesRead > 0 Then
                If BytesRead <> numberOfBytesToRead Then
                    Array.Resize(newReceivedData, BytesRead)
                End If
                buffer.AddRange(newReceivedData) 'accumulate in buffer
            End If
        End Sub
    You'll need to figure out when the arduino is done sending, but once you do the entire image should be in 'buffer'
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Corrupted data from arduino on my app

    be aware that the DataReceived event runs in a different thread than the rest of your code. so to stick with dbase example, make sure you synchronize access to the "buffer" variable otherwise ugly things will happen sooner or later.

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    9

    Re: Corrupted data from arduino on my app

    I'll try this last code, but is a very simple app, https://imgur.com/a/y74Hwhf I send only one command and i receive the image data, the .yuv file need to have 38kb. And i receive only 1 image per command.
    Last edited by arcyde; May 18th, 2018 at 01:51 PM.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    9

    Re: Corrupted data from arduino on my app

    This is the full first code:

    Code:
    Public Class Form1
        Dim comPORT As String
        Dim receivedData As String = ""
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Enabled = False
            comPORT = ""
            For Each sp As String In My.Computer.Ports.SerialPortNames
                ComboBox1.Items.Add(sp)
            Next
        End Sub
    
    
        Private Sub comPort_ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            If (ComboBox1.SelectedItem <> "") Then
                comPORT = ComboBox1.SelectedItem
            End If
        End Sub
    
    
        Private Sub connect_BTN_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            If (comPORT <> "") Then
                    SerialPort1.Close()
                    SerialPort1.PortName = comPORT
                    SerialPort1.BaudRate = 9600
                    SerialPort1.DataBits = 8
                    SerialPort1.Parity = Parity.None
                    SerialPort1.StopBits = StopBits.One
                    SerialPort1.Handshake = Handshake.None
                    SerialPort1.Encoding = System.Text.Encoding.Default
                    SerialPort1.ReadTimeout = 10000
    
                    SerialPort1.Open()
                    Button1.Text = "Desconectar"
                    Timer1.Enabled = True
                    ComboBox1.Enabled = False
    Else
                    MsgBox("Select a COM port first")
                End If
            Else
                SerialPort1.Close()
                Button1.Text = "Conectar"
                timer1.enabled=false
            End If
    end sub
    
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
            receivedData = ReceiveSerialData()
            richtextbox1.Text &= receivedData
        End Sub
    
    
        Function ReceiveSerialData() As String
            Dim Incoming As String
            Try
                Incoming = SerialPort1.ReadExisting()
                If Incoming Is Nothing Then
                    Return "nothing" & vbCrLf
                Else
                    Return Incoming
                End If
            Catch ex As TimeoutException
                Return "Error: Serial Port read timed out."
            End Try
    
        End Function
    
    Private Sub clearbtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles clearbtn.Click
            richtextbox1.Text = ""
        End Sub
    
    
        Private Sub sendbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendbtn.Click
            SerialPort1.WriteLine(TextBox1.Text)
        End Sub
    
        
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim imgtxt As String = richtextbox1.Text
            Dim wrttxt As New StreamWriter(Application.StartupPath & "/test.YUV")
            wrttxt.Write(imgtxt)
            wrttxt.Close()
        End Sub
    End Class

  10. #10
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: Corrupted data from arduino on my app

    First we need to clear up that your captured image is not meant to be read as text or a string of ASCII characters, it is 38K byte values between 0 and 255, this kind of file is called a binary file. Your code is wrong for this application so you need to look at my example or the example of dbasnett.

    I modified my example to use a Queue to hold the incoming serial data, there is a place to write in your own folder path ( notice the correct syntax is "\test.YUV" and not "/test.YUV" ) and finally I added a button with the code to write the data in the Queue to an array and then write a binary file to disk. For the example you must wait until you have received all data before trying to write to disk.

    Code:
      Dim mypath = My.Computer.FileSystem.CombinePath(Application.StartupPath, "\test.YUV")
        Dim myq As Queue(Of Byte) = New Queue(Of Byte)
    
        Public Delegate Sub myDelegate(ByVal sData As Byte)
    
        Private Sub Text_Out(ByVal sData As Byte)
            RichTextBox1.AppendText(sData.ToString & " ")
            myq.Enqueue(sData)
        End Sub
    
        Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim sData As Byte
            SerialPort1.ReadTimeout = 20
    
            Do While SerialPort1.BytesToRead > 0
                Try
                    sData = CByte(SerialPort1.ReadByte)
                    Me.BeginInvoke((New myDelegate(AddressOf Text_Out)), sData)
    
                Catch ex As Exception
    
                End Try
            Loop
        End Sub
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            Dim mydata(myq.Count - 1) As Byte
            myq.CopyTo(mydata, 0)
            My.Computer.FileSystem.WriteAllBytes(mypath, mydata, False)
    
    
        End Sub

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    9

    Re: Corrupted data from arduino on my app

    Unbelievable, perfect hahaha worked 100%, the only problem now is the foccus on camera(just turn left-right), and the app design. Ty so much, all. I will change to resolved. ^.^

  12. #12
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: [RESOLVED] Corrupted data from arduino on my app

    Remember in your final application you will need to clear the contents of the Queue between each capture or write directly to an array and skip the Queue alltogether.

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2018
    Posts
    9

    Re: [RESOLVED] Corrupted data from arduino on my app

    Yeah, its working good now, ty so much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width