首页新闻找找看学习计划

在有串口通讯的情况下,调用webapi的时候,死机

0
悬赏园豆:20 [待解决问题]

1. webapi如下:

// GET: api/Default/5
public string Get(int id)
{
return "value"+id;
}
View Code

2.调用webapi的代码如下:

private string dooGet()
        {
            string url = "http://localhost:6408/api/Default/5";
            var handler = new HttpClientHandler();
            using (var http = new HttpClient(handler))
            {
                var response = http.GetAsync(url).Result;
                response.EnsureSuccessStatusCode();
                return response.Content.ReadAsAsync<string>().Result;
            }
        }
View Code

正常情况下调用完全没问题

3. 程序有个地方,需要接收串口中的数据,串口程序单独执行也完全没有问题(单独执行,跑一天也没问题).

4. 在接收串口数据的时候,执行2中调用webapi的代码,界面立即死掉,什么都点不了.

请问哪位遇到这种情况,帮忙看一下,谢谢!

问题补充:

串口发送可以用模拟串口工具"VSPD".

源代码在http://pan.baidu.com/s/1kTtNuRH可以下载

串口调试器http://pan.baidu.com/s/1mgMR8M0可以下载

W e i的主页 W e i | 初学一级 | 园豆:25
提问于:2015-08-08 17:03
< >
分享
所有回答(1)
0

串口的代码是怎样的?

I,Robot | 园豆:9461 (大侠五级) | 2015-08-08 17:11

串口代码如下:

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            while (!isclosing)
            {
                byte cur_data = 0;
                try
                {
                    int tempbyte = serialPort1.ReadByte();
                    if (tempbyte >= 0 && tempbyte < 256)
                        cur_data = Convert.ToByte(tempbyte);
                }
                catch (Exception ex)
                {
                    break;
                }
                try
                {
                    if (currentpos < 65)
                        buff[currentpos++] = cur_data;
                    else
                    {
                        currentpos = 0;
                        state = 0;
                        buff[currentpos++] = cur_data;
                    }
                    switch (state)
                    {
                        case 0:
                            if (cur_data == 0x01)
                            {
                                state++;
                            }
                            else
                            {
                                currentpos = 0;
                                state = 0;
                            }
                            break;
                        case 1:
                            if (cur_data == 0x02)
                            {
                                state++;
                            }
                            else
                            {
                                currentpos = 0;
                                state = 0;
                            }
                            break;
                        case 2:
                            if (currentpos == 13)
                                state++;
                            break;
                        case 3:
                            if (cur_data == 0x03)
                            {
                                state++;
                            }
                            else
                            {
                                currentpos = 0;
                                state = 0;
                            }
                            break;
                        case 4:
                            if (cur_data == 4)
                            {
                                decimal shiWei = 0, geWei = 0, shiFenWei = 0, baiFenWei = 0, qianFenWei = 0;
                                if (buff[4] > 0x30)
                                    shiWei = (buff[4] - 0x30) * 10;
                                if (buff[5] > 0x30)
                                    geWei = buff[5] - 0x30;
                                if (buff[7] > 0x30)
                                    shiFenWei = Convert.ToDecimal((buff[7] - 0x30) * (decimal)0.1);
                                if (buff[8] > 0x30)
                                    baiFenWei = Convert.ToDecimal((buff[8] - 0x30) * (decimal)0.01);
                                if (buff[9] > 0x30)
                                    qianFenWei = Convert.ToDecimal((buff[9] - 0x30) * (decimal)0.001);
                                decimal weight = shiWei + geWei + shiFenWei + baiFenWei + qianFenWei;
                                getWDingjian(weight.ToString());
                            }
                            currentpos = 0;
                            state = 0;
                            break;
                    }
                }
                catch (Exception ex2)
                {
                    break;
                }
            }
        }

        private void getWDingjian(string ssWeight) //顶尖 直接传入重量
        {
            if (txtWeight.InvokeRequired)//&& )
            {
                DisplayWeight displayWeightDJ = getWDingjian;
                txtWeight.Invoke(displayWeightDJ, ssWeight);
            }
            else
            {
                decimal tw = 0;
                decimal.TryParse(ssWeight, out tw);
                rbwd.Checked = true;
                txtWeight.Text = tw.ToString("0.0###");
                txtTotal.Text = (tw * myLsj).ToString("0.###");
                lblMsg.Text = "正常读取数据";
            }
        }
View Code

 

支持(0) 反对(0) W e i | 园豆:25 (初学一级) | 2015-08-08 17:17

@W e i: 代码大致看了一下,建议试一下把serialPort1_DataReceived里的代码新开一个线程执行。

支持(0) 反对(0) I,Robot | 园豆:9461 (大侠五级) | 2015-08-08 18:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册