First a picture of the
bug .
If using rtl_tcp with sdr# in raspberry Pi or other embedded machines, something described in
Rtl_tcp issues or
raspberry_pi_rtl_tcp_problems may happen, no hints to solve this bug until this thread appear for_those_having_problem_with_rtl_tcp.
I decompile the sdrsharper's SDRSharp.RTLTCP.dll, and found that the dll is using
an old codebase of sdrsharp .
I minimize the bug area of the code in RTLTCPIO.cs, and list them below. After I replace the bug code with the right one, the bug did vanish, but I can't find why. which line of code on earth cause the buzzing?
bug code
private void RecieveSamples()
{
var recBuffer = new byte[_bufferSize];
var recUnsafeBuffer = UnsafeBuffer.Create(recBuffer);
var recPtr = (byte*) recUnsafeBuffer;
_iqBuffer = UnsafeBuffer.Create(_bufferSize / 2, sizeof(Complex));
_iqBufferPtr = (Complex*) _iqBuffer;
var offs = 0;
while (_s != null && _s.Connected)
{
try
{
var bytesRec = _s.Receive(recBuffer, offs, _bufferSize - offs, SocketFlags.None);
var totalBytes = offs + bytesRec;
offs = totalBytes % 2; //Need to correctly handle the hypothetical case where we somehow get an odd number of bytes
ProcessSamples(recPtr, totalBytes - offs); //This might work.
if (offs == 1)
{
recPtr[0] = recPtr[totalBytes - 1];
}
}
catch
{
Close();
break;
}
}
}
private void ProcessSamples(byte* rawPtr, int len)
{
var sampleCount = len / 2;
var ptr = _iqBufferPtr;
for (var i = 0; i < sampleCount; i++)
{
ptr->Imag = _lutPtr[*rawPtr++];
ptr->Real = _lutPtr[*rawPtr++];
ptr++;
}
if (_callback != null)
{
_callback(this, _iqBufferPtr, sampleCount);
}
}
no bug code
private UnsafeBuffer _b;
private void RecieveSamples()
{
byte[] recBuffer = new byte[_bufferSize + 1024];
int offs = 0;
while (_callback != null && _s != null && _s.Connected)
{
try
{
int bytesRec = _s.Receive(recBuffer, offs, _bufferSize, SocketFlags.None);
int totalBytes = offs + bytesRec;
offs = totalBytes % 2; //Need to correctly handle the hypothetical case where we somehow get an odd number of bytes
beamUpThemSamples(recBuffer, totalBytes - offs); //This might work.
}
catch (Exception e)
{
Close();
break;
}
}
}
private void beamUpThemSamples(byte[] buffer, int len)
{
var sampleCount = len / 2;
Complex* bufPtr;
if (_b == null || _b.Length < sampleCount)
{
_b = UnsafeBuffer.Create(sampleCount, sizeof(Complex));
}
bufPtr = (Complex*)_b;
for (int i = 0; i < sampleCount; i++)
{
bufPtr[i].Real = (buffer[i * 2 + 1] - 127.5f) / 127.5f;
bufPtr[i].Imag = (buffer[i * 2] - 127.5f) / 127.5f ;
}
lock (this)
{
if (null == _callback) { return; }
_callback(this, bufPtr, sampleCount);
}
}
p.s. the bug has nothing to do with the lock(this) code, I have confirmed that.