private int blockRead(byte cmd, byte[] data, int len,ref MyportData myD) { QueueData c; int i = 0,time = 0; byte j; serialPort1.ReadTimeout = 2000; myD.start = 0xaa; // aa 01 00 00 00 00 ee myD.cmd = cmd; myD.len = len; myD.data = data; myD.end = 0xee; byte[] b = myD.getByte(); serialPort1.Write(b, 0, b.Length); //发送命令 while (true) { while (portRecQueue.TryDequeue(out c)) { if (c.cmd == "port2Thread") { myD = (MyportData)c.o; return 1; //找到串口了 } } if (time > 75) break; Thread.Sleep(20); } return 0; }
private void DataReceive(object sender, SerialDataReceivedEventArgs e) { int i = 0; byte j; MyportData myRecData = new MyportData(); while (true) { try { j = (byte)serialPort1.ReadByte(); i = myRecData.saveChar(j);//返回值 1 正在接收 2接收完成 负数有错误自动重新开始 if (i == 2) { QueueData qd = new QueueData(); qd.cmd = "port2Thread"; qd.o = myRecData; portRecQueue.Enqueue(qd); myRecData = new MyportData(); myRecData.num = 0; } } catch (Exception) { myRecData.num = 0; return; } } }
private int runCmd(byte cmd,byte[] b,int len,int time) { QueueData c; MyportData sendData = new MyportData(); byte[] b2; sendData.start = 0xbb; sendData.cmd = cmd; sendData.len = len; sendData.data = b; sendData.end = 0xee; b2 = sendData.getByte(); while (portRecQueue.TryDequeue(out c)); serialPort1.Write(b2, 0, b2.Length); //发送命令 while (true) { time--; while (portRecQueue.TryDequeue(out c)) { if (c.cmd == "port2Thread") { g_tem = (MyportData)c.o; if (g_tem.cmd == (cmd|0x10)) { return 1; }else { return -1; } } } if (time < 0) break; Thread.Sleep(1); } return -1; }
private int IAPUpdateCOMport()//在新线程中调用 { MyportData sendData = new MyportData(); //portFlag = 1; for (int i = 1; i < 255; i++) { serialPort1.Close(); try { serialPort1.PortName = "COM" + i; serialPort1.Open(); QueueData c; int time = 0; /*********************特殊板子 镜头*************************************/ if ((type == 15) || (type == 12)) //cfl镜头 可能使用转板 字符串形式 { serialPort1.BaudRate = 115200; while (true) { time++; byte[] b = Encoding.ASCII.GetBytes("Rest\r"); //b = sendData.getByte(); serialPort1.Write(b, 0, b.Length); //发送命令 if (time > 10) break; Thread.Sleep(20); while (portRecQueue.TryDequeue(out c)) { if (c.cmd == "port2Thread") { MyportData tem = (MyportData)c.o; if (tem.cmd == 0x11) { oldUp = 2; serialPort1.BaudRate = 460800; return 1; //找到串口了 } } } } time = 0; serialPort1.BaudRate = 460800; while (true) { time++; byte[] b; sendData.start = 0xbb; sendData.cmd = 0x01; sendData.len = 0; sendData.end = 0xee; b = sendData.getByte(); serialPort1.Write(b, 0, b.Length); //发送命令 if (time > 5) break; Thread.Sleep(20); while (portRecQueue.TryDequeue(out c)) { if (c.cmd == "port2Thread") { MyportData tem = (MyportData)c.o; if (tem.cmd == 0x11) { oldUp = 2; serialPort1.BaudRate = 460800; return 1; //找到串口了 } } } } } /************S10*************************/ time = 0; serialPort1.BaudRate = 14400; while (true) { time++; byte[] b; sendData.start = 0xbb; sendData.cmd = 0x01; sendData.len = 0; sendData.end = 0xee; b = sendData.getByte(); serialPort1.Write(b, 0, b.Length); //发送命令 if (time > 5) break; Thread.Sleep(20); while (portRecQueue.TryDequeue(out c)) { if (c.cmd == "port2Thread") { MyportData tem = (MyportData)c.o; if (tem.cmd == 0x11) { oldUp = 0; serialPort1.BaudRate = 460800; return 1; //找到串口了 } } } } /****************旧老化主机***********************/ time = 0; if (type == 1) { MCURest(); serialPort1.BaudRate = 460800; while (true) { time++; byte[] b; sendData.start = 0xaa; sendData.cmd = 0x01; sendData.len = 0; sendData.end = 0xee; b = sendData.getByte(); serialPort1.Write(b, 0, b.Length); //发送命令 if (time > 75) break; Thread.Sleep(20); while (portRecQueue.TryDequeue(out c)) { if (c.cmd == "port2Thread") { MyportData tem = (MyportData)c.o; if (tem.cmd == 0x01) { oldUp = 1; return 1; //找到串口了 } } } } } serialPort1.Close(); } catch (Exception) { continue; } } return -1; }
private int downlandIAPOld() { int start = 0; MyportData myD = new MyportData(); byte[] b = new byte[1024 * 1024]; int l; if ((l = readFile(ref b,ref start)) < 0) { return -1; } byte[] b2 = BitConverter.GetBytes(l); int j = blockRead(0x02, b2, 4, ref myD); if ((j == 1) && (myD.cmd == 0x02) && (myD.data[0] == 0x01)) { setText("debug", "擦除成功\r\n"); } else { setText("debug", "擦除错误\r\n"); setText("debug", "threadStop\r\n"); return -1; } setProgressBar1("Maximum", l); byte[] temB = new byte[4 * 1024]; int temBLen = 4 * 1024; int i = 0, maxI = 0; maxI = l / 4 * 1024; if ((l % 4 * 1024) != 0) maxI++; int ca = l; while (true) { if (ca >= 1024 * 4) { temBLen = 4 * 1024; Array.Copy(b, i * 1024 * 4, temB, 0, 4 * 1024); } else if (ca > 0) { temBLen = ca; Array.Copy(b, i * 1024 * 4, temB, 0, ca); } else { setText("debug", "发送完成\r\n"); break; } j = blockRead(0x03, temB, temBLen, ref myD); if ((j == 1) && (myD.cmd == 0x03)) { //setText("debug", "write" + i + "\r\n"); int temI = myD.data[1] + myD.data[2] * 256 + myD.data[3] * 256 * 256 + myD.data[4] * 256 * 256 * 256; if (temI != i) { setText("debug", "包编号" + i + "顺序出错" + "temI:" + temI + "\r\n"); break; } if (myD.data[0] == 0x01) { ca = ca - temBLen; setProgressBar1("Value", l - ca); i++; } else { setText("debug", "编号:" + i + " 错误\r\n"); } } else { if (j == 1) { setText("debug", "命令错误\r\n"); } else { setText("debug", "超时\r\n"); } break; } } MCURest2(); serialPort1.Close(); Thread.Sleep(100); setText("debug", "串口关闭\r\nthreadStop\r\n"); //progressBar1.Value = 0; return 0; }
private int processUIcmd(QueueData cm) { int restFlag = 0; int time = 0; int i = 0; QueueData c = new QueueData(); MyportData sendData = new MyportData(); BData hs = (BData)cm.o; type = hs.type; id = hs.id; Path = hs.path; startA = hs.startA; serialPort1.Close(); serialPort1.BaudRate = 460800; serialPort1.PortName = hs.COM; serialPort1.Open(); setProgressBar1("Value", 0); i = 0; while (true) { if (runCmd(0x01, null, 0, 50) >= 0) { setText("debug", "转板进入iap成功\r\n"); break; } i++; if (i > 20) { setText("debug", "转板进入iap失败\r\n"); return -1; } Thread.Sleep(1); } downlandCFL(); //if ((type == 12) || (type == 15)) //{ // try // { // serialPort1.Close(); // serialPort1.BaudRate = 115200; // serialPort1.PortName = hs.COM; // serialPort1.Open(); // } // catch // { // setText("debug", "未能打开串口,请先通电用普通方式找到串口,或手动修改XML文件填写端口重启软件\r\n"); // return -1; // } // setText("debug", "已打开串口\r\n"); // i = 0; // while (true) // { // i++; // byte[] b = Encoding.ASCII.GetBytes("Rest\r"); // //b = sendData.getByte(); // serialPort1.Write(b, 0, b.Length); //发送命令 // if (i > 10) // { // setText("debug", "主板复位失败\r\n"); // break; //超时 不是转板 // } // Thread.Sleep(20); // while (portRecQueue.TryDequeue(out c)) // { // if (c.cmd == "port2Thread") // { // MyportData tem = (MyportData)c.o; // if (tem.cmd == 0x11) // { // setText("debug", "主板进入iap成功\r\n"); // serialPort1.BaudRate = 460800; // i = 0; // break; // } // } // } // if (i == 0) // break; // } // serialPort1.BaudRate = 460800; // i = 0; // Thread.Sleep(5); //while (true) //{ // if (runCmd(0x40, null, 0, 20) >= 0) // { // setText("debug", "转板进入iap成功\r\n"); // break; // } // i++; // if (i > 10) // { // setText("debug", "转板进入iap失败\r\n"); // return -1; // } // Thread.Sleep(1); //} // if (type == 12) // { // i = downlandOther(hs.downMode); // return i; // } // else // { // i = downlandCFL(); // return i; // } //} serialPort1.Close(); return 0; }
private int runForwarding(byte cmd, byte[] b, int len, int time) { QueueData c; MyportData sendData = new MyportData(); byte[] b2; sendData.start = 0xbb; sendData.cmd = cmd; sendData.len = len; sendData.data = b; sendData.end = 0xee; b2 = sendData.getByte(); byte[] b3 = new byte[12 + b2.Length]; int[] i3 = new int[] { type,id, time }; Buffer.BlockCopy(i3, 0, b3, 0, 12); Buffer.BlockCopy(b2, 0, b3, 12, b2.Length); sendData.start = 0xbb; sendData.cmd = 0x61; sendData.len = b3.Length; sendData.data = b3; sendData.end = 0xee; b2 = sendData.getByte(); while (portRecQueue.TryDequeue(out c)); serialPort1.Write(b2, 0, b2.Length); //发送命令 time += 10; while (true) { time--; while (portRecQueue.TryDequeue(out c)) { if (c.cmd == "port2Thread") { MyportData tem = (MyportData)c.o; if (tem.cmd == (0x61 | 0x10)) { MyportData myPData = new MyportData(); for (int i = 0; i < tem.len; i++) { myPData.saveChar(tem.data[i]); } g_tem = myPData; if (myPData.cmd == (cmd | 0x10)) return 1; else return -1; }else { return -1; } } } if (time < 0) break; Thread.Sleep(1); } return -1; }