示例#1
0
        private void message_SelectedIndexChanged(object sender, EventArgs e)
        {
            signal.Items.Clear();
            txtSend.Text = "";
            string select_m = (string)message.SelectedItem.ToString();

            if (select_m != "")
            {
                int m_id = 0;
                foreach (CANMessageObject cm in canMessage)
                {
                    string ss = new string(cm.MessageName);
                    if (ss.Equals(select_m))
                    {
                        m_id = Convert.ToInt32(cm.Id);
                        break;
                    }
                }
                //SqlHelper.connect();
                //string sql_select = "select id from cantoolapp.canmessage where messagename = '" + select_m + "'";
                //SqlDataReader sql_m_id = SqlHelper.query(sql_select);
                //sql_m_id.Read();
                //int m_id = Convert.ToInt32(sql_m_id[0]);
                //SqlHelper.close();

                SqlHelper.connect();
                //   string sql_s = "select signalname from cantoolapp.cansignal where canmessageid = " + m_id;
                string sql_s = "select * from cantoolapp.cansignal where canmessageid = " + m_id;
                SqlDataReader sqlReader_s = SqlHelper.query(sql_s);

                while (sqlReader_s.Read())
                {
                    CANSignalObject temp = new CANSignalObject();
                    string signalName = (string)sqlReader_s["signalname"];
                    temp.SignalName = signalName.ToCharArray();
                    temp.C1 = (double)sqlReader_s["C"];
                    temp.D1 = (double)sqlReader_s["D"];
                    canSignal.Add(temp);
                    signal.Items.Add(signalName);
                }
                SqlHelper.close();

            }
        }
示例#2
0
        public static string EncodeCANSignal(string messageName, string signalName, double input, string cycle)
        {
            string result = "";

            SqlHelper.connect();
            SqlDataReader msgReader = SqlHelper.query("select * from cantoolapp.canmessage where messagename = '" + messageName + "'");

            if (!msgReader.HasRows)
            {
                MessageBox.Show("数据库中无此Message!");
                SqlHelper.close();
            }
            else
            {
                msgReader.Read();
                int  canId = Convert.ToInt32(msgReader[1].ToString());
                char tT    = ' ';
                if (canId > 0 && canId < 2047)
                {
                    tT = 't';
                }
                else if (canId > 0 && canId < 536870911)
                {
                    tT = 'T';
                }
                string canIdHex = Convert.ToString(canId, 16).PadLeft(tT == 't' ? 3 : 8, '0');
                int    DLC      = (int)msgReader[4];

                SqlHelper.close();
                SqlHelper.connect();
                SqlDataReader sigReader = SqlHelper.query("select * from cantoolapp.cansignal where signalname = '" + signalName + "'" + "and canmessageid = " + canId);
                sigReader.Read();
                if (!sigReader.HasRows)
                {
                    MessageBox.Show("Message下无信号!");
                    SqlHelper.close();
                }
                else
                {
                    char[] data = new char[DLC * 8];
                    for (int k = 0; k < DLC * 8; k++)
                    {
                        data[k] = '0';
                    }
                    double A = (double)sigReader[4];
                    double B = (double)sigReader[5];
                    double C = (double)sigReader[6];
                    double D = (double)sigReader[7];
                    if (!(input >= C && input <= D))
                    {
                        MessageBox.Show("输入的数据超出范围,请重新输入!");
                        SqlHelper.close();
                        return(null);
                    }
                    int x = (int)((input - B) / A);
                    // 起始位、bit长度、bit格式
                    string[] startAndLengthAndPattern = sigReader[3].ToString().Split(new char[2] {
                        '|', '@'
                    });
                    int    start        = (int)Convert.ToUInt32(startAndLengthAndPattern[0]);
                    int    length       = (int)Convert.ToUInt32(startAndLengthAndPattern[1]);
                    string pattern      = startAndLengthAndPattern[2];
                    string input_binary = Convert.ToString(x, 2);
                    input_binary = input_binary.PadLeft(length, '0');
                    if (pattern == "0+")
                    {
                        int m = 0, n = start;
                        int line = 0;
                        int leftIndex = 0, rightIndex = 0;
                        for (m = 0; m < length; m++)
                        {
                            data[n]    = input_binary[m];
                            line       = n / 8;
                            leftIndex  = 7 * (line + 1) + line;
                            rightIndex = 8 * line;
                            if (line >= 0 && line < 8)
                            {
                                if (n <= leftIndex && n > rightIndex)
                                {
                                    n--;
                                }
                                else if (n == rightIndex && line < 7)
                                {
                                    n = n + 15;
                                }
                            }
                        }
                    }
                    else if (pattern == "1+")
                    {
                        int t = 0, j = start;
                        for (t = length - 1; t >= 0; t--)
                        {
                            data[j] = input_binary[t];
                            j++;
                        }
                    }
                    else
                    {
                        MessageBox.Show("暂不支持此数据格式!");
                        SqlHelper.close();
                        return("");
                    }
                    SqlHelper.close();
                    string        s    = new string(data);
                    StringBuilder shex = new StringBuilder();
                    int           i    = 0;
                    int           len  = data.Length;
                    for (i = 0; i < len; i += 8)
                    {
                        int           m, n;
                        int           line     = i / 8;
                        StringBuilder tempdata = new StringBuilder();
                        for (m = 0, n = 7 * (line + 1) + line; m < 4; m++, n--)
                        {
                            tempdata.Append(data[n]);
                        }
                        shex.Append(string.Format("{0:X}", Convert.ToInt32(tempdata.ToString(), 2)));
                        tempdata = new StringBuilder();
                        for (m = 0, n = 7 * (line + 1) + line - 4; m < 4; m++, n--)
                        {
                            tempdata.Append(data[n]);
                        }
                        shex.Append(string.Format("{0:X}", Convert.ToInt32(tempdata.ToString(), 2)));
                    }
                    result = tT + canIdHex.ToUpper() + DLC + shex.ToString().ToUpper() + cycle + "\r";
                }
            }
            return(result);
        }
示例#3
0
        //加载
        private void Form1_Load(object sender, EventArgs e)
        {
            INIFILE.Profile.LoadProfile();//加载所有

            // 预置波特率
            switch (Profile.G_BAUDRATE)
            {
                case "300":
                    cbBaudRate.SelectedIndex = 0;
                    break;
                case "600":
                    cbBaudRate.SelectedIndex = 1;
                    break;
                case "1200":
                    cbBaudRate.SelectedIndex = 2;
                    break;
                case "2400":
                    cbBaudRate.SelectedIndex = 3;
                    break;
                case "4800":
                    cbBaudRate.SelectedIndex = 4;
                    break;
                case "9600":
                    cbBaudRate.SelectedIndex = 5;
                    break;
                case "19200":
                    cbBaudRate.SelectedIndex = 6;
                    break;
                case "38400":
                    cbBaudRate.SelectedIndex = 7;
                    break;
                case "115200":
                    cbBaudRate.SelectedIndex = 8;
                    break;
                default:
                    {
                        MessageBox.Show("波特率预置参数错误。");
                        return;
                    }
            }
            //预置数据位
            switch (Profile.G_DATABITS)
            {
                case "5":
                    cbDataBits.SelectedIndex = 0;
                    break;
                case "6":
                    cbDataBits.SelectedIndex = 1;
                    break;
                case "7":
                    cbDataBits.SelectedIndex = 2;
                    break;
                case "8":
                    cbDataBits.SelectedIndex = 3;
                    break;
                default:
                    {
                        MessageBox.Show("数据位预置参数错误。");
                        return;
                    }
            }
            //预置停止位
            switch (Profile.G_STOP)
            {
                case "1":
                    cbStop.SelectedIndex = 0;
                    break;
                case "1.5":
                    cbStop.SelectedIndex = 1;
                    break;
                case "2":
                    cbStop.SelectedIndex = 2;
                    break;
                default:
                    {
                        MessageBox.Show("停止位预置参数错误。");
                        return;
                    }
            }

            //预置校验位
            switch (Profile.G_PARITY)
            {
                case "None":
                    cbParity.SelectedIndex = 0;
                    break;
                case "Odd":
                    cbParity.SelectedIndex = 1;
                    break;
                case "Even":
                    cbParity.SelectedIndex = 2;
                    break;
                default:
                    {
                        MessageBox.Show("校验位预置参数错误。");
                        return;
                    }
            }
            //预置CAN速率
            switch (Profile.G_RATE)
            {
                case "10":
                    canRate.SelectedIndex = 0;
                    break;
                case "20":
                    canRate.SelectedIndex = 1;
                    break;
                case "50":
                    canRate.SelectedIndex = 2;
                    break;
                case "100":
                    canRate.SelectedIndex = 3;
                    break;
                case "125":
                    canRate.SelectedIndex = 4;
                    break;
                case "250":
                    canRate.SelectedIndex = 5;
                    break;
                case "500":
                    canRate.SelectedIndex = 6;
                    break;
                case "800":
                    canRate.SelectedIndex = 7;
                    break;
                case "1024":
                    canRate.SelectedIndex = 8;
                    break;
                default:
                    {
                        MessageBox.Show("CAN速率预置参数错误。");
                        return;
                    }
            }

            //检查是否含有串口
            string[] str = SerialPort.GetPortNames();
            if (str == null)
            {
                MessageBox.Show("本机没有串口!", "Error");
                return;
            }

            //添加串口项目
            foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
            {//获取有多少个COM口
                //System.Diagnostics.Debug.Write(s);
                cbSerial.Items.Add(s);
            }

            //message
            string sql_m = "select * from cantoolapp.canmessage";
            SqlHelper.connect();
            SqlDataReader sqldr_m = SqlHelper.query(sql_m);
            while (sqldr_m.Read())
            {
                string messageName = (string)sqldr_m["messagename"];
                CANMessageObject temp = new CANMessageObject();
                temp.MessageName = messageName.ToCharArray();
                temp.Id = Convert.ToUInt32(sqldr_m["id"]);
                canMessage.Add(temp);
                message.Items.Add(messageName);
            }
            SqlHelper.close();

            //串口设置默认选择项
            //cbSerial.SelectedIndex = 1;         //note:获得COM9口,但别忘修改

            Control.CheckForIllegalCrossThreadCalls = false;
            //这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,,不允许在winform中直接跨线程访问控件的属性)
            sp1.DataReceived += new SerialDataReceivedEventHandler(sp1_DataReceived);
            //sp1.ReceivedBytesThreshold = 1;

            //准备就绪              
            sp1.DtrEnable = true;
            sp1.RtsEnable = true;
            //设置数据读取超时为1秒
            sp1.ReadTimeout = 1000;
            sp1.Close();
        }