/// <summary> /// 接收数据 /// </summary> /// <param name="socket"></param> /// <param name="timeout"></param> /// <param name="content"></param> /// <param name="conLen"></param> /// <param name="type"></param> /// <param name="tel"></param> /// <returns></returns> public static bool rdata(ref string protocol, Socket socket, int timeout, ref byte[] content, ref int conLen, ref byte type, ref string tel) { if (protocol == DTU._ZG_DTU) { return(ZG_DTU.rdata(socket, timeout, ref content, ref conLen, ref type, ref tel)); //1秒超时 等待注册数据 } else if (protocol == DTU._HD_DTU) { return(HD_DTU.rdata(socket, timeout, ref content, ref conLen, ref type, ref tel)); //1秒超时 等待注册数据 } else if (protocol == DTU._JBT_DTU) { return(JBT_DTU.rdata(socket, timeout, ref content, ref conLen, ref type, ref tel)); //1秒超时 等待注册数据 } else { //在DTU未注册之前, socket接收都是调用以下的代码 , try { byte[] buffer = new byte[GlobalPara.SEND_DATA_LENGTH]; //接收数据的数组 socket.ReceiveTimeout = timeout; int len = socket.Receive(buffer, buffer.Length, SocketFlags.None); protocol = DTU.CheckProtocolByRegisterData(buffer, len); if (protocol == DTU._ZG_DTU) { return(ZG_DTU.UnPack(buffer, len, ref content, ref conLen, ref type, ref tel)); } else if (protocol == DTU._HD_DTU) { return(HD_DTU.UnPack(buffer, len, ref content, ref conLen, ref type, ref tel)); } else if (protocol == DTU._JBT_DTU) { return(JBT_DTU.UnPack(buffer, len, ref content, ref conLen, ref type, ref tel)); } else { return(false); } } catch (SocketException) { return(false); } catch (Exception ex) { LogMg.AddError(ex); return(false); } } }
/// <summary> /// 拍照 /// </summary> /// <param name="client"></param> /// <param name="timeout"></param> public static bool TakePhoto(DTUClientInfo client, int timeout, ref byte[] imageBytes) { //这个方法主要包含三个步骤, //第一步:发送拍照指令 //第二步:发送“获取照片包数量”的指令 //第三步:循环去读取每一个照片包内容 byte[] rubbish = new byte[JBT_DTU.ReceiveLength]; byte packcount = 0x00; int n = 1; //当发送的命令没接收到,这个变量就会+1,直到他等于5 int commandLen1 = 0; byte[] command1 = GetTakePhotoCommand(ref commandLen1); try { client.socket.Receive(rubbish, JBT_DTU.ReceiveLength, SocketFlags.None); } catch (Exception) { } //第一步:发送拍照指令 JBT_DTU.SendUserData_Assistant(client.socket, command1, commandLen1, client.TelOrGprsId); while (n <= 5) { try { byte[] content = new byte[JBT_DTU.ReceiveLength]; int conLen = 0; byte type = new byte(); string tel = ""; Thread.Sleep(3000); bool flag = JBT_DTU.rdata(client.socket, timeout, ref content, ref conLen, ref type, ref tel); if (flag == false) { n++; continue; } if (type != JBT_DTU.DTU_to_Server_SendData_Assistant) { n++; continue; } if (conLen != 7) { n++; continue; } break; } catch (SocketException ex) { n++; LogMg.AddError(ex); } catch (Exception ex) { n++; LogMg.AddError(ex); } } DTU_ClientManager.UpdateLastVisitTime(client, DateTime.Now); if (n > 5) { return(false); //如果读取5次都读不到数据,就表示这次拍照失败 } LogMg.AddError("拍照命令 拍照次数=" + n); n = 1; int commandLen2 = 0; //第二步:发送“获取照片包数量”的指令 byte[] command2 = GetPicPackCountCommand(ref commandLen2); JBT_DTU.SendUserData_Assistant(client.socket, command2, commandLen2, client.TelOrGprsId); while (n <= 5) { try { byte[] content = new byte[4096]; int conLen = 0; byte type = new byte(); string tel = ""; Thread.Sleep(1000); bool flag = JBT_DTU.rdata(client.socket, timeout, ref content, ref conLen, ref type, ref tel); if (flag == false) { n++; continue; } if (type != JBT_DTU.DTU_to_Server_SendData_Assistant) { n++; continue; } if (conLen != 8) { n++; continue; } packcount = content[4]; break; } catch (SocketException ex) { n++; LogMg.AddError(ex); } catch (Exception ex) { n++; LogMg.AddError(ex); } } if (n > 5) { return(false); } LogMg.AddError("获取照片包数量 n=" + n); imageBytes = new byte[packcount * 1000]; int picIndex = 0; //第三步:循环去读取每一个照片包内容 for (byte i = 0x00; i < packcount; i++) { n = 1; int commandLen3 = 0; byte[] command3 = GetReadPictureCommand(i, ref commandLen3); JBT_DTU.SendUserData_Assistant(client.socket, command3, commandLen3, client.TelOrGprsId); while (n <= 5) { try { byte[] content = new byte[4096]; int conLen = 0; byte type = new byte(); string tel = ""; Thread.Sleep(1000); bool flag = JBT_DTU.rdata(client.socket, timeout, ref content, ref conLen, ref type, ref tel); if (flag == false) { n++; continue; } if (type != JBT_DTU.DTU_to_Server_SendData_Assistant) { n++; continue; } if (content[0] != HEAD || content[1007] != HEAD) // { n++; continue; } if (content[04] != i) //如果包号不对 { n++; continue; } Buffer.BlockCopy(content, 5, imageBytes, picIndex, 1000); picIndex += 1000; break; } catch (SocketException ex) { n++; LogMg.AddError(ex); LogMg.AddError("循环读取包 n=" + n); } catch (Exception ex) { n++; LogMg.AddError(ex); } } if (n > 5) { return(false); } } //ImageUtil.SaveFormBytes(imageBytes, GetImageLen(imageBytes)); //len = GetImageLen(imageBytes); return(true); }