Translate

Komunikasi RS485 Arduino 2 Master 1 Slave Nurse Call Interface Visual Basic 6.0

Komunikasi RS485 Arduino 2 Master 1 Slave Nurse Call Interface Visual Basic 6.0


        Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat nurse call dengan komunikasi 2 master dan 1 slave dengan menggunakan RS485 Arduino. alat ini menggunakan interface VB 6.0 dengan tampilan berupa ruang 1 dan 2 yang mana terdapat 2 perintah yaitu warna merah dan biru. untuk lebih jelasnya berikut adalah koding dan skemanya.


1. Skema Master 1 dan 2



2. Skema Slave



3. Program Arduino Master 1

#include <ModbusRtu.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);

#define slaveID 11

int tombol1 = 10;
int tombol2 = 11;
int tombol1x;
int tombol2x;

uint16_t nilainya = 0;
unsigned long lastPrint = 0;

// data array for modbus network sharing
uint16_t au16data[4] = {
  slaveID, 225, 8888, 9999
};

Modbus slave(slaveID, mySerial, 4); // this is slave @1 and RS-232 or USB-FTDI

void setup() {
  Serial.begin(9600);
  mySerial.begin( 19200 ); // baud-rate at 19200
  slave.start();
  delay(10);
  pinMode(tombol1,INPUT_PULLUP);
  pinMode(tombol2,INPUT_PULLUP);
}

void loop() {
  slave.poll( au16data, 4 );
  if (millis() - lastPrint > 200) {
    Serial.print(au16data[0]); Serial.print(":");
    Serial.print(au16data[1]); Serial.print(":");
    Serial.print(au16data[2]); Serial.println();
    lastPrint = millis();
  }
  readSensor(); //for ultrasonic sensor
}

void readSensor() {
  tombol1x = digitalRead(tombol1);
  tombol2x = digitalRead(tombol2);

  if((tombol1x == 1)&&(tombol2x == 1)){
    nilainya = 0;
   }
   
  if(tombol1x == 0){
    nilainya = 1;
   }

  if(tombol2x == 0){
    nilainya = 2;
   }

  au16data[1] = nilainya; //data to be sent to slave device
}



4. Program Aduino Master 2

#include <ModbusRtu.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);

#define slaveID 12

int tombol1 = 10;
int tombol2 = 11;
int tombol1x;
int tombol2x;

uint16_t nilainya = 0;
unsigned long lastPrint = 0;

// data array for modbus network sharing
uint16_t au16data[4] = {
  slaveID, 225, 8888, 9999
};

Modbus slave(slaveID, mySerial, 4); // this is slave @1 and RS-232 or USB-FTDI

void setup() {
  Serial.begin(9600);
  mySerial.begin(19200); // baud-rate at 19200
  slave.start();
  delay(10);
  pinMode(tombol1,INPUT_PULLUP);
  pinMode(tombol2,INPUT_PULLUP);
}

void loop() {
  slave.poll( au16data, 4 );
  if (millis() - lastPrint > 200) {
    Serial.print(au16data[0]); Serial.print(":");
    Serial.print(au16data[1]); Serial.print(":");
    Serial.print(au16data[2]); Serial.println();
    lastPrint = millis();
  }
  readSensor(); //for ultrasonic sensor
}

void readSensor() {
  tombol1x = digitalRead(tombol1);
  tombol2x = digitalRead(tombol2);

  if((tombol1x == 1)&&(tombol2x == 1)){
    nilainya = 3;
   }
   
  if(tombol1x == 0){
    nilainya = 4;
   }

  if(tombol2x == 0){
    nilainya = 5;
   }
   
  au16data[1] = nilainya; //data to be sent to slave device
}


5. Program Arduino Slave

// modbus RS485 read and write register
// V1.1 
// 1 master 2 slave, 
// 1 slave : 1 input 1 output

#include <Wire.h>
#include <ModbusRtu.h>
#define slaveNumber 5
#define delayCom 15
#define maxQuery 2*2//slaveNumer*2
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

int ruang1;
int ruang2;

uint8_t u8state; //!< machine state
uint8_t u8query; //!< pointer to message query

uint16_t dataBus[slaveNumber*3];
uint16_t lastPrint=100;
int slaveID[slaveNumber] = {11,12,13,14,15}; //id = 0,1,2,3,4
/**
 *  Modbus object declaration
 *  u8id : node id = 0 for master, = 1..247 for slave
 *  port : Serial1 port
 *  u8txenpin : 0 for RS-232 and USB-FTDI 
 *               or any pin number > 1 for RS-485
 */
Modbus master(0,mySerial,4); // ID, seriapNumber, enablePin

/**
 * This is an structe which contains a query to an slave device
 */
modbus_t telegram[slaveNumber*2];

unsigned long u32wait;

void init_modBus(){
  int num=0;
  int addr=0;
////SLAVE 1
  // Read 1 data from Slave 11
  telegram[num].u8id = slaveID[0]; // slave address
  telegram[num].u8fct = 3; // function code (this one is registers read)
  telegram[num].u16RegAdd = 0; // start address in slave
  telegram[num].u16CoilsNo = 2; // number of elements (coils or registers) to read
  telegram[num].au16reg = dataBus; // pointer to a memory array in the Arduino
  num+=1;
  addr+=2;
  
  // Write 1 data to Slave 11
  telegram[num].u8id = slaveID[0]; // slave address
  telegram[num].u8fct = 6; // function code (this one is write a multiple register)
  telegram[num].u16RegAdd = 2; // start address in slave
  telegram[num].u16CoilsNo = 1; // number of elements (coils or registers) to write
  telegram[num].au16reg = dataBus+2; // pointer to a memory array in the Arduino
  num+=1;
  addr+=1;

  ////SLAVE 2
  // Read 1 data from Slave 12
  telegram[num].u8id = slaveID[1]; // slave address
  telegram[num].u8fct = 3; // function code (this one is registers read)
  telegram[num].u16RegAdd = 0; // start address in slave
  telegram[num].u16CoilsNo = 2; // number of elements (coils or registers) to read
  telegram[num].au16reg = dataBus; // pointer to a memory array in the Arduino
  num+=1;
  addr+=2;
  
  // Write 1 data to Slave 12
  telegram[num].u8id = slaveID[1]; // slave address
  telegram[num].u8fct = 6; // function code (this one is write a multiple register)
  telegram[num].u16RegAdd = 2; // start address in slave
  telegram[num].u16CoilsNo = 1; // number of elements (coils or registers) to write
  telegram[num].au16reg = dataBus+2; // pointer to a memory array in the Arduino
  num+=1;
  addr+=1;

  master.start();
  master.setTimeOut( 100 ); // if there is no answer in 100 ms, roll over
  u32wait = millis() + 40;
  u8state = u8query = 0; 
  
}

void rtuState(){
  switch( u8state ) {
  case 0: 
    if (millis() >= u32wait) u8state++; // wait state
    break;
  case 1: 
    master.query( telegram[u8query] ); // send query (only once)
    u8state++;
    u8query++;
    if (u8query >= maxQuery) 
      u8query = 0;
    break;
  case 2:
    master.poll(); // check incoming messages if communication in idle state
    if (master.getState() == COM_IDLE) {
      u8state = 0;
      u32wait = millis() + delayCom;  //delay for next state
    }
    break;
  }
}

void printData(){
  if (millis() - lastPrint>200){
    //print data to validate
    //Serial.print(dataBus[0]); Serial.print(":");
    //Serial.print(dataBus[1]); Serial.print(":");
    //Serial.print(dataBus[2]); Serial.print("\t:\t");
    //Serial.println();
  }
}

void processData(){
  //data to be sent so slave 1 based on slave 2 data
  dataBus[2] = dataBus[4]*0.25; //convert 1023 to 255
  //data to be sent so slave 2 based on slave 1 data
  //dataBus[5] = dataBus[1]*0.25;

   if((dataBus[0] == 11)&&(dataBus[1] == 0)){
     ruang1 = 0;
     Serial.println(ruang1);
    }
    
   if((dataBus[0] == 11)&&(dataBus[1] == 1)){
     ruang1 = 1;
     Serial.println(ruang1);
    }
    
   if((dataBus[0] == 11)&&(dataBus[1] == 2)){
     ruang1 = 2;
     Serial.println(ruang1);
    }

   if((dataBus[0] == 12)&&(dataBus[1] == 3)){
     ruang2 = 3;
     Serial.println(ruang2);
    }

    if((dataBus[0] == 12)&&(dataBus[1] == 4)){
     ruang2 = 4;
     Serial.println(ruang2);
    }
    
   if((dataBus[0] == 12)&&(dataBus[1] == 5)){
     ruang2 = 5;
     Serial.println(ruang2);
    } 

}

void setup() {
  Serial.begin (9600); //baud rate of Serial PC
  mySerial.begin( 19200 ); // baud-rate of RS485
  init_modBus();
}

void loop() {
  rtuState();
  printData();
  processData();
}


6. Program Interface VB 6.0


Option Explicit
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 10
Dim sHari As String
Dim aHari

Private Sub berhenti_Click()
  start.Enabled = True
    berhenti.Enabled = False
    TimerBaca.Enabled = False
End Sub

Private Sub Command2_Click()
Unload Me
End Sub

Private Sub Command3_Click()
Text1.Text = " "
Label1.Caption = " "

End Sub

Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
    ComboCOM.AddItem (i)
Next i

 aHari = Array("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu")

End Sub

Private Sub start_Click()
Dim u As Integer

    On Error GoTo ada_eror
    
    MSComm2.CommPort = ComboCOM.Text
    MSComm2.Settings = "9600,N,8,1"
    MSComm2.RThreshold = 15
    MSComm2.InputLen = 15
    MSComm2.InputMode = comInputModeText
    MSComm2.PortOpen = True ' buka port
    
    ReDim arrdata(1 To MAKSBACA + 1)
    For u = 1 To MAKSBACA
        arrdata(u) = 0
    Next
    TotalBaca = 0
    start.Enabled = False
    berhenti.Enabled = True
    TimerBaca.Enabled = True
ada_eror:
 If ComboCOM.ListIndex < 1 Then
    MsgBox "COM berada di Nomor : " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, "ERROR"
    start.Enabled = True
    berhenti.Enabled = True
    ComboCOM.Text = "Pilih COM"
End If
End Sub

Private Sub Timer1_Timer()
 sHari = aHari(Abs(Weekday(Date) - 1))
 lbltanggal.Caption = "" & sHari & "," & Format(Date, "dd mmmm yyyy")
 lbljam.Caption = Format(Time, "hh:mm:ss")
End Sub

Private Sub TimerBaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
    strInput = MSComm2.Input
        strPotong = strInput
        Text1.Text = Text1.Text + strPotong + vbCrLf
        singleInput = Val(strPotong)
        Label5.Caption = singleInput
        If TotalBaca >= MAKSBACA Then
            TimerBaca.Enabled = False
            If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
            Call berhenti_Click
        End If
        
        If Label5.Caption = "0" Then
         Shape1.Visible = False
         Shape2.Visible = False
        End If
        
        If Label5.Caption = "11111" Then
         Shape1.Visible = True
         Shape2.Visible = False
        End If
        
        If Label5.Caption = "22222" Then
         Shape1.Visible = False
         Shape2.Visible = True
        End If
        
        If Label5.Caption = "33333" Then
         Shape3.Visible = True
         Shape4.Visible = True
        End If
        
        If Label5.Caption = "44444" Then
         Shape3.Visible = True
         Shape4.Visible = False
        End If
        
        If Label5.Caption = "55555" Then
         Shape3.Visible = False
         Shape4.Visible = True
        End If
        
End Sub


7. VIDEO HASILNYA




No comments:

Post a Comment