c# socket采集上传程序
程序功能:
1、数据库读取数据;
2、数据格式转换成6进制;
3、CRC-16校验码计算;
4、TCP socket send;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.OleDb; 6 using System.Data; 7 using System.Net; 8 using System.Net.Sockets; 9 using System.Threading; 10 using System.Runtime.Serialization.Formatters.Binary; 11 using System.IO; 12 13 namespace tinso35 14 { 15 class Program 16 { 17 //----------项目编号------------ 18 static int pid = 2010; 19 private static byte[] result = new byte[46]; 20 static int sum = 1; 21 22 static void TcpSocket() 23 { 24 string danwei1 = "mA"; 25 string danwei2 = "℃"; 26 result = new byte[] { 0x7E, 0x00, 0x0A, 0x00, 0x01, 0x03, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD1, 0x22, 0x7F }; 27 //--------数据库数据读取------------------ 28 OleDbDataAdapter dataAdapter = new OleDbDataAdapter(); 29 OleDbConnection con = new OleDbConnection(); 30 OleDbCommand sqlcmd = new OleDbCommand(); 31 DataSet ds = new DataSet(); 32 string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\FrieAl\\ts.mdb"; 33 string cmdstr = "SELECT Information.* FROM Information"; 34 con = new OleDbConnection(strcon); 35 con.Open(); 36 sqlcmd.Connection = con; 37 sqlcmd.CommandType = CommandType.Text; 38 sqlcmd.CommandText = cmdstr; 39 dataAdapter.SelectCommand = sqlcmd; 40 dataAdapter.Fill(ds); 41 int rows = ds.Tables[0].Rows.Count; 42 for (int g = 0; g < rows; g++) 43 { 44 string sbid = ds.Tables[0].Rows[g]["Rs485Add"].ToString(); 45 string str1 = ds.Tables[0].Rows[g]["no1"].ToString(); 46 string str2 = ds.Tables[0].Rows[g]["no2"].ToString(); 47 string alarminfo = ds.Tables[0].Rows[g]["alarm"].ToString(); 48 //Console.WriteLine(sbid + "漏电" + str1 + "温度" + str2 + "报警:" + alarminfo + "ds行数:" + rows); 49 //con.Close(); 50 /* 51 //--------------数据格式转换----------------- 52 */ 53 //---------项目编号处理----------- 54 string strpid = pid.ToString(format: "x4"); 55 byte[] bpid = strToToHexByte(strpid); 56 result[1] = bpid[0]; 57 result[2] = bpid[1]; 58 59 //---------设备编号处理----------- 60 string sbid2 = null; 61 for (int i = 0; i < danwei1.Count(); i++) 62 { 63 sbid2 = sbid.Replace(danwei1, ""); 64 } 65 sbid2 = sbid2.Trim(); 66 int sbid3 = Convert.ToInt32(sbid2); 67 string sbid4 = sbid3.ToString(format: "x4"); 68 byte[] sbid5 = strToToHexByte(sbid4); 69 result[3] = sbid5[0]; 70 result[4] = sbid5[1]; 71 72 //--------漏电1处理----------------- 73 string ld = "null"; 74 for (int i = 0; i < danwei1.Count(); i++) 75 { 76 77 ld = str1.Replace(danwei1, ""); 78 } 79 ld = ld.Trim(); 80 int ld2 = Convert.ToInt32(ld); 81 string r2 = ld2.ToString(format: "x4"); 82 byte[] bld = strToToHexByte(r2); 83 result[19] = bld[0]; 84 result[20] = bld[1]; 85 86 //--------------温度1处理--------------- 87 string wendu = "null"; 88 for (int i = 0; i < danwei2.Count(); i++) 89 { 90 91 wendu = str2.Replace(danwei2, ""); 92 } 93 wendu = wendu.Trim(); 94 int wd = Convert.ToInt32(wendu) * 10; 95 string r1 = wd.ToString(format: "x4"); 96 byte[] w = strToToHexByte(r1); 97 result[27] = w[0]; 98 result[28] = w[1]; 99 100 //----------------报警位处理-------------------- 101 if (alarminfo == "报警" || alarminfo == "已接警" || alarminfo == "已报警") 102 { 103 result[37] = 0x01; 104 } 105 else 106 { 107 result[37] = 0x00; 108 } 109 110 //----------------CRC-------------------- 111 byte[] newA = result.Skip(1).Take(42).ToArray(); 112 int crc = CRC16(newA, newA.Length); 113 string crc2 = Convert.ToString(crc, 16); 114 if (crc2.Length > 4) 115 { 116 crc2 = crc2.Remove(0, 4); 117 118 } 119 else if (crc2.Length < 4) 120 { 121 for (int k = 0; k <= 4 - crc2.Length; k++) 122 { 123 crc2 = 0 + crc2; 124 } 125 126 } 127 //Console.Write("打印crc:" + crc2 + "\n"); 128 byte[] crc3 = strToToHexByte(crc2); 129 result[43] = crc3[0]; 130 result[44] = crc3[1]; 131 132 //------------socket 发送数据------------------ 133 IPAddress ip = IPAddress.Parse("47.100.13.162"); 134 //IPAddress ip = IPAddress.Parse("192.168.1.8"); 135 Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 136 try 137 { 138 clientSocket.Connect(new IPEndPoint(ip, 3000)); 139 //Console.WriteLine("连接服务器成功:"); 140 } 141 catch 142 { 143 Console.WriteLine("连接服务器失败,请按回车键退出!"); 144 return; 145 } 146 for (int i = 0; i < 1; i++) 147 { 148 try 149 { 150 Thread.Sleep(1000); 151 clientSocket.Send(result); 152 //Console.WriteLine("向服务器发送消息:" + System.Text.Encoding.Default.GetString(result)); 153 /* 154 string recvStr = ""; 155 byte[] recvBytes = new byte[1024]; 156 int bytes; 157 bytes = clientSocket.Receive(recvBytes, recvBytes.Length, 0); 158 recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes); 159 Console.WriteLine(bytes); 160 Console.WriteLine("Client Get Message:{0}", recvStr);//显示服务器返回信息 161 */ 162 } 163 catch 164 { 165 clientSocket.Shutdown(SocketShutdown.Both); 166 clientSocket.Close(); 167 break; 168 } 169 //Console.WriteLine("发送完毕!"); 170 } 171 Console.WriteLine("第" + (g + 1) + "条数据上传成功!"); 172 173 174 } 175 con.Close(); 176 ds.Clear(); 177 Console.WriteLine("------完成第" + sum + "次数据上传!------"); 178 sum++; 179 180 } 181 182 /// <summary> 183 /// 字符串转16进制字节数组 184 /// </summary> 185 /// <param name="hexString"></param> 186 /// <returns></returns> 187 private static void Senddata(byte[] buff) 188 { 189 190 } 191 192 /// <summary> 193 /// 字符串转16进制字节数组 194 /// </summary> 195 /// <param name="hexString"></param> 196 /// <returns></returns> 197 private static byte[] strToToHexByte(string hexString) 198 { 199 hexString = hexString.Replace(" ", ""); 200 if ((hexString.Length % 2) != 0) 201 hexString += " "; 202 byte[] returnBytes = new byte[hexString.Length / 2]; 203 for (int i = 0; i < returnBytes.Length; i++) 204 returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); 205 return returnBytes; 206 } 207 208 /// <summary> 209 /// 字节数组转16进制字符串 210 /// </summary> 211 /// <param name="bytes"></param> 212 /// <returns></returns> 213 public static string byteToHexStr(byte[] bytes) 214 { 215 string returnStr = ""; 216 if (bytes != null) 217 { 218 for (int i = 0; i < bytes.Length; i++) 219 { 220 returnStr += bytes[i].ToString("X2"); 221 } 222 } 223 return returnStr; 224 } 225 /// <summary> 226 /// CRC-16 227 /// </summary> 228 /// <param name="bytes"></param> 229 /// <returns></returns> 230 static byte[] ArrayCRCHigh = 231 { 232 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 233 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 234 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 235 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 236 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 237 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 238 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 239 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 240 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 241 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 242 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 243 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 244 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 245 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 246 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 247 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 248 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 249 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 250 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 251 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 252 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 253 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 254 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 255 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 256 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 257 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 258 }; 259 static byte[] checkCRCLow = 260 { 261 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 262 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0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, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 281 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 282 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 283 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 284 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 285 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 286 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 287 }; 288 public static Int16 CRC16(byte[] data, int arrayLength) 289 { 290 byte CRCHigh = 0xFF; 291 byte CRCLow = 0xFF; 292 byte index; 293 int i = 0; 294 while (arrayLength-- > 0) 295 { 296 index = (System.Byte)(CRCHigh ^ data[i++]); 297 CRCHigh = (System.Byte)(CRCLow ^ ArrayCRCHigh[index]); 298 CRCLow = checkCRCLow[index]; 299 } 300 return (Int16)(CRCHigh << 8 | CRCLow); 301 } 302 303 //Main方法 304 static void Main(string[] args) 305 { 306 307 Console.WriteLine("**********项目编号:" + pid + "开始上传数据,请勿关闭!**********"); 308 while (true) 309 { 310 try 311 { 312 TcpSocket(); 313 System.Threading.Thread.Sleep(1000 * 30); 314 } 315 catch (Exception) 316 { 317 System.Threading.Thread.Sleep(1000 * 60 * 5); 318 } 319 320 } 321 322 323 } 324 } 325 }
岁月的痕迹,知识的沉淀,生活的感悟,情感的寄托......