public static string ToHexString(byte[] bytes)
        {
            char[] chars = new char[bytes.Length * 3];
            for (int i = 0; i < bytes.Length; i++)
            {
                int b = bytes[i];
                chars[i * 3] = hexDigits[b >> 4];
                chars[i * 3 + 1] = hexDigits[b & 0xF];
                chars[i * 3 + 2] = ' ';
            }
            return new String(chars);
        }
        private void mycomm_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            int length = mycomm.BytesToRead;
            byte[] data = new byte[length];
            for (int i = 0; i < length; i++)
            {
                data[i] = (byte)mycomm.ReadByte();
            }            string str =(string ) ToHexString(data);
            aaa.Text = str;
            
        }
            
        }
           
为什么我的最后一个(tsend.Text = str;)提示有错啊 郁闷啊

解决方案 »

  1.   

    tsend 是什么,aaa 又是什么?
      

  2.   


    将aaa.Text = str;改为:Invoke(New EventHandler(AddressOf GetData))
    '自己转成C#:Invoke(New EventHandler(GetData));Public Sub GetData(ByVal sender As System.Object, ByVal e As System.EventArgs)
      aaa.Text = str;
    End Sub
      

  3.   


       public static string ToHexString(byte[] bytes)
            {
                char[] chars = new char[bytes.Length * 3];
                for (int i = 0; i < bytes.Length; i++)
                {
                    int b = bytes[i];
                    chars[i * 3] = hexDigits[b >> 4];
                    chars[i * 3 + 1] = hexDigits[b & 0xF];
                    chars[i * 3 + 2] = ' ';
                }
                return new String(chars);
            }
            private void mycomm_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                int length = mycomm.BytesToRead;
                byte[] data = new byte[length];
                for (int i = 0; i < length; i++)
                {
                    data[i] = (byte)mycomm.ReadByte();
                }            string str =(string ) ToHexString(data);
                aaa.Text = str;
                
            }
                
            }不好意思写错了 是aaa.Text = str;
    这个地方提示错误
      

  4.   

    这里要用互斥对象,,,给你一段代码参考void Form1_OnStringDataAccept(byte[] btRcv) //自定事件
            {
                string strRcv = "";//接收字符
                foreach (byte btEachRcv in btRcv)
                    strRcv += Convert.ToChar(btEachRcv);            if (txtRcv.InvokeRequired)
                {
                    CallBackRef cbr = new CallBackRef(ShowTextCallBack);
                    try
                    {
                        this.Invoke(cbr, new object[] { strRcv });
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                }
                else
                {
                    txtRcv.Text +=  strRcv ;
                    TransactRtnCode(strRcv);            }