none
串列接收圖像資料 RRS feed

  • 問題

  • 以下是我用串列埠接收影像的程式, 在傳送端方面我有幾張圖要傳,其傳送的資料形式是以前面4個byte為圖像長度之後才是圖像資料,先傳送第一張圖的時候,接收端可以正常顯示,當我要在傳送第二張圖的時候就不行了(蓋過前一張圖),是不是我程式哪裡有有問題呢? 還是哪裡資料沒完全釋放導致呢? 如果說第一張可以的話第二張也應該要可以才對阿甚至是更多張!  請高手不吝解答一下吧!!   謝謝! 

     

     

    namespace getmap
    {
        public partial class Form1 : Form
        {
            private MemoryStream m = new MemoryStream();
            private SerialPort COM3 = new SerialPort();
            private string error = null;
            public int iByteComing;
            public Form1()
            {
                 
                InitializeComponent();
                COM3.DataReceived += new SerialDataReceivedEventHandler(COM3_dataReceived);   //  接收長度事件
                COM3.DataReceived += new SerialDataReceivedEventHandler(COM3_DataReceived);  //  接收圖像資料事件
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                COM3.PortName = "COM3";
                COM3.BaudRate = 38400;
                COM3.Parity = Parity.None;
                COM3.StopBits = StopBits.One;
            }

            private void COM3_dataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                int iByteRead;
                byte[] byLength = new byte[4];
                iByteRead = COM3.Read(byLength, 0, 4);
                if (iByteRead != 4)
                {
                    this.textBox1.Text = "no length from sender";
                }
                iByteComing = Decodeimagelength(byLength);    //  image Length
            }

            private void COM3_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                int ioffset = 0;
                int iByteRead;
                byte[] byimage = new byte[1000];
                MemoryStream m = new MemoryStream(byimage);
                if (iByteComing > byimage.Length)
                {
                    byimage = new byte[iByteComing];
                    m = new MemoryStream(byimage);
                }
                else { m.Position = 0; }
                do{
                   iByteRead = COM3.Read(byimage,ioffset,iByteComing - ioffset);
                    if(iByteRead !=0)
                    {
                      ioffset += iByteRead;
                    }
                  }while(ioffset != iByteComing);
                  if (true)
                  {
                      pictureBox1.Image = Image.FromStream(m);
                  }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                COM3.Open();
            }

            public int Decodeimagelength(byte[] bufferx)
            {
                int datalength = 0;
                try
                {   //image's length
                    byte[] buffer1 = new byte[4];
                    int iix = 0;
                    while (iix < 4)
                    {
                        buffer1.SetValue(bufferx.GetValue(iix), iix);
                        iix += 1;
                    }
                    datalength = (int)BitConverter.ToInt32(buffer1, 0);
                }
                catch (Exception ex)
                {
                    this.error = "Error in response data length." + ex.Message.ToString();
                    return 0;
                }
                return datalength;
            }
        }
    }

    2008年3月12日 下午 01:38