Skip to content

Commit 8b6cce8

Browse files
committed
refactor(SocketCanBus): Optimized frame handling and transmission logic
1 parent df5fdfa commit 8b6cce8

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/adapters/CanKit.Adapter.SocketCAN/SocketCanBus.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,10 @@ public int Transmit(ReadOnlySpan<CanFrame> frames, int timeOut = 0)
251251
int sendCount = 0;
252252
var stopWatch = new Stopwatch();
253253
var pollFd = new Libc.pollfd { fd = _fd.DangerousGetHandle().ToInt32(), events = Libc.POLLOUT };
254-
var needSend = frames.ToArray();
255-
var wrote = _transceiver.Transmit(this, needSend.AsSpan());
254+
var wrote = _transceiver.Transmit(this, frames);
256255
sendCount = wrote;
257256
var remainingTime = timeOut;
258-
while ((timeOut < 0 || remainingTime > 0) && sendCount < needSend.Length)
257+
while ((timeOut < 0 || remainingTime > 0) && sendCount < frames.Length)
259258
{
260259
remainingTime = timeOut > 0
261260
? (int)Math.Max(0, timeOut - stopWatch.ElapsedMilliseconds)
@@ -279,8 +278,7 @@ public int Transmit(ReadOnlySpan<CanFrame> frames, int timeOut = 0)
279278
Libc.ThrowErrno("poll(POLLOUT)", "socket error");
280279
}
281280

282-
wrote = _transceiver.Transmit(this,
283-
new ArraySegment<CanFrame>(needSend, sendCount, needSend.Length - sendCount).AsSpan());
281+
wrote = _transceiver.Transmit(this, frames.Slice(sendCount, frames.Length - sendCount));
284282
sendCount += wrote;
285283

286284
}

0 commit comments

Comments
 (0)