解决方案 »

  1.   

    textbox1初始赋值、调用上面的方法到再次查看textbo1值的代码贴出来。如果仅仅是你上面的描述,没理由不改变textbox1的值
      

  2.   

    我是在串口接收下位机发送的数据后,进入这个自定义的函数。
    textbox1我没有进行初始赋值,调用此方法的代码如下:调用方法语句为红色字体处
                    SerialPort serialPort = (SerialPort)serialPortobj;
                    try
                    {
                        int lenght1 = serialPort.BytesToRead;//获取缓冲区中数据的字节数
                        if (lenght1 != 0 && lenght1 != null)
                        {
                            threadReceive.Join(10);        //加入线程:threadReceive
                            serialPort.ReadTimeout = 100000;
                            //阻塞到读取数据或超时(这里为2秒)
                            bytesRead = serialPort.BytesToRead;
                            byte[] bytesData = new byte[bytesRead];
                            string Sdate = "";
                            for (int i = 0; i < bytesRead; i++)   //接收下位机发送的全部数据,共bytesRead个字节
                            {
                                
                                if (checkHex.Checked)    //接收的数据显示为16进制
                                {
                                    bytesData[i] = Convert.ToByte(serialPort.ReadByte());
                                    strReceive += Convert.ToString(bytesData[i], 16).PadLeft(2, '0').PadRight(3, ' ').ToUpper();
                                    Sdate += Convert.ToString(bytesData[i], 16).PadLeft(2, '0').PadRight(3, ' ').ToUpper();  
                                }
                                else                     //接收的数据显示为字符
                                {
                                    bytesData[i] = Convert.ToByte(serialPort.ReadByte()); 
                                    strReceive += Convert.ToChar(bytesData[i]);//将接收到的数据转成字符,并转化为字符串 strReceive。 
                                    Sdate += Convert.ToChar(bytesData[i]);                     
                                }
                                  
                            } 
                            //------------------ 译码-----------------//
                            CommandDecoder(bytesData);
                                
                            s1 = strReceive;
                            textRecive.BeginInvoke(new InvokeDelegate(Display));  // 在接收框中显示接收到的数据
                        }
                        else
                        {
                            threadReceive.Join(10);
                            serialPort.ReadTimeout = 100000;  //获取或设置读取操作未完成时发生超时之前的毫秒数。                    }
                    }                catch
                    {
                    }
                }
            }
      

  3.   

    不好意思,少贴了一段,CommandDecoder(bytesData);没有贴出来,如下:
            public void CommandDecoder(byte[] receiveData)
            {            command_high_byte = receiveData[4];
                command_low_byte = receiveData[5];
                switch (command_high_byte)
                { 
                    case SYS:
                        SYS_command_decoder();
                        break;
                    default:
                        break;
                }
                
            }
      

  4.   


                       try
                        {                        
                              textBox1.Text = "55AA";                                            
                        }
                        catch
                        {
                        }在catch的地方加上日志
    或者把try...catch取掉,是不是出错了?
      

  5.   

    是不是还没走到红色字体代码,就catch
                     {
                     }了?
      

  6.   

    1. 既然调试到了textBox1.Text = "55AA"; 处,那就再F10试试能不能成功执行。
    2. Ctrl+K,R 查看下textBox1.Text的所有引用地方,如果1执行成功,没理由值不改变。只可能是你另外的地方又把值给改回来了。
    3. 你查看的textBox1并不是这里赋值的textBox1,所以看起来值没有赋成功。
      

  7.   

    threadReceive.Join(10);        //加入线程:threadReceive
    你这注释根本就不对,所以严重怀疑你根本不知道Join方法是干什么用的
    而且也没看到你线程到底在哪里还是先写伪代码,写明白你整个的逻辑流程,然后我们来分析你到底哪里出现了问题
      

  8.   

    还有,既然你直接给textbox赋值了textRecive.BeginInvoke(new InvokeDelegate(Display));又是干什么用的,为什么调用Invoke之前就给textbox赋值了??
      

  9.   

    谢谢大家的回复。我是刚接触C#,这个程序的框架是在网站上找的,为了做一个上位机界面,通过UART接口和我的下位机通信。    xian_wwq问的问题确实是发生了,如果把try..catch..去掉,程序就出错了,直接跳到了CommandDecoder(bytesData);   语句下面那个catch里了。
    第二个问题,textRecive.BeginInvoke(new InvokeDelegate(Display));这个语句我也不太明白,不过它的效果是调用Display方法,显示下位机发送的数据,可以先不管它。
      

  10.   

    C#中自定义函数修改TextBox值示例
    //修改TextBox值按钮事件
      private datatier dt = new datatier();//定义datatier类型的私有字段
    private void btn_update_Click(object sender, EventArgs e)
            {
                //调用datatier对象的Update()方法,更改数据库中的信息
                dt.Update(new Instance() { Name = txt_name_update.Text, phone = txt_phone_update.Text });
                dataGridView1.DataSource = dt.Select();//更新dataGridView1控件中的信息
                Clear();//清空TextBox控件中的文本
            }
    //调用自定义函数
     class datatier
        {
            private SqlConnection GetConnection()
            {
                //返回连接到数据库的SqlConnection对象
                return new SqlConnection("server=WRET-MOSY3755HGW\\MRGLL;database=db_test;Trusted_Connection=true");
            }
      public void Update(Instance it)
            {
                SqlConnection sc = GetConnection();//调用GetConnections()方法,得到连接对象
                try
                {
                    sc.Open();//打开到数据库的连接
                    SqlCommand cmd = new SqlCommand(//创建SqlCommand对象
                        "update tb_friend set phone=@phone where names=@names", sc);
                    cmd.Parameters.Add("@names", SqlDbType.VarChar).Value = it.Name;//向SqlCommand对象添加参数
                    cmd.Parameters.Add("phone", SqlDbType.VarChar).Value = it.phone;//向SqlCommand对象添加参数
                    cmd.ExecuteNonQuery();//执行SqlCommand对象中的SQL命令
                }//codego.net/tags/1/1/
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (sc.State == ConnectionState.Open)//判断是否连接数据库
                    {
                        sc.Close();//如果已经连接则关闭连接
                    }
                }
            }
    }