Skip to content

Commit

Permalink
Make it compilable
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxwe11 committed Dec 10, 2015
1 parent ea0be01 commit 76f7fd9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion NModbus4/IO/ModbusAsciiTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal override byte[] BuildMessageFrame(IModbusMessage message)

var msgFrameAscii = ModbusUtility.GetAsciiBytes(msgFrame);
var lrcAscii = ModbusUtility.GetAsciiBytes(ModbusUtility.CalculateLrc(msgFrame));
var nlAscii = Encoding.ASCII.GetBytes(Modbus.NewLine.ToCharArray());
var nlAscii = Encoding.UTF8.GetBytes(Modbus.NewLine.ToCharArray());

var frame = new MemoryStream(1 + msgFrameAscii.Length + lrcAscii.Length + nlAscii.Length);
frame.WriteByte((byte)':');
Expand Down
2 changes: 1 addition & 1 deletion NModbus4/IO/StreamResourceUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static string ReadLine(IStreamResource stream)
continue;
}

result.Append(Encoding.ASCII.GetChars(singleByteBuffer).First());
result.Append(Encoding.UTF8.GetChars(singleByteBuffer).First());
}
while (!result.ToString().EndsWith(Modbus.NewLine));

Expand Down
21 changes: 10 additions & 11 deletions NModbus4/IO/UdpClientAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
namespace Modbus.IO
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;

Expand All @@ -16,8 +14,10 @@
internal class UdpClientAdapter : IStreamResource
{
// strategy for cross platform r/w
private const int MaxBufferSize = ushort.MaxValue;
private UdpClient _udpClient;
private List<byte> _buffer;
private readonly byte[] _buffer = new byte[MaxBufferSize];
private int _bufferOffset;

public UdpClientAdapter(UdpClient udpClient)
{
Expand Down Expand Up @@ -86,20 +86,19 @@ public int Read(byte[] buffer, int offset, int count)
"Argument count cannot be greater than the length of buffer minus offset.");
}

if (_buffer == null || _buffer.Count == 0)
if (_bufferOffset == 0)
{
IPEndPoint remoteIpEndPoint = null;
_buffer = _udpClient.Receive(ref remoteIpEndPoint).ToList();
_bufferOffset = _udpClient.Client.Receive(_buffer);
}

if (_buffer.Count < count)
if (_bufferOffset < count)
{
throw new IOException("Not enough bytes in the datagram.");
}

_buffer.CopyTo(0, buffer, offset, count);

_buffer.RemoveRange(0, count);
Buffer.BlockCopy(_buffer, 0, buffer, offset, count);
_bufferOffset -= count;
Buffer.BlockCopy(_buffer, count, _buffer, 0, _bufferOffset);

return count;
}
Expand Down Expand Up @@ -139,7 +138,7 @@ public void Write(byte[] buffer, int offset, int count)
"Argument count cannot be greater than the length of buffer minus offset.");
}

_udpClient.Send(buffer.Skip(offset).ToArray(), count);
_udpClient.Client.Send(buffer.Skip(offset).Take(count).ToArray());
}

public void Dispose()
Expand Down
4 changes: 2 additions & 2 deletions NModbus4/Utility/ModbusUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static uint GetUInt32(ushort highOrderValue, ushort lowOrderValue)
/// <returns>An array of ASCII byte values.</returns>
public static byte[] GetAsciiBytes(params byte[] numbers)
{
return Encoding.ASCII.GetBytes(numbers.SelectMany(n => n.ToString("X2")).ToArray());
return Encoding.UTF8.GetBytes(numbers.SelectMany(n => n.ToString("X2")).ToArray());
}

/// <summary>
Expand All @@ -109,7 +109,7 @@ public static byte[] GetAsciiBytes(params byte[] numbers)
/// <returns>An array of ASCII byte values.</returns>
public static byte[] GetAsciiBytes(params ushort[] numbers)
{
return Encoding.ASCII.GetBytes(numbers.SelectMany(n => n.ToString("X4")).ToArray());
return Encoding.UTF8.GetBytes(numbers.SelectMany(n => n.ToString("X4")).ToArray());
}

/// <summary>
Expand Down

0 comments on commit 76f7fd9

Please sign in to comment.