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 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 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; }