Posted on

ตัวอย่างโปรแกรม แสดงผล Counter บนคอมพิวเตอร์ Modbus RTU RS485 ด้วย Software Processing 

โปรโตคอล Modbus เป็นวิธีการมาตรฐานในอุตสาหกรรมสำหรับการถ่ายโอนข้อมูล ใช้กับระบบอัตโนมัติและระบบควบคุมในงานอุตสาหกรรม

  • Step 3 : เชื่อมต่อสายสื่อสารข้อมูล RS485-USB เข้ากับคอมพิวเตอร์
  • Step 4 : เปิด Source Code เปลี่ยน Comport ให้ตรง (ทำได้โดยแก้ไขบรรทัดที่  3 เดิมเป็น COM11 เปลี่ยน commPort เป็น COMXX ให้ตรงกับ Comport ในคอมพิวเตอร์ของเรา *ดูใน Device Manager )
import processing.serial.*;
Serial myPort; // create a Serial port instance
String commPort = "COM11";
int baud = 9600;
char parity = 'N';
int dataBits = 8;
float stopBits = 1.0;

ModbusPort port_one; // create a Modbus port instance
int timeout = 500;
int polling = 100;
int no_of_retries = 10;
int total_no_of_packets = 2;

Packet[] packets = new Packet[total_no_of_packets]; // create an array of packets to transmit
int[] readRegs1  = new int[2]; // to store the requested data
int[] readRegs2  = new int[2]; // to store the requested data
//int[] readRegs3  = new int[2]; // to store the requested data

int previousMillis = 0;
int count = 0;
void setup(){
  size(1024, 768);

  myPort = new Serial(this, commPort, baud, parity, dataBits, stopBits);   // initialize the Serial port instance
  port_one = new ModbusPort(myPort, timeout, polling, no_of_retries, packets, total_no_of_packets);   // initialize a port for modbus communications

  // The packet format for function 1, 2, 3, 4, 15 & 16 is:
  // Packet(id, function, address, data, data array)

  packets[0] = new Packet(1, 03, 01, 1, readRegs1);  //(ID, FNC READ_HOLDING_REGISTERS = 3, ADD (00=Target) (36=Count), data, data array)
  packets[1] = new Packet(1, 03, 15, 1, readRegs2);  //(ID, FNC READ_HOLDING_REGISTERS = 3, ADD (00=Target) (36=Count), data, data array)
  //packets[2] = new Packet(1, 03, 31, 1, readRegs3);  //(ID, FNC READ_HOLDING_REGISTERS = 3, ADD (00=Target) (36=Count), data, data array)

}

void draw(){  
  background(200);
  port_one.update();
  // Display the registers received using function 3
  
  fill(255, 0, 0); // RGB Color
  textSize(50); 
  text("Counter Machine S2-Innovation", 40, 50);
  
  fill(0, 255, 0); // RGB Color
  textSize(200); 
  text("TARGET : "+readRegs1[0], 50, 300);
  
  fill(0, 0, 255); // RGB Color  
  textSize(200);
  text("ACTUAL : "+readRegs2[0], 50, 600);

  //text("OUT    :" +readRegs3[0], 10, 1200);
    
}

Step 5 : กดปุ่มรัน (Run) โปรแกรมจะแสดงผลค่า Target และค่าที่นับได้ (Actual)