Ich möchte über die Serielle Schnittstelle JPEG Files übertragen (in C#)

Allgemeine Diskussion Ich möchte über die Serielle Schnittstelle JPEG Files übertragen (in C#)

  • Mittwoch, 16. Mai 2012 06:00
     
      Enthält Code

    Ich habe das Problem, dass ich auf der Empfangsseite nicht vollständig bekomme. Nach ungefähr 12 kBytes hört die Übertragung auf. Was mache ich da nur falsch?

    Sendeseite:

     using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      using System.Linq;
      using System.IO;
    
    
    namespace SimpleSerial
    {
        public partial class Form1 : Form
        {
            // Add this variable
    
            string RxString;
            byte[] b;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void buttonStart_Click(object sender, EventArgs e)
            {
                serialPort1.PortName = "COM9";
                serialPort1.BaudRate = 57600;
    
                serialPort1.Open();
                if (serialPort1.IsOpen)
                {
                    buttonStart.Enabled = false;
                    buttonStop.Enabled = true;
                    textBox1.ReadOnly = false;
                }
            }
    
            private void buttonStop_Click(object sender, EventArgs e)
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                    buttonStart.Enabled = true;
                    buttonStop.Enabled = false;
                    textBox1.ReadOnly = false;
                }
    
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (serialPort1.IsOpen) serialPort1.Close();
            }
    
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                // If the port is closed, don't try to send a character.
    
                if (!serialPort1.IsOpen) return;
    
                // If the port is Open, declare a char[] array with one element.
                char[] buff = new char[1];
    
                // Load element 0 with the key character.
    
                buff[0] = e.KeyChar;
    
                // Send the one character buffer.
                serialPort1.Write(buff, 0, 1);
    
                // Set the KeyPress event as handled so the character won't
                // display locally. If you want it to display, omit the next line.
                e.Handled = true;
            }
    
            private void DisplayText(object sender, EventArgs e)
            {
                
                textBox1.AppendText(RxString);
            }
    
            private void serialPort1_DataReceived
              (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                RxString = serialPort1.ReadExisting();
                //int y = serialPort1.BytesToWrite;
                this.Invoke(new EventHandler(DisplayText));
            }
    
            private string ByteArrayToString(byte[] arr)
            {
                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                return enc.GetString(arr);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
                string path = @"C:\Users\ipa\Desktop\adidas.jpg";
    
               
    
                           //Open the stream and read it back.
                using (FileStream fs = File.OpenRead(path))
                {
                    
                    Int64 f = fs.Length;
                    byte[] b = new byte[f];
                                   
                    decimal pakete = f/138;
                    decimal paket = Math.Round(pakete);
                    fs.Read(b, 0, b.Length);
                    Int32 offset=0;
    
    
                    for (int a = 0; a <= paket-1; a++)
                    {
                        serialPort1.Write(b,offset,138);
                        System.Threading.Thread.Sleep(99);
                        offset = offset + 138 ;
                        
                            
                    }serialPort1.Close();
                }
                    
                
            }
    
            private static void AddText(FileStream fs, string value)
            {
                byte[] info = new UTF8Encoding(true).GetBytes(value);
                fs.Write(info, 0, info.Length);
            }
    
    
    
            public char v { get; set; }
    
        }
    
    
    
    }

    Empfangsseite:

     
    using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
     
      namespace SimpleSerial
      {
          public partial class Form1 : Form
          {
              // Add this variable
    
              string RxString;
              byte[] buff2 = new byte[98152];
              int offset = 0;
              int x;
              
              public Form1()
              {
                  InitializeComponent();
              }
     
              private void buttonStart_Click(object sender, EventArgs e)
              {
                  serialPort1.PortName = "COM9";
                  serialPort1.BaudRate = 57600;
     
                  serialPort1.Open();
                  if (serialPort1.IsOpen)
                  {
                      buttonStart.Enabled = false;
                      buttonStop.Enabled = true;
                      textBox1.ReadOnly = false;
                  }
              }
     
              private void buttonStop_Click(object sender, EventArgs e)
              {
                  if (serialPort1.IsOpen)
                  {
                      serialPort1.Close();
                      buttonStart.Enabled = true;
                      buttonStop.Enabled = false;
                      textBox1.ReadOnly = false;
                  }
     
              }
     
              private void Form1_FormClosing(object sender, FormClosingEventArgs e)
              {
                  if (serialPort1.IsOpen) serialPort1.Close();
              }
     
              private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
              {
                  // If the port is closed, don't try to send a character.
    
                  if(!serialPort1.IsOpen) return;
     
                  // If the port is Open, declare a char[] array with one element.
                  char[] buff = new char[1];
                  
                  // Load element 0 with the key character.
    
                  buff[0] = e.KeyChar;
     
                  // Send the one character buffer.
                  serialPort1.Write(buff, 0, 1);
     
                  // Set the KeyPress event as handled so the character won't
                  // display locally. If you want it to display, omit the next line.
                  e.Handled = true;
              }
    
              private string ByteArrayToString(byte[] arr)
              {
                  System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                  return enc.GetString(arr);
              }
    
              private void DisplayText(object sender, EventArgs e)
              {
                  RxString = ByteArrayToString(buff2);
                  textBox1.AppendText(RxString);
              }
    
              private void CreateFile(byte[] file)
              {
                  string path = @"C:\Users\ipa\Desktop\testbilder\test2.jpg";
                  if (!System.IO.File.Exists(path) || System.IO.File.Exists(path))
                  {
                      using (System.IO.FileStream fs = System.IO.File.Create(path))
                      {
    
                          fs.Write(buff2, 0, buff2.Length);
                          
                      }
                  }
                  
              }
    
    
              private void serialPort1_DataReceived
                (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
              {
                  
                  do
                  {
                      x = serialPort1.Read(buff2, offset, serialPort1.BytesToRead);
                      serialPort1.DiscardInBuffer();
                      offset = offset + x;
                      
                  } while (offset <= 30100);
                          
                  CreateFile(buff2);
                          
                      
                   
                  //buff.CopyTo(buff2, offset);
                  //offset = offset + x;
                  //CreateFile(buff2);      
                        
                            
                            //serialPort1.Close();
              }
          }
      }

     




Alle Antworten