public static CommDataBlock createHelloBlock() { var bytes = new byte[] { 0x01, 0x05, 0x04 }; var block = new CommDataBlock() { content_bytes = bytes }; return(block); }
public static CommDataBlock createEndBlock(uint task_id) { var bytes = new byte[7]; bytes[0] = 0x01; bytes[5] = 0x07; bytes[6] = 0x04; var block = new CommDataBlock() { content_bytes = bytes }; block.setTaskId(task_id); return(block); }
public static CommDataBlock createAckBlock(uint task_id, uint block_id) { var bytes = new byte[11]; bytes[0] = 0x01; bytes[9] = 0x06; bytes[10] = 0x04; var block = new CommDataBlock() { content_bytes = bytes }; block.setTaskId(task_id); block.setBlockId(block_id); return(block); }
public static void sendData(string data_content) { try { if (data_content.isNull()) { return; } if (sending_data == null) { sending_data = new List <CommDataBlock>(); } var dc = new CommDataContent(data_content); foreach (var db in dc.data_blocks) { sending_data.Add(db.Value); } sending_data.Add(CommDataBlock.createEndBlock(dc.task_id)); } catch { } }
public static CommDataBlock createDataBlock(uint task_id, uint block_id, byte[] data) { if (data == null || data.Length <= 0) { return(null); } var bytes = new byte[data.Length + 12]; bytes[0] = 0x01; bytes[9] = 0x02; Array.Copy(data, 0, bytes, 10, data.Length); bytes[bytes.Length - 2] = 0x03; bytes[bytes.Length - 1] = 0x04; var block = new CommDataBlock() { content_bytes = bytes }; block.setTaskId(task_id); block.setBlockId(block_id); return(block); }
public static bool checkConnection() { try { if (sending_data == null) { sending_data = new List <CommDataBlock>(); } sending_data.Add(CommDataBlock.createHelloBlock()); var check = 0; ack_status[0] = false; while (check < 60) { Thread.Sleep(50); check++; if (ack_status.ContainsKey(0) && ack_status[0]) { return(true); } } } catch { } return(false); }
private static void processReceivedData(byte[] data) { try { OnDataEndPointReached?.Invoke("", data); var start_index = -1; for (int i = data.Length - 2; i >= 0; i--) { if (data[i] == 0x01) { start_index = i; break; } } byte[] buffer; if (start_index >= 0) { buffer = new byte[data.Length - start_index]; Array.Copy(data, start_index, buffer, 0, buffer.Length); } else { buffer = new byte[data.Length + 1]; buffer[0] = 0x01; Array.Copy(data, 0, buffer, 1, data.Length); } var db = new CommDataBlock() { content_bytes = buffer }; if (db.isAvailableHello()) { OnDataBlockReceived?.Invoke("", db); sending_acks.Add(CommDataBlock.createAckBlock(0, 0)); } else if (db.isAvailableEnd()) //传输结束, 上报全部收取内容 { OnDataBlockReceived?.Invoke("", db); var task_id = db.getTaskId(); if (current_ack_mode) { sending_acks.Add(CommDataBlock.createAckBlock(task_id, 0)); } raiseTask(task_id); } else if (db.isAvailableAck()) //收到确认包 { OnDataBlockReceived?.Invoke("", db); ack_status[db.getTaskId()] = true; } else if (db.isAvailableData()) //收到数据包 { OnDataBlockReceived?.Invoke("", db); received_blocks.Add(db); if (db.content_bytes.Length < CommDataContent.DATA_BLOCK_SIZE) {//在收到的数据包长度小于最大长度值时, 已可以确认收到了全部的内容, 直接调起当前任务 raiseTask(db.getTaskId()); } if (current_ack_mode) { sending_acks.Add(CommDataBlock.createAckBlock(db.getTaskId(), db.getBlockId())); } } } catch { } }