// Function code changed event public void SelectindechangedfunctionCode(object sender, EventArgs e) { _functionCode = comboBoxFunctionCode.SelectedItem.ToString().Split(' ')[0]; switch (comboBoxFunctionCode.SelectedIndex) { case 0: _datatype = "LED"; groupBoxDatatype.Enabled = false; groupBoxFormat.Enabled = false; buttonWriteMultiRegister.Enabled = true; buttonWriteMultiRegister.Enabled = true; break; case 1: _functionCode = "2"; _datatype = "LED"; groupBoxDatatype.Enabled = false; groupBoxFormat.Enabled = false; buttonWriteMultiRegister.Enabled = false; break; case 2: groupBoxDatatype.Enabled = true; groupBoxFormat.Enabled = true; if (_datatype.Equals("LED")) { _datatype = "Float"; groupBoxDatatype.Enabled = true; comboBoxFormat.SelectedIndex = 1; } buttonWriteMultiRegister.Enabled = true; break; case 3: groupBoxDatatype.Enabled = true; groupBoxFormat.Enabled = true; buttonWriteMultiRegister.Enabled = false; if (_datatype.Equals("LED")) { _datatype = "Float"; comboBoxDatatype.SelectedIndex = 4; comboBoxFormat.SelectedIndex = 1; } break; } formBaseDataTable.changeFunctionCode(_functionCode); formBaseDataTable.changeDataType(_datatype); formBaseDataTable.UpdateData(); }
public bool receivedData(byte[] recBuf) { try { ushort receivedStartAddress, receivedTotalRegister, receivedFunctionCode; int temp, receiveFrameSize = 0, recBufFunctionCode = 0; var result = checkError(recBuf); if (result) { return(false); //if exception is received. } temp = tcpBaseDataTable.baseTotalRegister; recBufFunctionCode = (int)recBuf[7]; //comparing send and receive function code if (recBufFunctionCode > 4 && recBufFunctionCode == tcpBaseDataTable.baseWriteFunctionCode) { receiveFrameSize = 3; } else if (recBufFunctionCode < 5 && recBufFunctionCode == int.Parse(tcpBaseDataTable.baseFunctionCode)) { if (tcpBaseDataTable.baseFunctionCode.Contains("1") || tcpBaseDataTable.baseFunctionCode.Contains("2")) { receiveFrameSize = temp / 8; if (temp % 8 != 0) { receiveFrameSize += 1; } } else if (tcpBaseDataTable.baseFunctionCode.Contains("3") || tcpBaseDataTable.baseFunctionCode.Contains("4")) { receiveFrameSize = temp * 2; } } else { f.AppendLog("Exception: Requested Function code does note match with received function code."); return(false); } receiveFrameSize += 9; if (receiveFrameSize != recBuf.Length) { f.AppendLog("Incomplete Data"); return(false); } receivedFunctionCode = recBuf[7]; //this is wrong receivedStartAddress = (ushort)tcpBaseDataTable.baseStartAddress; receivedTotalRegister = (ushort)tcpBaseDataTable.baseTotalRegister; //if ((receivedStartAddress < tcpBaseDataTable.baseStartAddress) || (receivedStartAddress > (tcpBaseDataTable.baseStartAddress + tcpBaseDataTable.baseTotalRegister))) //{ // MessageBox.Show(pa) //} //var controls = f.panel1.Controls.OfType<TextBox>().AsEnumerable<TextBox>(); //IEnumerable<TextBox> textBoxes = // from control in controls // where ((int.Parse(control.Name) > receivedStartAddress) && (int.Parse(control.Name) <= (receivedStartAddress + receivedTotalRegister))) // select control; switch (receivedFunctionCode) { case 1: readMultipleCoilStatus(recBuf); break; case 2: readMultipleCoilStatus(recBuf); break; case 3: readMultipleRegister(recBuf); break; case 4: readMultipleRegister(recBuf); break; case 5: //MessageBox.Show("Write Single Coil"); break; case 6: //MessageBox.Show("Write Single Register"); break; case 15: //MessageBox.Show("Write Multi Coil"); break; case 16: //MessageBox.Show("Write Multi Register"); break; //default: // sendByte = new byte[9]; // Array.Copy(recBuf, 0, sendByte, 0, 7); // sendByte[7] = 131; // sendByte[8] = (byte)1; // break } tcpBaseDataTable.UpdateData(); } catch (Exception e) { MessageBox.Show("Exception while receiving data"); return(false); // not received correct data . } return(true); }
public bool ReceivedData(byte[] recBuf) { try { ushort receivedStartAddress, receivedTotalRegister, receivedFunctionCode; int temp, receiveFrameSize = 0, recBufFunctionCode = 0;; var result = CheckError(recBuf); if (result) { return(false); //if exception is received. } temp = serialBaseDataTable.baseTotalRegister; recBufFunctionCode = (int)recBuf[1]; // Checking received bytes frame size if (recBufFunctionCode > 4 && recBufFunctionCode == serialBaseDataTable.baseWriteFunctionCode) { receiveFrameSize = 3; } else if (recBufFunctionCode < 5 && recBufFunctionCode == int.Parse(serialBaseDataTable.baseFunctionCode)) { if (serialBaseDataTable.baseFunctionCode.Equals("01") || serialBaseDataTable.baseFunctionCode.Equals("02")) { receiveFrameSize = temp / 8; if (temp % 8 != 0) { receiveFrameSize += 1; } } else if (serialBaseDataTable.baseFunctionCode.Equals("03") || serialBaseDataTable.baseFunctionCode.Equals("04")) { receiveFrameSize = temp * 2; } else { f.AppendLog("Exception: Requested Function code does note match with received function code."); return(false); } } receiveFrameSize += 5; if (receiveFrameSize != recBuf.Length) { f.AppendLog("Incomplete Data"); return(false); } receivedFunctionCode = recBuf[1]; //this is wrong receivedStartAddress = (ushort)serialBaseDataTable.baseStartAddress; receivedTotalRegister = (ushort)serialBaseDataTable.baseTotalRegister; switch (receivedFunctionCode) { case 1: ReadMultipleCoilStatus(recBuf); break; case 2: ReadMultipleCoilStatus(recBuf); break; case 3: ReadMultipleRegister(recBuf); break; case 4: ReadMultipleRegister(recBuf); break; case 5: break; case 6: break; case 15: break; case 16: break; } serialBaseDataTable.UpdateData(); } catch (Exception e) { f.AppendLog("Exception while receiving data"); return(false); // not received correct data . } return(true); }