-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55d5163
commit 6bfb643
Showing
5 changed files
with
148 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System.IO.Ports; | ||
using System.Text; | ||
|
||
namespace InverterMon.Server.InverterService; | ||
|
||
public static class Inverter | ||
{ | ||
private static SerialPort? _serialPort; | ||
private static FileStream? _fileStream; | ||
|
||
public static bool Connect(string devicePath, ILogger logger) | ||
{ | ||
try | ||
{ | ||
if (devicePath.Contains("/hidraw", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
_fileStream = new FileStream(devicePath, FileMode.Open, FileAccess.ReadWrite); | ||
return true; | ||
} | ||
else if (devicePath.Contains("/ttyUSB", StringComparison.OrdinalIgnoreCase) || devicePath.Contains("COM", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
_serialPort = new SerialPort(devicePath) | ||
{ | ||
BaudRate = 2400, | ||
Parity = Parity.None, | ||
DataBits = 8, | ||
StopBits = StopBits.One, | ||
Handshake = Handshake.None | ||
}; | ||
_serialPort.Open(); | ||
return true; | ||
} | ||
else | ||
{ | ||
logger.LogError("device path [{path}] is not acceptable!", devicePath); | ||
} | ||
} | ||
catch (Exception x) | ||
{ | ||
logger.LogError("connection error at [{path}]. reason: [{reason}]", devicePath, x.Message); | ||
} | ||
return false; | ||
} | ||
|
||
private static readonly byte[] _writeBuffer = new byte[512]; | ||
public static Task Write(string command, CancellationToken ct) | ||
{ | ||
byte[] cmdBytes = Encoding.ASCII.GetBytes(command); | ||
ushort crc = CalculateXmodemCrc16(command); | ||
|
||
Buffer.BlockCopy(cmdBytes, 0, _writeBuffer, 0, cmdBytes.Length); | ||
_writeBuffer[cmdBytes.Length] = (byte)(crc >> 8); | ||
_writeBuffer[cmdBytes.Length + 1] = (byte)(crc & 0xff); | ||
_writeBuffer[cmdBytes.Length + 2] = 0x0d; | ||
|
||
if (_fileStream != null) | ||
{ | ||
return _fileStream.WriteAsync(_writeBuffer, 0, cmdBytes.Length + 3, ct); | ||
} | ||
else if (_serialPort != null) | ||
{ | ||
return _serialPort.BaseStream.WriteAsync(_writeBuffer, 0, cmdBytes.Length + 3, ct); | ||
} | ||
return Task.CompletedTask; | ||
} | ||
|
||
private static readonly byte[] _readBuffer = new byte[1024]; | ||
public static async Task<string> Read(CancellationToken ct) | ||
{ | ||
int pos = 0; | ||
const byte eol = 0x0d; | ||
|
||
if (_fileStream != null) | ||
{ | ||
do | ||
{ | ||
int readCount = await _fileStream.ReadAsync(_readBuffer.AsMemory(pos, _readBuffer.Length - pos), ct); | ||
if (readCount > 0) | ||
{ | ||
pos += readCount; | ||
for (int i = pos - readCount; i < pos; i++) | ||
{ | ||
if (_readBuffer[i] == eol) | ||
return Encoding.ASCII.GetString(_readBuffer, 0, i - 2).Sanitize(); | ||
} | ||
} | ||
} | ||
while (pos < _readBuffer.Length); | ||
} | ||
else if (_serialPort != null) | ||
{ | ||
do | ||
{ | ||
int readCount = await _serialPort.BaseStream.ReadAsync(_readBuffer.AsMemory(pos, _readBuffer.Length - pos), ct); | ||
if (readCount > 0) | ||
{ | ||
pos += readCount; | ||
for (int i = pos - readCount; i < pos; i++) | ||
{ | ||
if (_readBuffer[i] == eol) | ||
return Encoding.ASCII.GetString(_readBuffer, 0, i - 2).Sanitize(); | ||
} | ||
} | ||
} | ||
while (pos < _readBuffer.Length); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException("inverter not connected."); | ||
} | ||
throw new InvalidOperationException("buffer overflow."); | ||
} | ||
|
||
private static ushort CalculateXmodemCrc16(string data) | ||
{ | ||
ushort crc = 0; | ||
int length = data.Length; | ||
|
||
for (int i = 0; i < length; i++) | ||
{ | ||
crc ^= (ushort)(data[i] << 8); | ||
for (int j = 0; j < 8; j++) | ||
{ | ||
if ((crc & 0x8000) != 0) | ||
crc = (ushort)((crc << 1) ^ 0x1021); | ||
else | ||
crc <<= 1; | ||
} | ||
} | ||
return crc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
{ | ||
"LaunchSettings": { | ||
"DeviceAddress": "/dev/ttyUSB0", | ||
"JkBmsAddress": "/dev/ttyUSB1", | ||
"DeviceAddress": "/dev/hidraw0", | ||
"JkBmsAddress": "/dev/ttyUSB0", | ||
"WebPort": 80, | ||
"TroubleMode": "no" | ||
}, | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Error" | ||
"Microsoft.AspNetCore": "Error", | ||
"Microsoft.Hosting.Lifetime": "Error", | ||
"FastEndpoints.StartupTimer": "None" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
## changelog | ||
- decrease bms timeout | ||
- fix NAK error msg on startup | ||
- disable blazor unhandled exception modal | ||
- remove swagger | ||
- auto reload after 2 minute inactivity (helps with mobile browser suspension) | ||
- rewrite device access logic. | ||
- optimize speed and reduce allocations. |