/// <summary> /// Command code 41: Control RUN/STOP of PLC. /// </summary> /// <param name="slaveStationNo">Slave Station No</param> /// <param name="status">Control Code, 1: RUN, 0: STOP</param> /// <returns>string</returns> public bool SetPLCMode(int slaveStationNo, PLCMode status) { bool result = false; try { string frame = this.GetMessageOfControlPLC(slaveStationNo, CommandCode.CONTROL_RUN_STOP, status); objSerialPort.WriteLine(frame); // Read SerialPort int sizeOfMessageReceiver = 9; string buffReceiver = string.Empty; DateTime startDateTime = DateTime.Now; do { this.CheckBufferReceiver(buffReceiver); buffReceiver = objSerialPort.ReadExisting(); System.Threading.Thread.Sleep(100); } while (buffReceiver.Length < sizeOfMessageReceiver && (DateTime.Now.Subtract(startDateTime).TotalMilliseconds < objSerialPort.ReadTimeout)); string errorMsg = this.GetException(buffReceiver); if (!string.IsNullOrEmpty(errorMsg) || !string.IsNullOrWhiteSpace(errorMsg)) { throw new Exception(errorMsg); } } catch (Exception ex) { throw ex; } return(result); }
/// <summary> /// Command code 41: Control RUN/STOP of PLC /// </summary> /// <param name="slaveStationNo">Slave Station No</param> /// <param name="cmmdCode">Command Code</param> /// <param name="status">Control Code, 1: RUN, 0: STOP</param> /// <returns>string</returns> public string GetMessageOfControlPLC(int slaveStationNo, CommandCode cmmdCode, PLCMode status) { string frame = string.Format("{0:X2}", slaveStationNo); // Slave station no. frame += string.Format("{0:X2}", (byte)cmmdCode); // Command no. frame += string.Format("{0}", status == PLCMode.RUN ? 1 : 0); // data. frame += CheckSum(frame); return(Convert.ToChar(STX) + frame + Convert.ToChar(EXT)); }
private void btnGetPLCStatus_Click(object sender, EventArgs e) { try { PLCMode result = objFatekCommunication.GetPLCStatus(1); txtResult.AppendText(string.Format("PLC MODE: {0} \r\n", result)); txtResult.AppendText(Environment.NewLine); txtResult.ScrollToCaret(); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Command code 40: The gist read the system status of PLC. /// </summary> /// <param name="slaveStationNo">Slave Station No</param> /// <returns>bool</returns> public PLCMode GetPLCStatus(int slaveStationNo) { PLCMode result = PLCMode.STOP; try { string frame = this.GetMessageOfPLCStatus(slaveStationNo, CommandCode.PLC_STATUS); objSerialPort.WriteLine(frame); // Read SerialPort int sizeOfMessageReceiver = 15; string buffReceiver = string.Empty; DateTime startDateTime = DateTime.Now; do { this.CheckBufferReceiver(buffReceiver); buffReceiver = objSerialPort.ReadExisting(); System.Threading.Thread.Sleep(100); } while (buffReceiver.Length < sizeOfMessageReceiver && (DateTime.Now.Subtract(startDateTime).TotalMilliseconds < objSerialPort.ReadTimeout)); string errorMsg = this.GetException(buffReceiver); if (!string.IsNullOrEmpty(errorMsg) || !string.IsNullOrWhiteSpace(errorMsg)) { throw new Exception(errorMsg); } string dataReceiver = buffReceiver.Substring(6, 2); // STATUS 1. ushort plcStatus = Conversion.HEXToWORD(dataReceiver); result = (PLCMode)plcStatus; //Console.WriteLine("PLC STATUS = " + dataReceiver); } catch (Exception ex) { throw ex; } return(result); }
/// <summary> /// FATEK PLC Information /// </summary> /// <param name="slaveStationNo">Slave Station No</param> /// <returns>FATEK PLC Information</returns> public string GetPLCInfo(int slaveStationNo) { StringBuilder result = new StringBuilder(); try { string frame = this.GetMessageOfPLCStatus(slaveStationNo, CommandCode.PLC_STATUS); objSerialPort.WriteLine(frame); // Read SerialPort int sizeOfMessageReceiver = 15; string buffReceiver = string.Empty; DateTime startDateTime = DateTime.Now; do { this.CheckBufferReceiver(buffReceiver); buffReceiver = objSerialPort.ReadExisting(); System.Threading.Thread.Sleep(100); } while (buffReceiver.Length < sizeOfMessageReceiver && (DateTime.Now.Subtract(startDateTime).TotalMilliseconds < objSerialPort.ReadTimeout)); string errorMsg = this.GetException(buffReceiver); if (!string.IsNullOrEmpty(errorMsg) || !string.IsNullOrWhiteSpace(errorMsg)) { throw new Exception(errorMsg); } string dataReceiver = buffReceiver.Substring(6, 2); // STATUS 1. ushort plcStatus = Conversion.HEXToWORD(dataReceiver); result.AppendLine("FATEK PLC COMMUNICATION PROTOCOL VIA SERIALPORT"); result.AppendLine("==============================================================="); PLCMode objPLCMode = (PLCMode)plcStatus; result.AppendLine(string.Format("B0. PLC Mode: {0}", objPLCMode)); LADDER_CHECKSUM ladder = (LADDER_CHECKSUM)((plcStatus & (ushort)Math.Pow(2, 2)) >> 2); result.AppendLine(string.Format("B2. Ladder Checksum: {0}", ladder)); USE_ROM_PACK_OR_NOT_USE objUseRomPack = (USE_ROM_PACK_OR_NOT_USE)((plcStatus & (ushort)Math.Pow(2, 3)) >> 3); result.AppendLine(string.Format("B3. Use ROM Pack: {0}", objUseRomPack)); WDT_TIMEOUT_OR_NORMAL objWDTTimeout = (WDT_TIMEOUT_OR_NORMAL)((plcStatus & (ushort)Math.Pow(2, 4)) >> 4); result.AppendLine(string.Format("B4. WDT Timeout: {0}", objWDTTimeout)); SET_ID_OR_NOT_SET_ID objSetID = (SET_ID_OR_NOT_SET_ID)((plcStatus & (ushort)Math.Pow(2, 5)) >> 5); result.AppendLine(string.Format("B5. Set ID: {0}", objSetID)); EMERGENCY_STOP_OR_NORMAL objEmergencyStop = (EMERGENCY_STOP_OR_NORMAL)((plcStatus & (ushort)Math.Pow(2, 6)) >> 6); result.AppendLine(string.Format("B6. Emergency Stop: {0}", objEmergencyStop)); //RESERVE FOR FUTURE result.AppendLine("B7. Reserver For Future"); result.AppendLine("==============================================================="); result.AppendLine("Designed By Industrial Networks"); result.AppendLine("Skype: katllu"); result.AppendLine("Mobile: (+84) 909.886.483"); result.AppendLine("E-mail: [email protected]"); result.AppendLine("Youtube: https://www.youtube.com/industrialnetworks"); result.AppendLine(); } catch (Exception ex) { throw ex; } return(result.ToString()); }