Contoh Code Master Modbus RTU dengan C# (FC03)

Master Modbus RTU untuk  memberi perintah  Slave Modbus RTU  khususnya untuk function Code 03 , yaitu untuk membaca data register slave RTU microcontroller pada artikel sebelumya.

Tampilan Menu Utama

Modbus Master  untuk function code 03 dan 04

Kode program dengan C#:

//sebelumnya anda drag and drop komponen serial comm ke form dan ganti namanya menjadi  serialPort1.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SimpleSerial
{
    public partial class Form1 : Form
    {

      
        string RxString_Respon_dari_slave;
private void buttonStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM1";
            serialPort1.BaudRate = 9600;

            serialPort1.Open();
            if (serialPort1.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
                textBox1.ReadOnly = false;
            }
        }
 private void buttonStop_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;
                textBox1.ReadOnly = true;
            }

        }
private void button2_Click(object sender, EventArgs e)
        {

            
            byte[] CRC = new byte[2];
            byte[] message = new byte[8];
            ushort start = Convert.ToUInt16(textAlamatAwal.Text);
            ushort reg_qty = Convert.ToUInt16(textJumlahRegister.Text);
            // jika port belum di open , keluar
            if (!serialPort1.IsOpen) return;
            //Clear in/out buffers:
            serialPort1.DiscardOutBuffer();
            serialPort1.DiscardInBuffer();

            message[0] = Convert.ToByte(txtalamat.Text);
            message[1] = Convert.ToByte(textFunctionCode.Text); 

            message[2] = (byte)(start >> 8);
            message[3] = (byte)start;

            message[4] = (byte)(reg_qty >> 8);
            message[5] = (byte)reg_qty;

            HitungCRC(message, ref CRC);
            message[message.Length-2] = CRC[0];
            message[message.Length-1] = CRC[1];
        //tampilkan hasil CRC ke textbox
            textCRC.Text = CRC[1].ToString() + " " + CRC[0].ToString();
            textCRCHigh.Text = CRC[1].ToString();
            textCRCLow.Text = CRC[0].ToString();
            //kirim data di buffer message ke serial port
            serialPort1.Write(message, 0, message.Length);

        }
//tampilkan data respon dari slave
 private void DisplayText(object sender, EventArgs e)
        {
textBox1.AppendText(RxString_Respon_dari_slave);

        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString_Respon_dari_slave = serialPort1.ReadExisting();

            this.Invoke(new EventHandler(DisplayText));
        }

 private void HitungCRC(byte[] message, ref byte[] CRC)
        {
           

            ushort CRCFull = 0xFFFF;
            byte CRCHigh = 0xFF, CRCLow = 0xFF;
            char CRCLSB;

            for (int i = 0; i < (message.Length) - 2; i++)
            {
                CRCFull = (ushort)(CRCFull ^ message[i]);

                for (int j = 0; j < 8; j++)
                {
                    CRCLSB = (char)(CRCFull & 0x0001);
                    CRCFull = (ushort)((CRCFull >> 1) & 0x7FFF);

                    if (CRCLSB == 1)
                        CRCFull = (ushort)(CRCFull ^ 0xA001);
                }
            }
            CRC[1] = CRCHigh = (byte)((CRCFull >> 8) & 0xFF);
            CRC[0] = CRCLow = (byte)(CRCFull & 0xFF);
        }
Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis untuk pemula.

Posted on 04/08/2011, in Contoh-contoh Aplikasi. Bookmark the permalink. 3 Comments.

  1. S7 PLC SIEMENS WITH VISUAL C#(S7.Net Library)

    Open Source C#: The Industrial Networks Library

    Programming Modbus ASCII with .Net (C#/VB.Net)

    Programming Modbus TCP/IP with .Net (C#/VB.Net)

    S7-1200 Modbus TCP/IP With Visual C#

    Programming Modbus RTU with .Net (C#/VB.Net)

  2. kereen bugn…..

  3. gan punya source code buat master jg tp pke mikro ga? mohon bantuannya

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: