diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-02-10 15:49:54 +0200 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-02-10 15:49:54 +0200 |
| commit | a01a540f2f3465d6bb04db199949bb47462350bd (patch) | |
| tree | fd2ac21f6f9e8600f88271309979a04c626b6e60 /Software/Visual_Studio | |
| parent | 98d52d4eaeb42d21d2b845c6790e8abf5ddc2352 (diff) | |
| download | Tango-a01a540f2f3465d6bb04db199949bb47462350bd.tar.gz Tango-a01a540f2f3465d6bb04db199949bb47462350bd.zip | |
Fixed major issue with USB adapter buffering.
Fixed issues with TransportMessage raising task result/exception in task and not thread which caused a hang sometimes.
Diffstat (limited to 'Software/Visual_Studio')
4 files changed, 35 insertions, 11 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/EventLogging/DefaultEventLogger.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/EventLogging/DefaultEventLogger.cs index 0afd2e31d..ade15af7b 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/EventLogging/DefaultEventLogger.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/EventLogging/DefaultEventLogger.cs @@ -251,7 +251,7 @@ namespace Tango.PPC.Common.EventLogging machineEvent.HostName = _hostName; machineEvent.EventType = _eventTypesGuids[machineEvent.Type]; - if (_machineProvider.MachineOperator == null || _authentication.CurrentUser == null) + if (_machineProvider.Machine == null || _authentication.CurrentUser == null) { _pendingEvents.Add(machineEvent); } diff --git a/Software/Visual_Studio/Tango.Transport/Adapters/UsbTransportAdapter.cs b/Software/Visual_Studio/Tango.Transport/Adapters/UsbTransportAdapter.cs index bc0a1ae41..084c7b5ef 100644 --- a/Software/Visual_Studio/Tango.Transport/Adapters/UsbTransportAdapter.cs +++ b/Software/Visual_Studio/Tango.Transport/Adapters/UsbTransportAdapter.cs @@ -19,6 +19,7 @@ namespace Tango.Transport.Adapters public class UsbTransportAdapter : TransportAdapterBase { private SerialPort _serialPort; //Serial port instance used to communicate over the serial port. + private const int MAX_EXPECTED_SIZE = 50000; /// <summary> /// Gets or sets the baud rate. @@ -66,7 +67,7 @@ namespace Tango.Transport.Adapters if (State != TransportComponentState.Connected) { - Task.Factory.StartNew(() => + ThreadFactory.StartNew(() => { try { @@ -79,7 +80,6 @@ namespace Tango.Transport.Adapters _serialPort = new SerialPort(); _serialPort.BaudRate = BaudRate.ToInt32(); - _serialPort.DataReceived += OnSerialPortDataReceived; _serialPort.PortName = Address; _serialPort.ReadBufferSize = MAX_BUFFER_SIZE; _serialPort.WriteBufferSize = MAX_BUFFER_SIZE; @@ -88,6 +88,8 @@ namespace Tango.Transport.Adapters _serialPort.DiscardInBuffer(); _serialPort.DiscardOutBuffer(); + _serialPort.DataReceived += OnSerialPortDataReceived; + LogManager.Log($"USB adapter ({Address}) Connected..."); State = TransportComponentState.Connected; @@ -136,7 +138,7 @@ namespace Tango.Transport.Adapters if (State == TransportComponentState.Connected) { - Task.Factory.StartNew(() => + ThreadFactory.StartNew(() => { try { @@ -153,6 +155,7 @@ namespace Tango.Transport.Adapters //_serialPort.DiscardInBuffer(); _serialPort.Close(); _serialPort.Dispose(); + _serialPort.DataReceived -= OnSerialPortDataReceived; LogManager.Log("USB adapter disconnected."); @@ -223,7 +226,10 @@ namespace Tango.Transport.Adapters { try { - if (e.EventType == SerialData.Eof) return; + if (e.EventType == SerialData.Eof) + { + return; + } if (_serialPort.BytesToRead > 4) { @@ -231,6 +237,22 @@ namespace Tango.Transport.Adapters _serialPort.Read(size, 0, size.Length); int expectedSize = BitConverter.ToInt32(size, 0); + if (expectedSize > MAX_EXPECTED_SIZE || expectedSize < 1) + { + LogManager.Log($"Invalid expected size received on USB adapter ({expectedSize} bytes). Discarding buffers...", LogCategory.Warning); + + byte[] falseData = new byte[_serialPort.BytesToRead]; + _serialPort.Read(falseData, 0, falseData.Length); + + try + { + _serialPort.DiscardInBuffer(); + _serialPort.DiscardOutBuffer(); + } + catch { } + return; + } + byte[] data = new byte[expectedSize]; int read = 0; @@ -238,11 +260,13 @@ namespace Tango.Transport.Adapters { read += _serialPort.Read(data, read, Math.Min(_serialPort.BytesToRead, expectedSize - read)); - //Thread.Sleep(2); - if (State != TransportComponentState.Connected) { - break; + if (_serialPort != null) + { + _serialPort.DataReceived -= OnSerialPortDataReceived; + } + return; } } diff --git a/Software/Visual_Studio/Tango.Transport/TransportMessage.cs b/Software/Visual_Studio/Tango.Transport/TransportMessage.cs index 8810b8db4..64dfddfa4 100644 --- a/Software/Visual_Studio/Tango.Transport/TransportMessage.cs +++ b/Software/Visual_Studio/Tango.Transport/TransportMessage.cs @@ -6,6 +6,7 @@ using System.Reactive.Subjects; using System.Text; using System.Threading.Tasks; using Tango.Core.ExtensionMethods; +using Tango.Core.Threading; using Tango.Logging; using Tango.PMR; using Tango.PMR.Common; @@ -50,7 +51,7 @@ namespace Tango.Transport { Completed = completed; - Task.Factory.StartNew(() => + ThreadFactory.StartNew(() => { try { @@ -125,7 +126,7 @@ namespace Tango.Transport Completed = true; exceptionRaised = true; - Task.Factory.StartNew(() => + ThreadFactory.StartNew(() => { if (!IsContinuous) { diff --git a/Software/Visual_Studio/Tango.Transport/TransporterBase.cs b/Software/Visual_Studio/Tango.Transport/TransporterBase.cs index 70feb05f6..edd70f22f 100644 --- a/Software/Visual_Studio/Tango.Transport/TransporterBase.cs +++ b/Software/Visual_Studio/Tango.Transport/TransporterBase.cs @@ -597,7 +597,6 @@ namespace Tango.Transport { TimeoutTask.StartNew(() => { - if (!source.Task.IsCompleted) { TimeoutException ex = new TimeoutException("Request message: " + typeof(Request).Name + " had timed out after " + (config.Timeout != null ? config.Timeout.Value.TotalSeconds : RequestTimeout.TotalSeconds) + " seconds."); |
