123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- #include "iostream"
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/un.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <stdio.h>
- #include <time.h>
- #include <pthread.h>
- #include <math.h>
- #define LEN 7
- int HoldRigster[9999] = {0};
- //冲程:4.2米
- //冲次:3.34次/分钟
- const int dis1[] = {28, 31, 35, 38, 41, 45, 49, 52, 57, 61, 65, 69, 74, 78, 83, 88, 92, 97, 102, 107, 113, 117, 123,
- 128, 133, 138, 144, 149, 154, 159, 164, 169, 175, 180, 185, 190, 196, 201, 207, 212, 217, 222, 227,
- 232, 238, 243, 247, 252, 257, 261, 267, 271, 276, 280, 285, 289, 294, 298, 303, 307, 311, 315, 319,
- 323, 327, 331, 334, 338, 342, 345, 349, 352, 355, 358, 361, 364, 367, 370, 373, 376, 379, 381, 384,
- 386, 388, 391, 393, 395, 397, 399, 401, 402, 404, 406, 407, 409, 410, 411, 412, 413, 414, 415, 416,
- 417, 417, 418, 418, 418, 418, 419, 418, 418, 418, 418, 417, 417, 416, 415, 414, 413, 412, 410, 409,
- 408, 406, 404, 403, 400, 398, 396, 394, 391, 389, 386, 383, 380, 377, 374, 371, 367, 364, 360, 357,
- 353, 350, 345, 342, 338, 333, 329, 325, 320, 316, 311, 307, 302, 298, 292, 288, 283, 278, 273, 268,
- 263, 258, 253, 248, 243, 238, 232, 227, 222, 217, 212, 207, 201, 196, 191, 186, 181, 176, 170, 165,
- 160, 155, 150, 145, 139, 134, 129, 125, 120, 115, 110, 105, 100, 96, 91, 87, 82, 78, 74, 69, 65, 61,
- 57, 53, 50, 46, 43, 39, 36, 32, 29, 27, 24, 21, 18, 16, 14, 12, 10, 8, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 2, 3, 4, 5, 7, 8, 10, 12, 14, 17, 20, 22, 25};
- const int load1[] = {7625, 7726, 7819, 7880, 7894, 7857, 7769, 7643, 7509, 7383, 7280, 7188, 7116, 7048, 7000, 6976,
- 6991, 7037, 7126, 7229, 7329, 7411, 7469, 7508, 7525, 7532, 7525, 7500, 7455, 7386, 7297, 7207,
- 7126, 7063, 7026, 7012, 7008, 7019, 7038, 7068, 7112, 7175, 7243, 7312, 7373, 7408, 7426, 7419,
- 7398, 7363, 7333, 7295, 7248, 7197, 7141, 7073, 7016, 6984, 6975, 6994, 7037, 7082, 7123, 7165,
- 7211, 7252, 7302, 7366, 7412, 7430, 7432, 7404, 7350, 7298, 7255, 7212, 7175, 7148, 7115, 7069,
- 7048, 7055, 7080, 7130, 7202, 7276, 7333, 7388, 7432, 7459, 7473, 7487, 7484, 7466, 7425, 7366,
- 7294, 7212, 7143, 7101, 7079, 7051, 7019, 6977, 6925, 6857, 6790, 6719, 6644, 6557, 6458, 6359,
- 6259, 6159, 6079, 6025, 5982, 5947, 5925, 5906, 5875, 5863, 5884, 5924, 5988, 6066, 6134, 6174,
- 6195, 6191, 6179, 6161, 6149, 6118, 6068, 5991, 5906, 5822, 5759, 5727, 5734, 5768, 5806, 5847,
- 5877, 5909, 5943, 5986, 6018, 6043, 6038, 5997, 5915, 5816, 5706, 5606, 5516, 5441, 5375, 5318,
- 5274, 5243, 5234, 5234, 5238, 5204, 5127, 5040, 4965, 4907, 4895, 4929, 4970, 5000, 5018, 5022,
- 5027, 5054, 5102, 5156, 5204, 5234, 5227, 5198, 5166, 5136, 5122, 5120, 5124, 5116, 5102, 5086,
- 5073, 5070, 5086, 5115, 5145, 5174, 5190, 5195, 5198, 5202, 5211, 5225, 5238, 5241, 5232, 5211,
- 5191, 5175, 5174, 5181, 5188, 5195, 5199, 5195, 5191, 5200, 5222, 5249, 5275, 5293, 5295, 5284,
- 5279, 5288, 5313, 5354, 5420, 5504, 5600, 5706, 5824, 5936, 6033, 6111, 6181, 6245, 6293, 6338,
- 6400, 6476, 6565, 6682, 6826, 6977, 7147, 7311, 7463, 7597};
- //冲程:4.2米
- //冲次:3.24次/分钟
- const int dis2[] = {24, 27, 30, 33, 37, 40, 44, 48, 52, 56, 60, 64, 69, 74, 78, 83, 88, 92, 98, 103, 108, 113, 118, 124,
- 129, 134, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 189, 195, 199, 205, 209, 214, 220, 224,
- 229, 234, 239, 244, 249, 254, 258, 263, 267, 272, 276, 281, 285, 290, 294, 298, 303, 306, 311, 315,
- 319, 323, 326, 330, 333, 337, 341, 344, 348, 351, 354, 357, 360, 364, 367, 370, 372, 375, 377, 380,
- 382, 385, 388, 390, 392, 394, 396, 398, 400, 402, 404, 405, 407, 408, 409, 411, 412, 413, 414, 415,
- 416, 417, 417, 418, 419, 419, 419, 419, 419, 419, 419, 419, 419, 418, 418, 417, 416, 415, 414, 413,
- 412, 411, 409, 407, 406, 404, 402, 400, 398, 395, 393, 390, 388, 385, 382, 379, 376, 373, 369, 366,
- 362, 358, 355, 351, 347, 343, 339, 334, 330, 326, 322, 317, 313, 308, 303, 298, 293, 289, 284, 279,
- 274, 269, 264, 259, 254, 248, 243, 238, 233, 227, 222, 217, 211, 206, 200, 195, 190, 184, 179, 174,
- 169, 163, 158, 153, 147, 142, 137, 132, 126, 122, 117, 111, 107, 102, 97, 92, 88, 83, 78, 74, 70,
- 66, 61, 57, 53, 50, 46, 42, 39, 36, 32, 29, 26, 23, 20, 18, 16, 14, 11, 10, 8, 6, 5, 3, 2, 1, 1, 0,
- 0, 0, 0, 0, 0, 0, 1, 2, 3, 5, 6, 8, 9, 11, 14, 16, 18, 21};
- const int load2[] = {6783, 6845, 6894, 6923, 6937, 6929, 6915, 6901, 6887, 6877, 6873, 6873, 6879, 6891, 6901, 6912,
- 6920, 6926, 6929, 6929, 6922, 6911, 6898, 6883, 6873, 6869, 6869, 6873, 6884, 6897, 6908, 6916,
- 6920, 6923, 6922, 6916, 6912, 6905, 6894, 6884, 6883, 6883, 6890, 6898, 6912, 6923, 6933, 6941,
- 6948, 6948, 6947, 6941, 6930, 6923, 6916, 6913, 6911, 6916, 6918, 6929, 6936, 6944, 6948, 6955,
- 6957, 6954, 6951, 6945, 6938, 6932, 6927, 6925, 6926, 6927, 6930, 6936, 6941, 6948, 6951, 6955,
- 6958, 6962, 6962, 6968, 6968, 6972, 6977, 6984, 6991, 7001, 7011, 7019, 7029, 7034, 7041, 7041,
- 7041, 7036, 7030, 7023, 7016, 7009, 7005, 7007, 7008, 7008, 7012, 7015, 7013, 7008, 7005, 6998,
- 6990, 6983, 6980, 6976, 6969, 6962, 6948, 6933, 6918, 6904, 6891, 6876, 6862, 6851, 6845, 6840,
- 6841, 6841, 6838, 6830, 6819, 6808, 6794, 6780, 6769, 6758, 6751, 6751, 6751, 6754, 6761, 6766,
- 6768, 6773, 6776, 6769, 6761, 6755, 6741, 6726, 6712, 6697, 6682, 6669, 6651, 6613, 6558, 6477,
- 6375, 6268, 6161, 6075, 6000, 5954, 5929, 5931, 5936, 5958, 5977, 5988, 5991, 5988, 5977, 5963,
- 5943, 5925, 5909, 5902, 5900, 5907, 5918, 5934, 5950, 5963, 5975, 5979, 5975, 5968, 5956, 5943,
- 5929, 5920, 5916, 5918, 5922, 5931, 5943, 5954, 5965, 5975, 5977, 5977, 5970, 5961, 5952, 5945,
- 5938, 5936, 5938, 5941, 5950, 5959, 5970, 5977, 5984, 5988, 5986, 5983, 5977, 5968, 5961, 5963,
- 5963, 5972, 5979, 5984, 5986, 5991, 5993, 6004, 6018, 6036, 6056, 6075, 6093, 6113, 6133, 6159,
- 6191, 6231, 6279, 6338, 6405, 6477, 6555, 6630, 6704, 6780};
- //冲程:4.2米
- //冲次:3.14次/分钟
- const int dis3[] = {23, 26, 29, 32, 35, 38, 43, 46, 50, 54, 59, 63, 67, 72, 76, 81, 86, 91, 95, 100, 106, 111, 116, 121,
- 127, 132, 137, 143, 148, 154, 159, 165, 170, 175, 181, 186, 192, 197, 203, 208, 213, 219, 224, 230,
- 235, 240, 245, 250, 255, 260, 265, 269, 275, 279, 284, 288, 293, 298, 302, 306, 310, 315, 319, 323,
- 327, 330, 335, 338, 342, 345, 349, 353, 356, 359, 362, 366, 369, 372, 374, 377, 380, 383, 385, 388,
- 390, 392, 395, 397, 399, 401, 403, 404, 406, 408, 409, 411, 412, 413, 414, 415, 416, 417, 418, 419,
- 419, 420, 420, 420, 420, 421, 420, 420, 420, 420, 419, 419, 418, 417, 416, 415, 414, 413, 411, 410,
- 408, 407, 405, 403, 401, 399, 396, 394, 391, 389, 386, 383, 380, 377, 374, 371, 368, 364, 361, 357,
- 354, 350, 346, 342, 338, 334, 330, 326, 321, 317, 312, 308, 303, 299, 294, 289, 284, 280, 275, 270,
- 265, 260, 255, 250, 245, 240, 235, 230, 224, 219, 214, 209, 204, 198, 193, 188, 183, 178, 173, 168,
- 163, 158, 152, 147, 142, 138, 133, 127, 123, 118, 113, 109, 104, 99, 94, 90, 85, 81, 77, 73, 69, 64,
- 60, 56, 53, 49, 45, 42, 38, 35, 32, 29, 26, 23, 21, 18, 16, 14, 12, 10, 8, 6, 5, 4, 2, 2, 1, 0, 0,
- 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 20};
- const int load3[] = {5582, 5579, 5559, 5527, 5488, 5445, 5408, 5377, 5365, 5366, 5381, 5404, 5436, 5475, 5516, 5548,
- 5563, 5561, 5545, 5516, 5481, 5441, 5400, 5361, 5333, 5327, 5332, 5350, 5379, 5415, 5441, 5473,
- 5495, 5500, 5493, 5477, 5450, 5415, 5382, 5348, 5318, 5302, 5304, 5318, 5341, 5374, 5404, 5431,
- 5452, 5466, 5470, 5458, 5434, 5404, 5374, 5341, 5315, 5302, 5298, 5304, 5325, 5354, 5382, 5411,
- 5438, 5457, 5466, 5466, 5449, 5420, 5388, 5354, 5325, 5304, 5293, 5300, 5316, 5343, 5375, 5411,
- 5443, 5470, 5488, 5495, 5484, 5458, 5423, 5381, 5341, 5309, 5291, 5283, 5295, 5313, 5334, 5354,
- 5370, 5377, 5386, 5398, 5409, 5416, 5423, 5424, 5422, 5416, 5409, 5402, 5391, 5379, 5366, 5350,
- 5329, 5306, 5282, 5254, 5229, 5206, 5181, 5156, 5131, 5102, 5068, 5036, 5007, 4966, 4920, 4868,
- 4811, 4756, 4723, 4707, 4708, 4722, 4741, 4761, 4779, 4798, 4809, 4808, 4799, 4784, 4766, 4745,
- 4727, 4711, 4702, 4702, 4709, 4724, 4740, 4761, 4777, 4791, 4797, 4795, 4786, 4775, 4763, 4750,
- 4738, 4727, 4718, 4718, 4724, 4738, 4754, 4773, 4786, 4798, 4804, 4806, 4800, 4790, 4779, 4766,
- 4757, 4752, 4747, 4747, 4750, 4763, 4775, 4791, 4802, 4809, 4813, 4813, 4813, 4808, 4800, 4795,
- 4788, 4784, 4783, 4788, 4791, 4800, 4809, 4818, 4825, 4831, 4834, 4834, 4834, 4829, 4825, 4818,
- 4811, 4809, 4808, 4809, 4813, 4820, 4827, 4838, 4847, 4852, 4858, 4857, 4852, 4845, 4836, 4824,
- 4811, 4802, 4793, 4791, 4793, 4806, 4822, 4838, 4858, 4884, 4909, 4933, 4965, 4999, 5025, 5052,
- 5079, 5106, 5129, 5157, 5193, 5249, 5341, 5425, 5500, 5554};
- //冲程:4.2米
- //冲次:3.04次/分钟
- const int dis4[] = {25, 28, 31, 35, 38, 41, 45, 49, 53, 57, 61, 65, 69, 74, 78, 83, 87, 92, 97, 102, 107, 112, 117, 122,
- 127, 132, 137, 142, 147, 153, 158, 163, 168, 173, 179, 184, 189, 194, 200, 205, 210, 215, 220, 226,
- 230, 235, 240, 245, 250, 255, 260, 264, 269, 274, 278, 283, 287, 292, 296, 300, 304, 308, 312, 316,
- 320, 324, 328, 331, 335, 338, 342, 345, 349, 352, 355, 358, 361, 365, 367, 370, 373, 376, 378, 381,
- 383, 386, 388, 390, 392, 394, 396, 398, 400, 402, 403, 405, 406, 408, 409, 410, 411, 413, 413, 414,
- 415, 416, 416, 417, 417, 417, 417, 417, 417, 417, 417, 417, 416, 415, 415, 414, 413, 412, 411, 409,
- 408, 406, 405, 403, 401, 399, 397, 394, 392, 390, 387, 384, 381, 378, 375, 372, 369, 365, 362, 358,
- 354, 351, 347, 343, 339, 335, 330, 326, 322, 317, 313, 308, 303, 299, 294, 289, 284, 279, 274, 269,
- 264, 259, 254, 249, 244, 238, 233, 228, 223, 217, 212, 207, 201, 196, 191, 186, 180, 175, 170, 165,
- 160, 155, 150, 145, 140, 135, 130, 125, 120, 115, 110, 105, 101, 96, 91, 87, 82, 78, 73, 69, 65, 61,
- 57, 53, 49, 46, 42, 39, 35, 32, 29, 26, 23, 21, 18, 16, 14, 12, 10, 8, 6, 5, 3, 2, 2, 1, 0, 0, 0, 0,
- 0, 0, 0, 1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 20, 22};
- const int load4[] = {7394, 7498, 7608, 7726, 7783, 7791, 7748, 7641, 7483, 7347, 7230, 7119, 7030, 6936, 6883, 6875,
- 6908, 7001, 7130, 7258, 7365, 7457, 7500, 7540, 7540, 7525, 7473, 7398, 7279, 7170, 7065, 6966,
- 6916, 6901, 6894, 6902, 6961, 7013, 7091, 7186, 7276, 7357, 7408, 7423, 7408, 7391, 7344, 7286,
- 7219, 7134, 7044, 6972, 6916, 6900, 6876, 6890, 6923, 6973, 7040, 7172, 7262, 7330, 7383, 7394,
- 7358, 7330, 7280, 7219, 7176, 7102, 7023, 6973, 6932, 6894, 6925, 6970, 7026, 7098, 7162, 7220,
- 7286, 7332, 7369, 7397, 7394, 7355, 7280, 7197, 7122, 7048, 6973, 6948, 6937, 6932, 6940, 6977,
- 7041, 7101, 7148, 7177, 7202, 7183, 7147, 7094, 7051, 6998, 6948, 6908, 6887, 6868, 6847, 6830,
- 6798, 6740, 6691, 6632, 6562, 6498, 6458, 6398, 6336, 6272, 6234, 6236, 6282, 6370, 6472, 6575,
- 6638, 6669, 6677, 6691, 6686, 6650, 6580, 6487, 6384, 6268, 6188, 6163, 6159, 6163, 6190, 6240,
- 6300, 6372, 6447, 6527, 6569, 6580, 6540, 6468, 6404, 6341, 6257, 6181, 6127, 6058, 6004, 5982,
- 6009, 6043, 6066, 6104, 6132, 6143, 6140, 6138, 6104, 6029, 5929, 5827, 5708, 5602, 5532, 5466,
- 5418, 5382, 5359, 5343, 5336, 5318, 5302, 5291, 5295, 5302, 5327, 5366, 5395, 5407, 5418, 5425,
- 5432, 5438, 5450, 5454, 5445, 5425, 5408, 5390, 5386, 5388, 5408, 5422, 5429, 5429, 5443, 5450,
- 5466, 5488, 5509, 5511, 5508, 5502, 5502, 5511, 5516, 5518, 5516, 5502, 5491, 5497, 5515, 5533,
- 5550, 5586, 5622, 5661, 5704, 5754, 5788, 5823, 5850, 5888, 5934, 5984, 6045, 6123, 6197, 6274,
- 6368, 6450, 6529, 6623, 6729, 6829, 6966, 7108, 7248, 7375};
- //冲程:4.2米
- //冲次:3.74次/分钟
- const int dis5[] = {21, 24, 27, 30, 34, 37, 41, 45, 49, 52, 57, 61, 66, 70, 74, 79, 84, 89, 93, 99, 104, 109, 114, 119,
- 125, 130, 135, 141, 147, 152, 157, 163, 168, 174, 179, 184, 189, 195, 201, 206, 211, 217, 222, 228,
- 233, 238, 243, 248, 253, 258, 263, 268, 273, 277, 282, 286, 291, 296, 300, 304, 309, 313, 317, 321,
- 325, 329, 333, 337, 340, 344, 348, 351, 354, 358, 361, 364, 367, 370, 373, 376, 379, 381, 384, 387,
- 389, 391, 393, 396, 398, 400, 402, 404, 405, 407, 409, 410, 412, 413, 414, 415, 416, 417, 418, 418,
- 419, 419, 420, 420, 420, 420, 420, 420, 420, 420, 419, 419, 418, 417, 417, 415, 415, 413, 412, 411,
- 409, 407, 405, 404, 402, 400, 397, 395, 393, 390, 387, 385, 382, 379, 376, 373, 369, 366, 362, 359,
- 355, 352, 347, 344, 340, 336, 332, 327, 323, 319, 315, 310, 305, 301, 296, 292, 286, 282, 277, 272,
- 268, 262, 257, 253, 248, 242, 237, 232, 227, 222, 217, 211, 206, 201, 196, 191, 186, 181, 176, 170,
- 165, 160, 155, 150, 145, 140, 135, 130, 125, 120, 115, 111, 106, 101, 97, 92, 88, 83, 79, 75, 70,
- 67, 62, 58, 55, 51, 47, 44, 40, 37, 34, 30, 28, 25, 22, 19, 17, 15, 13, 11, 9, 7, 5, 4, 3, 2, 1, 0,
- 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 5, 6, 8, 10, 12, 14, 16, 19};
- const int load5[] = {5636, 5641, 5629, 5600, 5561, 5523, 5484, 5450, 5433, 5427, 5438, 5461, 5493, 5529, 5570, 5602,
- 5623, 5627, 5616, 5591, 5561, 5518, 5479, 5443, 5413, 5398, 5402, 5418, 5436, 5468, 5500, 5525,
- 5547, 5559, 5558, 5543, 5525, 5491, 5457, 5425, 5400, 5377, 5375, 5388, 5402, 5429, 5461, 5488,
- 5509, 5525, 5534, 5527, 5511, 5486, 5457, 5425, 5395, 5377, 5368, 5372, 5386, 5415, 5445, 5475,
- 5502, 5523, 5533, 5534, 5524, 5499, 5466, 5433, 5400, 5379, 5368, 5366, 5375, 5398, 5425, 5459,
- 5495, 5525, 5547, 5556, 5549, 5529, 5491, 5449, 5402, 5366, 5340, 5332, 5338, 5357, 5379, 5400,
- 5418, 5429, 5438, 5445, 5459, 5465, 5470, 5475, 5475, 5463, 5452, 5438, 5420, 5399, 5379, 5359,
- 5336, 5309, 5281, 5252, 5224, 5195, 5170, 5141, 5111, 5081, 5045, 5004, 4970, 4934, 4884, 4832,
- 4773, 4713, 4665, 4640, 4633, 4640, 4656, 4674, 4693, 4709, 4724, 4725, 4718, 4706, 4693, 4674,
- 4656, 4640, 4627, 4624, 4627, 4638, 4652, 4670, 4688, 4704, 4718, 4724, 4720, 4711, 4699, 4684,
- 4670, 4656, 4645, 4640, 4643, 4654, 4670, 4684, 4698, 4709, 4718, 4720, 4720, 4713, 4704, 4693,
- 4684, 4677, 4674, 4672, 4674, 4682, 4691, 4704, 4713, 4723, 4729, 4733, 4731, 4727, 4722, 4715,
- 4709, 4704, 4702, 4704, 4709, 4718, 4729, 4740, 4750, 4758, 4765, 4766, 4766, 4761, 4754, 4745,
- 4736, 4732, 4725, 4727, 4731, 4738, 4747, 4757, 4768, 4772, 4777, 4777, 4773, 4765, 4754, 4745,
- 4729, 4716, 4704, 4697, 4695, 4702, 4731, 4766, 4806, 4845, 4890, 4923, 4950, 4984, 5016, 5047,
- 5073, 5100, 5129, 5161, 5199, 5254, 5354, 5450, 5536, 5602};
- class modbustcp {
- public:
- typedef struct _mySocketType {
- int socketID;
- struct sockaddr destAddr;
- } mySocketType;
- /***************************************************************************
- *函数名称:Char2Bcd
- *函数介绍:字符串转BCD
- *输入参数:-buf:指向字符串
- *输出参数:无
- *返回值 :转换后的双精度值
- *修改历史:
- ***************************************************************************/
- static char Char2Bcd(char i_cHexData) {
- char i;
- i = ((i_cHexData % 100) / 10) * 16 + (i_cHexData % 10);
- return i;
- }
- /***************************************************************************
- *函数名称:Get_BCDTime7
- *函数介绍:转换为BCD码格式时间
- *输入参数:-*time:
- *输出参数:无
- *返回值 :无
- *修改历史:
- ***************************************************************************/
- void Get_BCDTime7(time_t i_tTime, char *o_cpBcdTime) {
- struct tm *time;
- time = localtime(&i_tTime);
- o_cpBcdTime[0] = Char2Bcd((time->tm_year + 1900) / 100);
- o_cpBcdTime[1] = Char2Bcd((time->tm_year + 1900) % 100);
- o_cpBcdTime[2] = Char2Bcd(time->tm_mon + 1);
- o_cpBcdTime[3] = Char2Bcd(time->tm_mday);
- o_cpBcdTime[4] = Char2Bcd(time->tm_hour);
- o_cpBcdTime[5] = Char2Bcd(time->tm_min);
- o_cpBcdTime[6] = Char2Bcd(time->tm_sec);
- }
- /***************************************************************************
- *函数名称:Get_CurBCDTime7
- *函数介绍:获取当前BCD码格式时间
- *输入参数:-*time:
- *输出参数:无
- *返回值 :无
- *修改历史:
- ***************************************************************************/
- static void Get_CurBCDTime7(char *o_cpBcdTime) {
- time_t tt;
- struct tm *curtime;
- time(&tt);
- curtime = localtime(&tt);
- o_cpBcdTime[0] = Char2Bcd((curtime->tm_year + 1900) / 100);
- o_cpBcdTime[1] = Char2Bcd((curtime->tm_year + 1900) % 100);
- o_cpBcdTime[2] = Char2Bcd(curtime->tm_mon + 1);
- o_cpBcdTime[3] = Char2Bcd(curtime->tm_mday);
- o_cpBcdTime[4] = Char2Bcd(curtime->tm_hour);
- o_cpBcdTime[5] = Char2Bcd(curtime->tm_min);
- o_cpBcdTime[6] = Char2Bcd(curtime->tm_sec);
- }
- /*******************************************
- * ModbusTCP.c
- * 函数名称: ModBusDatahandler
- *
- * 函数功能: Modbus数据处理函数
- *
- * 入口参数: socket:链接socket句柄
- *
- * 函数返回: 异常代码
- *
- ******************************************/
- static int ModBusDatahandler(mySocketType *socket, char *reqbuff, int startAddr, int readSize, char useNum) {
- char txBuff[500];
- int i = 0;
- int index = 0;
- char l_arrBcdtime[LEN] = {0};
- int PDU_Length = 0;
- int TotalLenth = 0;
- int chongCi = 0;
- int chongCheng = 0;
- Get_CurBCDTime7(l_arrBcdtime);
- HoldRigster[983] = 250;
- HoldRigster[984] = l_arrBcdtime[0] * 256 + l_arrBcdtime[1];
- HoldRigster[985] = l_arrBcdtime[2];
- HoldRigster[986] = l_arrBcdtime[3];
- HoldRigster[987] = l_arrBcdtime[4];
- HoldRigster[988] = l_arrBcdtime[5];
- HoldRigster[989] = l_arrBcdtime[6];
- if (useNum == 0) {
- chongCheng = (int) (420);
- chongCi = (int) ((60 / 3.34f) * 1000);
- /* 冲次数据 */
- HoldRigster[990] = chongCi;
- HoldRigster[992] = chongCheng;
- for (i = 0; i < 250; i++) {
- HoldRigster[1000 + i] = dis1[i];
- }
- for (i = 0; i < 250; i++) {
- HoldRigster[1250 + i] = load1[i];
- }
- } else if (useNum == 1) {
- chongCheng = (int) (420);
- chongCi = (int) ((60 / 3.24f) * 1000);
- /* 冲次数据 */
- HoldRigster[990] = chongCi;
- HoldRigster[992] = chongCheng;
- for (i = 0; i < 250; i++) {
- HoldRigster[1000 + i] = dis2[i];
- }
- for (i = 0; i < 250; i++) {
- HoldRigster[1250 + i] = load2[i];
- }
- } else if (useNum == 2) {
- chongCheng = (int) (420);
- chongCi = (int) ((60 / 3.14f) * 1000);
- /* 冲次数据 */
- HoldRigster[990] = chongCi;
- HoldRigster[992] = chongCheng;
- for (i = 0; i < 250; i++) {
- HoldRigster[1000 + i] = dis3[i];
- }
- for (i = 0; i < 250; i++) {
- HoldRigster[1250 + i] = load3[i];
- }
- } else if (useNum == 3) {
- chongCheng = (int) (420);
- chongCi = (int) ((60 / 3.04f) * 1000);
- /* 冲次数据 */
- HoldRigster[990] = chongCi;
- HoldRigster[992] = chongCheng;
- for (i = 0; i < 250; i++) {
- HoldRigster[1000 + i] = dis4[i];
- }
- for (i = 0; i < 250; i++) {
- HoldRigster[1250 + i] = load4[i];
- }
- } else if (useNum == 4) {
- chongCheng = (int) (420);
- chongCi = (int) ((60 / 3.74f) * 1000);
- /* 冲次数据 */
- HoldRigster[990] = chongCi;
- HoldRigster[992] = chongCheng;
- for (i = 0; i < 250; i++) {
- HoldRigster[1000 + i] = dis5[i];
- }
- for (i = 0; i < 250; i++) {
- HoldRigster[1250 + i] = load5[i];
- }
- }
- PDU_Length = readSize * 2 + 3;
- /* 填充包头数据 */
- txBuff[0] = reqbuff[0];
- txBuff[1] = reqbuff[1];
- txBuff[2] = reqbuff[2];
- txBuff[3] = reqbuff[3];
- txBuff[4] = ((PDU_Length >> 8) & 0XFF);
- txBuff[5] = ((PDU_Length >> 0) & 0XFF);
- txBuff[6] = reqbuff[6];
- txBuff[7] = reqbuff[7];
- txBuff[8] = readSize * 2;
- index = 0;
- for (i = startAddr; i < startAddr + readSize; i++) {
- txBuff[index * 2 + 9] = ((HoldRigster[i] >> 8) & 0XFF);
- txBuff[index * 2 + 10] = ((HoldRigster[i] >> 0) & 0XFF);
- index++;
- }
- printf("\r\n\r\n↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓\r\n");
- printf("\r\nSocket ID is %d \r\n", socket->socketID);
- printf("Start Address is %d \r\n", startAddr + 1);
- printf("Read Rigster Num is %d \r\n", readSize);
- printf("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑\r\n");
- TotalLenth = 9 + readSize * 2;
- socklen_t peer_addr_len;
- peer_addr_len = sizeof(struct sockaddr_storage);
- printf("\r\nSocket TX : ");
- for (i = 0; i < TotalLenth; i++) {
- printf("%02X ", txBuff[i]);
- }
- printf("\r\n");
- sendto(socket->socketID, txBuff, TotalLenth, 0, &socket->destAddr, peer_addr_len);
- }
- /*******************************************
- * ModbusTCP.c
- * 函数名称: socketHandle
- *
- * 函数功能: 子线程处理socket链接函数
- *
- * 入口参数: socket:链接socket句柄
- *
- * 函数返回: 异常代码
- *
- ******************************************/
- static void *socketHandle(void *socket) {
- modbustcp::mySocketType* s_t=(mySocketType *) socket;
- mySocketType socket_t = *(mySocketType *) socket;
- static int count = 0;
- char rcvBuff[2048] = {0};
- int rcvSocket = socket_t.socketID;
- int getNum = 0;
- int i = 0;
- while (1) {
- getNum = recv(rcvSocket, rcvBuff, 2048, 0);
- printf("\r\nSocket RX : ");
- for (i = 0; i < getNum; i++) {
- printf("%02X ", rcvBuff[i]);
- }
- printf("\r\n");
- if (getNum == 0) return NULL;
- else {
- /* 判断是不是有效的请求数据 */
- if ((getNum != 12) || (rcvBuff[0] != 0X30) || (rcvBuff[1] != 0X30) || (rcvBuff[5] != 0X06)) {
- printf("ERROR");
- return NULL;
- } else {
- printf("OK");
- switch (rcvBuff[7]) {
- case 0X01: /* 读取线圈输出参数; 地址范围:00001-09999*/
- printf("0X01");
- break;
- case 0X02: /* 读取输入线圈; 地址范围:10001-19999*/
- printf("0X02");
- break;
- case 0X03: /* 读取线保持寄存器; 地址范围:40001-49999*/
- ModBusDatahandler( s_t, rcvBuff, rcvBuff[8] * 256 + rcvBuff[9],
- rcvBuff[10] * 256 + rcvBuff[11], count % 4);
- count++;
- break;
- case 0X04: /* 读取输入寄存器; 地址范围:30001-39999*/
- printf("0X04");
- break;
- default:
- printf("ERROR");
- break;
- }
- }
- }
- }
- }
- /*******************************************
- * ModbusTCP.c
- * 函数名称: main
- *
- * 函数功能: 主函数
- *
- * 入口参数: 无
- *
- * 函数返回: 异常代码
- *
- ******************************************/
- };
- int mainsss(int args, char **argv) {
- time_t nowtime = time(NULL);
- modbustcp::mySocketType socket_t;
- pthread_t pid;
- int clienSocket = 0;
- socklen_t peer_addr_size;
- int serverSocket = 0;
- struct sockaddr_in *clientaddr;
- struct sockaddr_in servaddr;
- //struct sockaddr_un
- struct sockaddr_un ClienIP;
- clientaddr = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in));
- printf("\r\n\r\n\r\n\r\n\r\n********************ModbusTCP Thread********************\r\n");
- serverSocket = socket(PF_INET, SOCK_STREAM, 0);
- if (serverSocket < 0) {
- nowtime = time(NULL);
- printf("%s:Creat socket faild", asctime(localtime(&nowtime)));
- return -1;
- }
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY); // IP地址设置成INADDR_ANY,让系统自动获取本机的IP地址。
- servaddr.sin_port = htons(502); // 设置的端口为DEFAULT_PORT
- //将本地地址绑定到所创建的套接字上
- if (bind(serverSocket, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1) {
- nowtime = time(NULL);
- printf("%sbind socket error: %s(errno: %d)\n", asctime(localtime(&nowtime)), strerror(errno), errno);
- return -1;
- }
- if (listen(serverSocket, 5) < 0) {
- nowtime = time(NULL);
- printf("%s Listen socket error: %s(errno: %d)\n", asctime(localtime(&nowtime)), strerror(errno), errno);
- return -1;
- }
- nowtime = time(NULL);
- printf("\r\nInitialize Finish Threadr\n");
- printf("\r\nWatting For Connectionr\n");
- while (1) {
- if ((clienSocket = accept(serverSocket, (struct sockaddr *) &ClienIP, &peer_addr_size)) == -1)
- continue;
- else {
- printf("\r\nConnected socket id is %d\r\n", clienSocket);
- socket_t.socketID = clienSocket;
- socket_t.destAddr = *(struct sockaddr *) &ClienIP;
- if (pthread_create(&pid, NULL,modbustcp:: socketHandle, &socket_t) < 0) {
- printf("creat thread faild\r\n");
- }
- }
- }
- }
|