Form1.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System.Collections.Generic;
  2. using System.IO.Ports;
  3. using System.Text.RegularExpressions;
  4. namespace Air105摄像头预览
  5. {
  6. public partial class Form1 : Form
  7. {
  8. Thread thread;
  9. SerialPort serialPort = new SerialPort();
  10. string comName = "COM91";
  11. bool power_ready = false;
  12. int com_br = 1500000;
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. thread = new Thread(Run_mac_flasher);
  17. thread.Name = "com_reader";
  18. thread.IsBackground = true;
  19. }
  20. public void record_uart_data(byte[] buff, int offset, int size)
  21. {
  22. String path = "uart.dat";
  23. using (FileStream FS = new FileStream(path, File.Exists(path) ? FileMode.Append : FileMode.OpenOrCreate, FileAccess.Write))
  24. {
  25. FS.Write(buff, offset, size);
  26. FS.Close();
  27. }
  28. }
  29. private void Form1_Load(object sender, EventArgs e)
  30. {
  31. reload_com_names();
  32. thread.Start();
  33. }
  34. void reload_com_names()
  35. {
  36. comboBox_coms.Items.Clear();
  37. foreach (string com in System.IO.Ports.SerialPort.GetPortNames())
  38. {
  39. comboBox_coms.Items.Add(com);
  40. }
  41. if (comboBox_coms.Items.Count > 0)
  42. {
  43. comboBox_coms.SelectedIndex = 0;
  44. //richTextBox_logs.Text = "找到" + comboBox_coms.Items.Count + "个串口";
  45. }
  46. else
  47. {
  48. //richTextBox_logs.Text = "没有任何串口";
  49. }
  50. }
  51. void show_log(string text)
  52. {
  53. this.label_data_log.BeginInvoke(new Action(() => {
  54. this.label_data_log.Text = text;
  55. }));
  56. }
  57. void Run_mac_flasher()
  58. {
  59. Thread.Sleep(1000);
  60. var buff = new byte[16 * 1024 * 1024];
  61. while (true)
  62. {
  63. if (comName == "" || power_ready == false)
  64. {
  65. Thread.Sleep(100);
  66. continue;
  67. }
  68. if (!serialPort.IsOpen)
  69. {
  70. try
  71. {
  72. serialPort.PortName = comName;
  73. serialPort.BaudRate = this.com_br;
  74. serialPort.DataBits = 8;
  75. serialPort.StopBits = StopBits.One;
  76. serialPort.Parity = Parity.None;
  77. serialPort.Open();
  78. }
  79. catch (Exception ex)
  80. {
  81. show_log("打开串口异常 " + comName + " " + ex);
  82. Thread.Sleep(1000);
  83. continue;
  84. }
  85. }
  86. var rlen = serialPort.Read(buff, 0, buff.Length);
  87. var dataHeader = "Air105 USB JPG ";
  88. var text = "";
  89. for (int i = 0; i < buff.Length; i++)
  90. {
  91. //buff[i] = 0;
  92. }
  93. if (rlen > 0)
  94. {
  95. record_uart_data(buff, 0, rlen);
  96. if (buff[0] == 'A' && buff[1] == 'i' && buff[2] == 'r')
  97. {
  98. for (int i = 0; i < buff.Length; i++)
  99. {
  100. if (buff[i] == '\r' && buff[i+1] == '\n')
  101. {
  102. var head = System.Text.Encoding.UTF8.GetString(buff, 0, i);
  103. if (!head.StartsWith(dataHeader))
  104. {
  105. break;
  106. }
  107. // 解析出长度
  108. var tmp = head.Substring(dataHeader.Length);
  109. var dataRequire = 0;
  110. try
  111. {
  112. dataRequire = Int32.Parse(tmp);
  113. }
  114. catch (FormatException)
  115. {
  116. text = "错误的字符串" + tmp + " >> " + head;
  117. this.label_data_log.BeginInvoke(new Action(() =>
  118. {
  119. this.label_data_log.Text = text;
  120. }));
  121. break;
  122. }
  123. if (dataRequire > 16*1024*1024)
  124. {
  125. text = "最大支持16M的图片";
  126. this.label_data_log.BeginInvoke(new Action(() =>
  127. {
  128. this.label_data_log.Text = text;
  129. }));
  130. break;
  131. }
  132. //var dataRecv = rlen;
  133. while (rlen < dataRequire + i + 2)
  134. {
  135. var len2 = serialPort.Read(buff, rlen, 8192);
  136. if (len2 > 0)
  137. {
  138. rlen += len2;
  139. record_uart_data(buff, rlen, len2);
  140. }
  141. else if (len2 < 0)
  142. {
  143. break;
  144. }
  145. //Thread.Sleep(1);
  146. }
  147. text = "期待长度 " + (dataRequire + i + 2) + " 总共读取 " + rlen;
  148. var tmpbuff = new byte[dataRequire];
  149. for (int z = 0; z < dataRequire; z++)
  150. {
  151. tmpbuff[z] = buff[i + 2 + z];
  152. }
  153. File.WriteAllBytes("temp.jpg", tmpbuff);
  154. System.Drawing.Image image = null;
  155. try
  156. {
  157. image = System.Drawing.Image.FromStream(new MemoryStream(buff, i + 2, dataRequire));
  158. text = "图片解码成功 长度" + dataRequire + "字节";
  159. }
  160. catch
  161. {
  162. text = "图片不合法";
  163. }
  164. this.label_data_log.BeginInvoke(new Action(() =>
  165. {
  166. this.label_data_log.Text = text;
  167. if (image != null)
  168. this.pictureBox_main.Image = image;
  169. }));
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. Thread.Sleep(100);
  176. }
  177. }
  178. private void label_comName_Click(object sender, EventArgs e)
  179. {
  180. }
  181. private void button2_Click(object sender, EventArgs e)
  182. {
  183. reload_com_names();
  184. }
  185. private void button1_Click(object sender, EventArgs e)
  186. {
  187. if (power_ready)
  188. {
  189. power_ready = false;
  190. this.button_power.Text = "开始读取";
  191. this.button_power.BackColor = Color.Green;
  192. return;
  193. }
  194. if (this.comboBox_coms.SelectedIndex < 0 || this.comboBox_coms.Items[comboBox_coms.SelectedIndex] == null)
  195. {
  196. comName = "";
  197. MessageBox.Show(this, "请先刷新并选择串口");
  198. return;
  199. }
  200. try
  201. {
  202. this.com_br = Int32.Parse(this.textBox_br.Text);
  203. }
  204. catch (Exception ex)
  205. {
  206. MessageBox.Show(this, "波特率不合法,请检查");
  207. return;
  208. }
  209. this.power_ready = true;
  210. this.button_power.Text = "停止读取";
  211. this.comName = this.comboBox_coms.Items[comboBox_coms.SelectedIndex].ToString();
  212. this.button_power.BackColor = Color.Red;
  213. }
  214. }
  215. }