반응형
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ModbusRTU_Float
{
class Program
{
static void Main(string[] args)
{
// Define serial port settings
string portName = "COM6"; // Modify this with your COM port name
int baudRate = 38400; // Modify this with your baud rate
Parity parity = Parity.None;
int dataBits = 8;
StopBits stopBits = StopBits.One;
// Create serial port instance
SerialPort serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
try
{
// Open serial port
serialPort.Open();
// Define Modbus RTU request (example: read holding register)
byte[] request = { 0x01, 0x04, 0x00, 0x00, 0x00, 0x31, 0x31, 0xDE };
//byte[] request = { 0x01, 0x04, 0x00, 0x00, 0x00, 0x64, 0xF1, 0xE1 };
// Send request
serialPort.Write(request, 0, request.Length);
// Wait for response
System.Threading.Thread.Sleep(100); // Adjust delay as needed
// Read response
byte[] response = new byte[serialPort.BytesToRead];
serialPort.Read(response, 0, response.Length);
// Process response (example: print received data)
Console.WriteLine("Response: " + BitConverter.ToString(response));
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
// Close serial port
if (serialPort.IsOpen)
serialPort.Close();
}
}
}
}
http://www.motor114.co.kr/bbs/board.php?bo_table=tech&wr_id=67&sst=wr_hit&sod=asc&sop=and&page=1
ELD2드라이버의 RS-485(Modbus-RTU) C# 라이브러리
(주)모터114 0건 7,850회 22-07-12 13:47
ELD2드라이버의 RS-485(Modbus-RTU)라이브러리의 C#오픈 소스 자료입니다.
반응형
댓글