Skip to content

Commit a68e45e

Browse files
committed
초기 커밋.
1 parent e1dd937 commit a68e45e

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

ModBusSlave.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
12+
</ItemGroup>
13+
14+
</Project>

ModBusSlave.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35327.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ModBusSlave", "ModBusSlave.csproj", "{27410EB1-8C87-4874-B322-E1C904FF0838}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{27410EB1-8C87-4874-B322-E1C904FF0838}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{27410EB1-8C87-4874-B322-E1C904FF0838}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{27410EB1-8C87-4874-B322-E1C904FF0838}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{27410EB1-8C87-4874-B322-E1C904FF0838}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {37A3EEB7-5827-42E1-A620-D48E980CBC82}
24+
EndGlobalSection
25+
EndGlobal

ModbusRTU.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace ModBusSlave
2+
{
3+
internal class ModbusRTU
4+
{
5+
private byte[] _frame;
6+
private byte _slaveAddr;
7+
private byte _functionCode;
8+
private byte[] _data;
9+
private byte[] _crc;
10+
11+
public ModbusRTU(byte slaveAddr, byte functionCode, byte[] data)
12+
{
13+
_slaveAddr = slaveAddr;
14+
_functionCode = functionCode;
15+
_data = data;
16+
}
17+
18+
public ModbusRTU(byte[] frame)
19+
{
20+
_frame = frame;
21+
_slaveAddr = frame[0];
22+
_functionCode = frame[1];
23+
_data = new byte[frame.Length - 5];
24+
Array.Copy(frame, 2, _data, 0, _data.Length);
25+
_crc = new byte[2];
26+
Array.Copy(frame, frame.Length - 2, _crc, 0, 2);
27+
}
28+
29+
public byte[] GetFrame()
30+
{
31+
byte[] frame = new byte[1 + 1 + _data.Length + 2]; // SlaveAddr + FunctionCode + Data + CRC
32+
33+
frame[0] = _slaveAddr;
34+
frame[1] = _functionCode;
35+
Array.Copy(_data, 0, frame, 2, _data.Length);
36+
37+
ushort crc = CalcCRC(frame, 0, frame.Length - 2);
38+
frame[frame.Length - 2] = (byte)(crc & 0xFF);
39+
frame[frame.Length - 1] = (byte)(crc >> 8);
40+
41+
return frame;
42+
}
43+
44+
private static ushort CalcCRC(byte[] data, int offset, int count)
45+
{
46+
ushort crc = 0xFFFF;
47+
48+
for (int i = offset; i < offset + count; i++)
49+
{
50+
crc ^= data[i];
51+
52+
for (int j = 0; j < 8; j++)
53+
{
54+
if ((crc & 0x0001) == 1)
55+
{
56+
crc >>= 1;
57+
crc ^= 0xA001;
58+
}
59+
else
60+
{
61+
crc >>= 1;
62+
}
63+
}
64+
}
65+
66+
return crc;
67+
}
68+
69+
}
70+
}

Program.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using ModBusSlave;
4+
5+
SerialPortConnector serialPortConnector = new SerialPortConnector();
6+
serialPortConnector.GetPortNames();
7+
string portName = Console.ReadLine();
8+
try
9+
{
10+
serialPortConnector.Open(portName);
11+
}
12+
catch (Exception e)
13+
{
14+
Console.WriteLine("잘못된 포트");
15+
Console.Error.WriteLine(e.Message);
16+
return;
17+
}
18+
19+
Console.WriteLine("연결되었습니다.");
20+
21+
22+
23+
24+
25+
26+
27+
28+

SerialPortConnector.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.IO.Ports;
2+
3+
namespace ModBusSlave
4+
{
5+
public class SerialPortConnector
6+
{
7+
SerialPort seriallPort = new SerialPort();
8+
9+
public SerialPortConnector()
10+
{
11+
seriallPort.BaudRate = 115200;
12+
seriallPort.Parity = Parity.None;
13+
seriallPort.DataBits = 8;
14+
seriallPort.StopBits = StopBits.One;
15+
seriallPort.Handshake = Handshake.None;
16+
seriallPort.ReadTimeout = 500;
17+
seriallPort.WriteTimeout = 500;
18+
}
19+
20+
public void Open(string portName)
21+
{
22+
seriallPort.PortName = portName;
23+
seriallPort.Open();
24+
}
25+
26+
public void Close()
27+
{
28+
seriallPort.Close();
29+
}
30+
31+
public void Write(byte[] data)
32+
{
33+
seriallPort.Write(data, 0, data.Length);
34+
}
35+
36+
public byte[] Read()
37+
{
38+
byte[] buffer = new byte[seriallPort.BytesToRead];
39+
seriallPort.Read(buffer, 0, buffer.Length);
40+
return buffer;
41+
}
42+
43+
public bool IsOpen()
44+
{
45+
return seriallPort.IsOpen;
46+
}
47+
48+
public string[] GetPortNames()
49+
{
50+
string[] ports = SerialPort.GetPortNames();
51+
Console.WriteLine("시리얼 포트 선택하기: ");
52+
ports.ToList().ForEach(port => Console.WriteLine($"{port} "));
53+
return ports;
54+
}
55+
56+
}
57+
}

0 commit comments

Comments
 (0)