1. webapi如下:
// GET: api/Default/5 public string Get(int id) { return "value"+id; }
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; } }
正常情况下调用完全没问题
3. 程序有个地方,需要接收串口中的数据,串口程序单独执行也完全没有问题(单独执行,跑一天也没问题).
4. 在接收串口数据的时候,执行2中调用webapi的代码,界面立即死掉,什么都点不了.
请问哪位遇到这种情况,帮忙看一下,谢谢!
串口发送可以用模拟串口工具"VSPD".
源代码在http://pan.baidu.com/s/1kTtNuRH可以下载
串口调试器http://pan.baidu.com/s/1mgMR8M0可以下载
串口的代码是怎样的?
串口代码如下:
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 = "正常读取数据"; } }
@W e i: 代码大致看了一下,建议试一下把serialPort1_DataReceived里的代码新开一个线程执行。