本帖最后由 lideyu1205 于 2009-08-11 09:01:23 编辑

解决方案 »

  1.   

    不会把你的代码贴出来。
    你先加个断点调试一下,在你发送完后,串口确定有读到数据还有就是你串口组件的ReceivedBytesThreshold的值设置一下。
     ReceivedBytesThreshold是读取缓冲区有多少个字节时触发DataReceived事件。
      

  2.   

    手动调用DataReceived(object sender,SerialDataReceivedEventArgs e)方法 
    读取事件,不要手工调用,当缓冲区有数据时,并且等于或大于ReceivedBytesThreshold值时会自动触发。你事件委托加了没有。            this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO.Ports;
    using System.Text.RegularExpressions;
    using System.Data.OleDb;
    using System.Threading;namespace RS232
    {
        public partial class fclsRS232Tester : Form
        {
            
            string InputData = String.Empty;
            bool Head_En_Mo = false;
            int Jie_Count_Mo = 0;
            int Shou_Count_Mo = 0;
            bool Lenth_En_Mo = false;
            byte temp = 0;
            bool Enable_Mo = false;
            byte[] ssuf = new byte[2024];
            delegate void SetTextCallback(string text);
            string ds = "";
            int typeid = 0;
            int lcnum = 0;        List<Cjb> cjblist = new List<Cjb>();
            //List<string> f2 = null;//new List<string>();//回应 ”获得全部采集板设备标号命令的“  命令集合
            //List<string> f002 = null;//new List<string>();//回应 “根据一个采集板设备标号获得它下的检测板设备标号命令” 的命令集合
            //List<string> f8 = null;//new List<string>();//来车信息的命令集合
            //List<string> ff = null;//new List<string>();//其他命令结合        
            List<string> f2 = new List<string>();//回应 ”获得全部采集板设备标号命令的“  命令集合
            List<string> f002 = new List<string>();//回应 “根据一个采集板设备标号获得它下的检测板设备标号命令” 的命令集合
            List<string> f8 = new List<string>();//来车信息的命令集合
            List<string> f1 = new List<string>();//配置通信板命令的 返回结果
            List<string> e0 = new List<string>();//配置采集板命令的 返回结果
            List<string> c0 = new List<string>();//配置检测板命令的 返回结果
            List<string> f001 = new List<string>();//检测板启动计算机环境参数命令        string strconn = "provider=microsoft.jet.oledb.4.0;data source=c:\\only.mdb";
            public fclsRS232Tester()
            {
                InitializeComponent();
            }
     
            //清除按钮
            private void btnClear_Click(object sender, EventArgs e)
            {
                ds = "";
                //txtIn.Clear();
            }
                    #region  当缓存中有数据的时候  自动调用的方法
          
            private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
            {
               
                string datanum = port.BytesToRead.ToString();//缓冲区中的字节数
                byte[] yuan_byte = new byte[Convert.ToInt32(datanum)];
                for (int i = 0; i < Convert.ToInt16(datanum); i++)
                {               
                   temp =Convert.ToByte(port.ReadByte());
                   yuan_byte[i] = temp;
                   if(Head_En_Mo) 
               {
                       goto Can_ReMo;
                        //if(Lenth_En_Mo)  goto Can_ReMo;
                        //else 
                        //{   
                        //  if(temp==0x0ff)  goto int1exit;
                        //  else goto Can_ReMo;
                        //}
               }
                   //if(temp!=0xff) { 
                   //    Jie_Count_Mo=0; goto int1exit; 
                   //}
                   if(temp==0xaa){
                       if (i == 0)
                       {
                           Jie_Count_Mo++;
                       }
                       else {
                           Jie_Count_Mo++;
                       }
                   }
                   if(temp==0x55){
                       if (i == 0)
                       {
                           Jie_Count_Mo = 0;
                       }
                       else {
                           if (yuan_byte[i - 1] == 0xaa)
                           {
                               Jie_Count_Mo++;
                           }
                           else {
                               Jie_Count_Mo = 0;
                           }
                       }
                   }
                   if (Jie_Count_Mo == 2)
                   {
                       Head_En_Mo = true;
                       Jie_Count_Mo = 0;
                       Shou_Count_Mo = 0;
                       Lenth_En_Mo = false;
                       goto int1exit;
                   }
                   //if((++Jie_Count_Mo)==5) 
                   //{ 
                   //     Head_En_Mo=true; 
                   //     Jie_Count_Mo=0; 
                   //     Shou_Count_Mo=0; 
                   //     Lenth_En_Mo=false; 
                   //     goto int1exit; 
                   //}
                   else goto int1exit;
        Can_ReMo:  ssuf[Jie_Count_Mo++]=temp; 
                   if(Jie_Count_Mo>320) 
                   {   
                    Head_En_Mo=false; 
                Jie_Count_Mo=0; 
                Shou_Count_Mo=0; 
                Lenth_En_Mo=false; 
                goto int1exit;
                }
                    if(Lenth_En_Mo)
                        if(Jie_Count_Mo==Shou_Count_Mo) 
                        { 
                            Enable_Mo=true; goto int1exit; 
                        }
                    if(Jie_Count_Mo==3) 
                    {
                        Shou_Count_Mo = ssuf[1]<<8;
                        Shou_Count_Mo += ssuf[2];
                        Shou_Count_Mo = Shou_Count_Mo + 4;
                       if (Shou_Count_Mo > 320)
                       {
                           Head_En_Mo = false;
                           Jie_Count_Mo = 0;
                           Shou_Count_Mo = 0;
                           Lenth_En_Mo = false;
                           goto int1exit;
                       }
                    }
                else
                {
                       Lenth_En_Mo=true; 
                           goto int1exit;
                   }
         int1exit:  if (Enable_Mo)
                    {
                        Enable_Mo = false;
                        Head_En_Mo = false;
                        Jie_Count_Mo = 0;
                        Shou_Count_Mo = 0;
                        Lenth_En_Mo = false;
                        int num = 0;
                        num = ssuf[1] << 8;
                        num += ssuf[2];  //数据码长度  +3  就是除去头以外的所有数据
                        string dsss = "";
                        for (int ni = 0; ni < num+4;ni++ )
                        {
                            if (ssuf[ni].ToString("X").Length == 1)
                            {
                                dsss += "0" + ssuf[ni].ToString("X");
                                ds += "0" + ssuf[ni].ToString("X");
                            }
                            else {
                                dsss += ssuf[ni].ToString("X");
                                ds += ssuf[ni].ToString("X");
                            }
                        }
                        string[] d = ConvertStringArray(dsss);
                        
                        OleDbConnection conn = new OleDbConnection(strconn);
                        int cnum = Convert.ToByte(Convert.ToInt32(d[1], 16).ToString("X")) << 8;
                        cnum += Convert.ToByte(Convert.ToInt32(d[2], 16));
                        //int nnnnn = Convert.ToInt32(cnum.ToString(), 16);
                        int nnnnn = cnum + 4;   //数据码长度  +3  就是除去头以外的所有数据
                        string su = "ff";
                        for (int ini = 0; ini < nnnnn - 1; ini++)
                        {
                            su = this.GetXorSum(su, d[ini]);
                        }
                        if (su.Length == 1)
                        {
                            su = "0" + su;
                        }
                        //string sys = "";
                        //for (int jij = 0; jij < nnnnn; jij++)
                        //{
                        //    sys += d[jij];
                        //}
                        if (su.ToUpper().Equals(d[nnnnn - 1]))
                        {
                            try
                            {
                                conn.Open();
                                OleDbCommand cmd = new OleDbCommand("insert into xy_table(content,trueorfalse) values('" + dsss + "',0)", conn);
                                cmd.ExecuteNonQuery();
                                conn.Close();
                                if (d[0].ToUpper().Equals("F2"))
                                {
                                    f2.Add(dsss);
                                }
                                else if (d[0].ToUpper().Equals("F0") && d[4].ToUpper().Equals("02"))
                                {
                                    f002.Add(dsss);
                                }
                                else if (d[0].ToUpper().Equals("F0") && d[4].ToUpper().Equals("01"))
                                {
                                    f001.Add(dsss);
                                }
                                else if (d[0].ToUpper().Equals("F8"))
                                {
                                    f8.Add(dsss);
                                }
                                else if (d[0].ToUpper().Equals("F1"))
                                {
                                    f1.Add(dsss);
                                }
                                else if (d[0].ToUpper().Equals("E0"))
                                {
                                    e0.Add(dsss);
                                }
                                else if (d[0].ToUpper().Equals("C0")){
                                    c0.Add(dsss);
                                }
                            }
                            catch { }                        
                        }
                        else
                        {
                            try
                            {
                                conn.Open();
                                OleDbCommand cmd = new OleDbCommand("insert into xy_table(content,trueorfalse) values('" + dsss + "',1)", conn);
                                cmd.ExecuteNonQuery();
                                conn.Close();
                            }
                            catch { }                                              
                        }
                     
                    }
                 }
             }
                   #endregion
      

  4.   

            #region  把采集板的 信息 添加到数据库中  并发送命令获得 检测板id 的命令
            private void addCjb() {
                //string strconn = "provider=microsoft.jet.oledb.4.0;data source=c:\\only.mdb";
                OleDbConnection conn = new OleDbConnection(strconn);
                try
                {
                    conn.Open();
                    if (f2.Count > 0)
                    {
                        string cjbstrlist = "";
                        int cjbstrlist1 = 0;
                        //获得f2list中的第一条记录
                        foreach (string str in f2)
                        {
                            if (cjbstrlist1 != 0)
                            {
                                break;
                            }
                            else
                            {
                                cjbstrlist = str;
                            }
                            cjbstrlist1++;
                        }
                        string[] cjbsrtarray12 = getCjbId(cjbstrlist);
                        string[] cjbflsrarray = new string[cjbsrtarray12.Length / 2];//从命令里  获得的采集板的对应的锋联输入标号  数组型的 
                        string[] cjbsrtarray = new string[cjbsrtarray12.Length / 2];//从命令里  获得的采集板的设备标号  数组型的 
                        for (int i = 0; i < cjbsrtarray.Length; i++)
                        {
                            cjbsrtarray[i] = cjbsrtarray12[i * 2];
                            cjbflsrarray[i] = cjbsrtarray12[i*2+1];
                        }
                        string querysql = "select * from cjb_table";
                        OleDbCommand qcmd = new OleDbCommand(querysql, conn);
                        OleDbDataReader qdr = qcmd.ExecuteReader();
                        string datacjbstr = "";  //采集板的设备标号  字符串形式的
                        string datacjbflsrstr = ""; //采集板锋联输入标号 字符串形式的
                        while (qdr.Read())
                        {
                            datacjbstr += qdr.GetString(1);
                            datacjbflsrstr += qdr.GetString(2);
                        }
                        qdr.Close();
                        //datacjbstr.length>0说明数据库中已经有了  不是第一次加入值了 
                        if (datacjbstr.Length > 0)
                        {
                            //cjbsrtarray = new string[2];
                            //cjbsrtarray[0] = "01";
                            //cjbsrtarray[1] = "02";
                            string[] datacjb = ConvertStringArray(datacjbstr);//采集板的设备标号  数组形式的  来源  数据库
                            string[] datacjbflsr = ConvertStringArray(datacjbflsrstr);//采集板的锋联输入标号  数组形式的  来源  数据库
                            string[] myd = getBtsj(datacjb, cjbsrtarray);  //返回数据库中  没有出现在  命令中的 采集板的设备标号    也就是故障的采集板
                            string[] ffmyd = getBtsj(cjbsrtarray, datacjb);  //返回命令中  没有出现在  数据库中的  采集板的设备标号   将他们添加到数据库中
                            if (cjbflsrarray.Length == cjbsrtarray.Length)
                            {
                                
                                for (int ii = 0; ii < cjbflsrarray.Length; ii++)
                                {
                                    int numflsr = 0;
                                    for (int iii = 0; iii < datacjb.Length;iii++ )
                                    {
                                        if (datacjb[iii].ToUpper().Equals(cjbsrtarray[ii].ToUpper()))
                                        {
                                            numflsr = iii;
                                            break;
                                        }
                                    }
                                    OleDbCommand upflsr = new OleDbCommand("insert into fcjb_table(content) values('" + "采集板:" + cjbsrtarray[ii] + "对应的封连标号:" + datacjbflsr[numflsr] + "')", conn);
                                    upflsr.ExecuteNonQuery();
                                    //OleDbDataReader dddr = upflsr.ExecuteReader();
                                    //int jji = 0;
                                    //if(dddr.Read()){
                                    //    jji++;
                                    //}
                                    //dddr.Close();
                                    //if (jji == 0)
                                    //{
                                    upflsr.CommandText = "select * from cjb_table where cjbname = '" + cjbsrtarray[ii] + "'";
                                    OleDbDataReader dddddr = upflsr.ExecuteReader();
                                    int jji = 0;
                                    while(dddddr.Read()){
                                        jji++;
                                    }
                                    dddddr.Close();
                                    if(jji==0){
                                        upflsr.CommandText = "insert into  cjb_table(flsrname,cjbname) values('" + cjbflsrarray[ii] + "' , '" + cjbsrtarray[ii] + "')";
                                        upflsr.ExecuteNonQuery();
                                    }
                                    else {
                                        upflsr.CommandText = "update cjb_table set flsrname='" + cjbflsrarray[ii] + "' where cjbname = '" + cjbsrtarray[ii] + "'";
                                        upflsr.ExecuteNonQuery();
                                    }
                                    
                                    //upflsr.CommandText = "insert into fcjb_table(content) values('" + "采集板:" + cjbsrtarray[ii] + "对应的封连标号:" + datacjbflsr[numflsr] + "')";
                                    //upflsr.ExecuteNonQuery();
                                }
                                
                            }
                            if (ffmyd != null)
                            {
                                for (int fd = 0; fd < ffmyd.Length; fd = fd + 2)
                                {
                                    int numcjb = 0;
                                    for (int ii = 0; ii < cjbsrtarray.Length; ii++)
                                    {
                                        if (cjbsrtarray[ii].ToUpper().Equals(ffmyd[fd].ToUpper()))
                                        {
                                            numcjb = ii;
                                            break;
                                        }                                   
                                    }
                                    string ffmydsql = " insert into cjb_table(cjbname,flsrname) values('" + ffmyd[fd] + "','" + cjbflsrarray[numcjb] + "')";
                                    OleDbCommand ffmydcmd = new OleDbCommand(ffmydsql, conn);
                                    ffmydcmd.ExecuteNonQuery();
                                }
                            }
      

  5.   

                            if (myd != null)
                            {
                                ///存在有故障的采集板         
                                OleDbCommand findalljcb = new OleDbCommand("select * from cjb_table", conn);
                                OleDbDataReader alljcbdr = findalljcb.ExecuteReader();
                                while (alljcbdr.Read())
                                {
                                    OleDbCommand insjcbwzx = new OleDbCommand("insert into cjbwzx_table(cjbid,wzx) values(" + alljcbdr.GetInt32(0) + ",0)", conn);
                                    insjcbwzx.ExecuteNonQuery();
                                }
                                alljcbdr.Close();                            string jh = "";
                                for (int h = 0; h < myd.Length; h++)
                                {
                                    jh += myd[h];
                                    OleDbCommand findgzcjbname = new OleDbCommand("select * from cjb_table where cjbname ='" + myd[h] + "'", conn);
                                    OleDbDataReader gzcjbdr = findgzcjbname.ExecuteReader();
                                    int cjbid = 0;
                                    if (gzcjbdr.Read())
                                    {
                                        cjbid = gzcjbdr.GetInt32(0);
                                    }
                                    gzcjbdr.Close();
                                    string sql2 = "insert into cjbwzx_table(cjbid,wzx) values(" + cjbid + ",1)";  //损坏1  正常0
                                    OleDbCommand cmdu = new OleDbCommand(sql2, conn);
                                    cmdu.ExecuteNonQuery();
                                    //string sql4 = "insert into cjbygz_table(name,sj) values('" + myd[h] + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "')";
                                    //OleDbCommand cmdi = new OleDbCommand(sql4,conn);
                                    //cmdi.ExecuteNonQuery();
                                }
                                string[] fmyd = getBtsj(datacjb, myd);
                                for (int l = 0; l < fmyd.Length; l++)
                                {
                                    string porttexthead = "aa55";
                                    string porttextdata = "f00002" + fmyd[l] + "02";
                                    string yhh = "ff";
                                    string[] portdata = ConvertStringArray(porttextdata);
                                    for (int jij = 0; jij < portdata.Length; jij++)
                                    {
                                        yhh = GetXorSum(yhh, portdata[jij]);
                                    }
                                    if (yhh.Length == 1) { yhh = "0" + yhh; }
                                    string porttext = porttexthead + porttextdata + yhh;//获得该采集板下的检测板
                                    string[] portstrtext = ConvertStringArray(porttext);
                                    byte[] pst = new byte[portstrtext.Length];
                                    for (int ii = 0; ii < pst.Length; ii++)
                                    {
                                        pst[ii] = Convert.ToByte(portstrtext[ii], 16);
                                    }
                                    port.Write(pst, 0, pst.Length);
                                    #region 为了等
                                        for (int i = 0; i < 200;i++ )
                                        {
                                            Console.Write("waitwaitwaitwaitwaitwaitwaitwaitwait");
                                        }
                                    #endregion
                                }
                                MessageBox.Show("存在有故障的采集板" + jh);
                            }                       
                            else
                            {
                                MessageBox.Show("不存在有故障的采集板");                            int cjbid = 0;
                                for (int jl = 0; jl < cjbsrtarray.Length; jl++)
                                {
                                    OleDbCommand findgzcjbname = new OleDbCommand("select * from cjb_table where cjbname ='" + cjbsrtarray[jl] + "'", conn);
                                    OleDbDataReader gzcjbdr = findgzcjbname.ExecuteReader();
                                    if (gzcjbdr.Read())
                                    {
                                        cjbid = gzcjbdr.GetInt32(0);
                                    }
                                    gzcjbdr.Close();
                                    string sql3 = "insert into cjbwzx_table(cjbid,wzx) values(" + cjbid + ",0)";  //损坏1  正常0
                                    OleDbCommand cmdu = new OleDbCommand(sql3, conn);
                                    cmdu.ExecuteNonQuery();                                string porttexthead = "aa55";
                                    string porttextdata = "f00002" + cjbsrtarray[jl] + "02";
                                    string yhh = "ff";
                                    string[] portdata = ConvertStringArray(porttextdata);
                                    for (int jij = 0; jij < portdata.Length; jij++)
                                    {
                                        yhh = GetXorSum(yhh, portdata[jij]);
                                    }
                                    if (yhh.Length == 1) { yhh = "0" + yhh; }
                                    string porttext = porttexthead + porttextdata + yhh;//获得该采集板下的检测板
                                    string[] portstrtext = ConvertStringArray(porttext);
                                    byte[] pst = new byte[portstrtext.Length];
                                    for (int ii = 0; ii < pst.Length; ii++)
                                    {
                                        pst[ii] = Convert.ToByte(portstrtext[ii], 16);
                                    }
                                    port.Write(pst, 0, pst.Length);
                                    #region 为了等 
                                    for (int i = 0; i < 200;i++ )
                                    {
                                        Console.Write("waitwaitwaitwaitwaitwaitwaitwaitwait");
                                    }
                                    #endregion
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            for (int jj = 0; jj < cjbsrtarray.Length; jj ++)
                            {
                                string sql = "insert into cjb_table(cjbname,flsrname) values('" + cjbsrtarray[jj] + "','" + cjbflsrarray[jj] + "')";
                                OleDbCommand cmd = new OleDbCommand(sql, conn);
                                int ji = cmd.ExecuteNonQuery();
                                if (ji > 0)
                                {
                                    MessageBox.Show(cjbsrtarray[jj] + "添加成功!");
                                    // OleDbCommand cmd2 = new OleDbCommand("insert into jcb_table(jcbname,cjbname) values('"+vv+"','"+cjbsrtarray[jj]+"')");
                                    string porttexthead = "aa55";
                                    string porttextdata = "f00002" + cjbsrtarray[jj] + "02";
                                    string yhh = "ff";
                                    string[] portdata = ConvertStringArray(porttextdata);
                                    for (int jij = 0; jij < portdata.Length; jij++)
                                    {
                                        yhh = GetXorSum(yhh, portdata[jij]);
                                    }
                                    if (yhh.Length == 1) { yhh = "0" + yhh; }
                                    string porttext = porttexthead + porttextdata + yhh;//获得该采集板下的检测板
                                    string[] portstrtext = ConvertStringArray(porttext);
                                    byte[] pst = new byte[portstrtext.Length];
                                    for (int ii = 0; ii < pst.Length; ii++)
                                    {
                                        pst[ii] = Convert.ToByte(portstrtext[ii], 16);
                                    }
                                    port.Write(pst, 0, pst.Length);
                                    #region 为了等
                                    for (int i = 0; i < 250; i++)
                                    {
                                        Console.Write("waitwaitwaitwaitwaitwaitwaitwaitwait");
                                    }
                                    #endregion
                                }
                                else
                                {
                                    MessageBox.Show(cjbsrtarray[jj] + "添加失败!");
                                }
                            }
                            conn.Close();                    }
                    }
                    else {
                        MessageBox.Show("没有回应的数据!");
                    }
            }
            catch
            {
                MessageBox.Show("添加失败!");
            }
              
            }
      

  6.   

    public fclsRS232Tester() 
            { 
                InitializeComponent(); 
            } 
    你把你的设计器代码也贴出来。在贴之前你先在你的读取事件里添加调试代码
     private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e) 
            { 
                MessageBox.Show("有数据");
                string datanum = port.BytesToRead.ToString();//缓冲区中的字节数 
                  byte[] yuan_byte = new byte[Convert.ToInt32(datanum)]; 
                for (int i = 0; i < Convert.ToInt16(datanum); i++) 看看是否弹出消息框。
      

  7.   

    要注意的是SerialPort中的DataReceived事件的触发是在另外一个线程中进行的,不是主线程