Professional Documents
Culture Documents
ImplementationofSerialPort
CommunicationBasedonModbusRTU
CommunicationProtocolinC#.NET
PanZhao*1,YongZheng2
CollegeofMechanicalandElectricalEngineering, NorthChinaUniversityofTechnology,Beijing,China
*1
277746909@qq.com;2cntzy@ncut.edu.cn
Abstract
This paper introduces ModbusRTU Communication
protocol, and combining with an instance, it states the
specificmethodtoimplementtheserialportcommunication
betweencomputerandADVANTECH4018+modulesbythe
Serial Port control based on the analysis of the principle of
communicationinC#.NET.
Keywords
FIG.1THEHARDWAREDESIGNOFTHESYSTEMOF
DISTRIBUTEDTEMPERATUREANDPRESSURE
MEASUREMENT
Introduction
TABLE1THEDATAFRAMEFORMATOFMODBUSRTU
COMMUNICATIONPROTOCOL
AddressCode
8bits
FunctionCode
8bits
DataArea
N*8bits
CheckCode
16bits
TABLE2THEQUERYCOMMANDOFTHECOMPUTER
Address Function
Code
Code
0108
03
StartingChannel
Number(highorder)
00
StartingChannel
TheNumberofData TheNumberofData
Number(loworder)
(highorder)
(loworder)
0007
00
01
CRCLow CRCHigh
Byte
Byte
lowbyte highbyte
TABLE3THERESPONSEMESSAGEFORMATOF4018+MODULES
DeviceNumber
FunctionCode
0108
03
TheNumberof
BytesintheData
02
Data1HighByte
Data1LowByte
CRCLowByte
CRCHighByte
lowbyte
Highbyte
37
ElectricalEngineeringResearchVolume2,2014
1)InitializationandOpeningPort
SerialPort1.PortName=COM+PortCmb.Text;
SerialPort1.BaudRate=
Convert.ToInt32(BaudRateCmb.Text);
SerialPort1.DataBits=8;
SerialPort1.StopBits=
System.IO.Ports.StopBits.One;
SerialPort1.Parity=System.IO.Ports.Parity.None;
SerialPort1.Open();
2)SendingData
m_commands[0]=addressDevice;
m_commands[1]=command;
m_commands[2]=0;
m_commands[3]=channel;
m_commands[4]=0;
m_commands[5]=1;
m_crc=CRC16(m_commands,6);
m_commands[6]=m_crc[1];
m_commands[7]=m_crc[0];
SerialPort1.DiscardOutBuffer();
SerialPort1.Write(m_commands,0,
m_commands.Length);
3)ReceivingData
m_receive=newbyte[SerialPort1.BytesToRead];
SerialPort1.Read(m_receive,0,Serial
Port1.BytesToRead);
if(m_receive.GetUpperBound(0)>=6)
{
m_crc=CRC16(m_receive,5);
if (m_crc[0] == m_receive[6] && m_crc[1] ==
m_receive[5])
{
returnValue=m_receive[3]*256
+m_receive[4];
}
else
{
crcCount=crcCount+1;
lblCRC.Text=crcCount.ToString();
returnstring.Empty;
}
}
else
{
SerialPort1.DiscardInBuffer();
returnstring.Empty;
}
ImplementationofCommunication
Serial Port classs namespace is System. IO.Ports, so
onlyaddingusingSystem.IO.Portsatthebeginning
of the routine, the class can be used. As with other
controls, Serial Port interacts with user by attribute,
method and event. Through the design of attribute,
method and event, Serial Port can send and receive
data.
ThegeneralprocessofserialcommunicationforSerial
Port control is opening port, sending data, receiving
dataandclosingport.
38
ImplementationMethodoftheCRCCheck
CRC value is calculated by the sending device and
ElectricalEngineeringResearchVolume2,2014
savedintheendofthedataframe.Theequipmentof
receiving information recalculates CRC value. And
take the CRC value to compare with the new CRC
valuethatiscalculatedbythesendingdevicebasedon
the information that is returned by the receiving
device. It means that there is something wrong with
thecommunicationifthetwovaluesareinconsistent.
(1) Settheregister0FFFFHwhichisCRCregister.
(2) Save the result in the CRC register after the xor
operationbetweenthefirstbyteofthedataframe
andlowbyteoftheCRCregister.
(3) Move the data to the right for a bit, and fill the
highorder with 0, then detect the removing
orderwhichislowestorder.
(4) Iftheremovingorderis0,returntostep(3);ifit
is 1, xor operation between CRC register and a
fixedvalue(0A001H)willbemade.
(5) Repeat step (3) and (4) until 8 times. Then the 8
bitsdatacanbegained.
(6) Repeat step (2) to (5) to deal with the next 8 bits
datauntilallthebytesarefinishedtodispose.
(7) NowCRCregistervalueisCRCvalue.
According to the above rules, the program of CRC
checkcodeinC#isasfollows:
CRC16Lo=0xFF;
CRC16Hi=0xFF;
CL=0x1;
CH=0xA0;
for(intdataInx=0;dataInx<dataLength;dataInx++)
{
CRC16Lo = Convert.ToByte(CRC16Lo ^
data[dataInx]);
for(intFlagInx=0;FlagInx<8;FlagInx++)
{
SaveHi=CRC16Hi;
SaveLo=CRC16Lo;
CRC16Hi=Convert.ToByte(CRC16Hi>>1);
CRC16Lo=Convert.ToByte(CRC16Lo>>1);
if((SaveHi&0x1)==0x1)
{
CRC16Lo=Convert.ToByte(CRC16Lo|0x80);
}
if((SaveLo&0x1)==0x1)
{
CRC16Hi = Convert.ToByte(CRC16Hi ^
CH);
CRC16Lo=Convert.ToByte(CRC16Lo^
CL);
}
}
crc[0]=CRC16Hi;
crc[1]=CRC16Lo;
}
Note:Thelowinbyteisinthefrontandthehighbyte
of CRC16 is at the behind in ModbusRTU
Communicationprotocol.
TestSoftwareDesign
Use VisualStudio 2010 to achieve the design of
software.Thesoftwareinterfaceofcommunicationtest
programisshowninFIG.2.
FIG.2THESOFTWAREINTERFACEOFCOMMUNICATION
TESTPROGRAM
FIG.3THEAPPLICATIONSOFTWAREINTERFACE
39
ElectricalEngineeringResearchVolume2,2014
ApplicationSoftwareDesign
Lijun
Dong,
Shuwei
Liu
&
Yiqing
Hou,Serial
Visual
Basic,
Industrial
Control
Computer,pp.811,August2006.
Liping Li, Quanli Wei, Application of SerialPort Class in
Messaging Software in .NET, Microcomputer & ITS
Application,Vol.31,No.21,pp.1113,2012.
Wenquan
Wang,
Implementation
of
Serial
Port
Conclusions
Mosaic,pp.2123,May2011.
Guo,
Xinggui
Wang,
Realization
of
the
CommunicationbetweenIPCandIntelligentInstrument
Based on Modbus Protocol, Vol.20, No.1, pp.102104,
REFERENCES
2008.
Yuefei Wu, Implementation of Serial Port Communication
of
Serial
Port
Communication
betweenUpperMachineandPLCbySerialPort,Internet
and
Fortune,pp.156157,January2009.
MeasurementandControlApplicationinVisualC#.NET,
PublishingHouseofElectronicsIndustry,Beijing,2012.
40