Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
28 views4 pages

C# ProgramDesign

The document provides instructions for integrating and using the DMT.dll and DMT.CS files in a Microsoft Visual Studio project for Modbus communication. It details the steps for setting up serial and TCP communication, including importing necessary functions, declaring variables, and handling data requests and responses. Additionally, it includes example code snippets for both Modbus Serial and Modbus TCP communication processes.

Uploaded by

Roberto Rojas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

C# ProgramDesign

The document provides instructions for integrating and using the DMT.dll and DMT.CS files in a Microsoft Visual Studio project for Modbus communication. It details the steps for setting up serial and TCP communication, including importing necessary functions, declaring variables, and handling data requests and responses. Additionally, it includes example code snippets for both Modbus Serial and Modbus TCP communication processes.

Uploaded by

Roberto Rojas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.Copy the“DMT.dll & DMT.CS”file to the project director.

2.Refrences DMT.CS into Microsoft Visual Studio.Solution Explorer.(as


shown in Figure1)。

圖1
3.Import header
using DMTLTB;
4.Import function
//Communication control
[DllImport("DMT.dll", EntryPoint = "OpenModbusSerial")]
public static extern short OpenModbusSerial(short conn_num, short baud_rate, short
data_len, char parity, short stop_bits,
short modbus_mode);
[DllImport("DMT.dll", EntryPoint = "CloseSerial")]
public static extern void CloseSerial(short comport_num);
[DllImport("DMT.dll", EntryPoint = "OpenModbusTCPSocket")]
public static extern short OpenModbusTCPSocket(short conn_num, uint ipaddr);
[DllImport("DMT.dll", EntryPoint = "CloseSocket")]
public static extern void CloseSocket(int conn_num);
//Data Request and Response
[DllImport("DMT.dll",EntryPoint = "RequestData")]
public static extern short RequestData(short comm_type, short conn_num, short
station_addr, short func_code, byte[] txbuf,
short datalen);
[DllImport("DMT.dll", EntryPoint = "ResponseData")]
public static extern short ResponseData(short comm_type, short conn_num,ref short
station_addr,ref short func_code, ref byte
rxbuf);
Example

Declare variable
private byte[] txbuf = new byte[1024];//delivery buffer
private byte[] rxbuf = new byte[1024];//receive buffer
short conn_num = 0; //serial port number or line
indentification code
public int CommType; //communication type
0-SerialPort 1-Ethernet
public short ModbusMode; //communication mode 1-ASCII,2-
RTU
public string SerialPort; //serial port
public string IPAddr; //internet IP
public short sModbusAddr; //delivery station
public short sFunCode; //delivery function code
public string SendData; //delivery data
public short rModbusAddr; //receive station
public short rFunCode; //receive function code
public string ReceiveData; //receive data
public int DataLen; //data length

specify the variable value


sModbusAddr=0x01; //station,1
sFunCode=0x03; //function code,0x03
txbuf[0]=0x21; //Modbus start address,2102
txbuf[1]=0x02
txbuf[2]=0x00; //read length,2 registers
txbuf[3]=0x02;
CommType=0; //modbus serial communication

1.Modbus Serial
specify communication parameter
short BaudRate = 9600; //baud rate
short DataBits = 7; //data bits
char Parity = ‘E’; //parity
short StopBit =1; //stop bit
short SendLen = 4; //delivery data length
short StationAddr =sModbusAddr; //delivery station
short FunCode =sFunCode; //delivery function code
short Mode = ModbusMode; //communication mode
short ComPort =1; //serial port number COM1

Open communication
short rst = DMT.OpenModbusSerial(ComPort, BaudRate, DataBits,
Parity, StopBit, Mode);
if(rst >0)
{
//communication sucucess
}
else
{
//communication fail
}

Request Data
short rst = DMT.RequestData(CommType, ComPort, StationAddr,
FunCode, txbuf, SendLen);
if(rst>0)
{
// RequestData success
}
else
{
// RequestData fail
}

Response Data
short rst = DMT.ResponseData(CommType, ComPort, ref
rModebusAddr, ref senddata.rFunCode,
ref rxbuf[0]);
if(senddata.rFunCode == sFunCode)
{
// Response Data success
}
else
{
// Response Data fail
}

Close communication
DMT.CloseSerial(ComPort);

2.Modbus/Tcp
specify communication parameter
short StationAddr = sModbusAddr;
short FunCode = sFunCode;
short Mode = ModbusMode;
short SendLen = 4;
string ipStr = "192.168.0.1";
System.Net.IPAddress _T = System.Net.IPAddress.Parse(ipStr);
uint ipCode = BitConverter.ToUInt32(_T.GetAddressBytes(), 0);

Open communication
short rst = DMT.OpenModbusTCPSocket(conn_num, ipCode);
if(rst>0)
{
// communication sucucess
}
else
{
// communication fail
}
Request Data
short rst = DMT.RequestData(1, conn_num, StationAddr, FunCode,
txbuf, SendLen);
if(rst>0)
{
// RequestData success
}
else
{
// RequestData fail
}

Response Data
short rst = DMT.ResponseData(1, conn_num, ref rModebusAddr, ref
rFunCode, ref rxbuf[0]);
if(senddata.rFunCode == sFunCode)
{
// Response Data success
}
else
{
// Response Data fail
}
Close communication
DMT.CloseSocket(conn_num);

You might also like