C# 信号灯LIM在一周后不工作

C# 信号灯LIM在一周后不工作,c#,uwp,win-universal-app,windows-10-universal,C#,Uwp,Win Universal App,Windows 10 Universal,更新:发现错误。我的代码按预期工作,但我的同事在与我通信的设备上添加了一个小代码。结果是它忽略了我的第二个命令,因为它正忙于做其他事情。 修复了它,现在它又开始工作了 在本周初,我问了一个关于将串行端口的访问限制为一个实例的问题 有人真的帮了我,并向我提到了信号量lim。 这在当时确实起到了作用,我想就是这样 几天后,我终于解决了另一个主要问题,在测试时,我注意到信号灯不再工作了 我将代码回滚到以前的版本,在那里我刚刚实现了信号量,并且它确实在工作。 但即使是这些代码也不再有效了 我没有在我的V

更新:发现错误。我的代码按预期工作,但我的同事在与我通信的设备上添加了一个小代码。结果是它忽略了我的第二个命令,因为它正忙于做其他事情。 修复了它,现在它又开始工作了

在本周初,我问了一个关于将串行端口的访问限制为一个实例的问题

有人真的帮了我,并向我提到了
信号量lim
。 这在当时确实起到了作用,我想就是这样

几天后,我终于解决了另一个主要问题,在测试时,我注意到信号灯不再工作了

我将代码回滚到以前的版本,在那里我刚刚实现了信号量,并且它确实在工作。 但即使是这些代码也不再有效了

我没有在我的Visual Studio、我的PC或我的Raspberry中更改任何内容

有人知道为什么理论上应该起作用的东西不再起作用了吗

非常感谢您的帮助:)

添加了我正在使用的代码的非常简短的版本。 通常,我会将得到的数据处理为Int或string数组。 我还将进行CRC16验证,所有内容都将包含在try/catch块中,以便在异常发生时捕获异常

但测试方面,这是我目前所需要的一切。 如果需要,我会尽量提供更详细的信息。请告诉我

当前行为: 第一个任务开始并运行良好。 第二个任务不会启动,之后的每个新任务也不会启动

预期行为: 开始并完成第一项任务。 第一个任务完成后,加载第二个任务并完成它。 之后当我开始另一个任务时,它也应该运行该任务

Mainpage.xaml

public MainPage()
    {
        this.InitializeComponent();

        InitPort();
    }

    private async void getData_Click(object sender, RoutedEventArgs e)
    {
        // Call the async operations.
        // I usually don't call the same operation twice at the same time but it's just a test here.
        // In normal usage the data contains different sizes.

        await getData();

        await getData();
    }

    private async Task getData()
    {
        ReadData readData = new ReadData();

        byte[] readOutput = await readData.ReadParaBlock();

        DisplayTextBox.Text = BitConverter.ToString(readOutput);            
    }

    public async void InitPort()
    {
        string success = await ReadWriteAdapter.Current.Init();
        DisplayTextBox.Text = success;
    }
ReadData.cs

public class ReadData
{
    private ReadBlock readBlock = new ReadBlock();

    public async Task<byte[]> ReadParaBlock()
    {
        // Load task into the semaphore
        await ReadWriteAdapter.Current.semaphore.WaitAsync();

        // start writing to device
        await readBlock.Write();

        // dropped check of recieved checksum because obsolete for test

        // start reading from device
        byte[] recievedArray = await readBlock.Read();

        // release the task from semaphore
        ReadWriteAdapter.Current.semaphore.Release();

        return recievedArray;            
    }
}
public class ReadBlock
{

    public async Task<uint> Write()
    {
        // Command sent to device to get data
        byte[] WriteArray = System.Text.Encoding.ASCII.GetBytes("getdata");            

        return await ReadWriteAdapter.Current.WriteAsync(WriteArray);
    }

    public async Task<byte[]> Read()
    {
        byte[] ListenOut = await ReadWriteAdapter.Current.Listen(100);

        // dropped data conversion because obsolete for test

        return ListenOut;
    }
}
public class ReadWriteAdapter
{
    public SemaphoreSlim semaphore { get; private set; }

    private static readonly Object lockObject = new object();
    private static ReadWriteAdapter instance;
    private DataWriter dataWriter = null;
    private DataReader dataReader = null;
    private SerialDevice serialPort = null;

    public static ReadWriteAdapter Current
    {
        get
        {
            if (instance == null)
            {
                lock (lockObject)
                {
                    if (instance == null)
                    {
                        instance = new ReadWriteAdapter();
                    }
                }
            }
            return instance;
        }
    }

    private ReadWriteAdapter()
    {
        this.semaphore = new SemaphoreSlim(1, 1);
    }

    // initialize the serial port and configure it
    public async Task<string> Init()
    {  
        string aqs = SerialDevice.GetDeviceSelector();

        DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(aqs, null);

        if (devices.Any())
        {                
            string deviceId = devices[0].Id;

            serialPort = await SerialDevice.FromIdAsync(deviceId);

            serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.BaudRate = 19200;
            serialPort.Parity = SerialParity.None;
            serialPort.StopBits = SerialStopBitCount.One;
            serialPort.DataBits = 8;
            serialPort.Handshake = SerialHandshake.None;

            dataWriter = new DataWriter(serialPort.OutputStream);
            dataReader = new DataReader(serialPort.InputStream);                
        }

        return "found port";
    }

    // start to listen to the serial port
    public async Task<byte[]> Listen(uint BufferLength)
    {
        byte[] listen = new byte[BufferLength];

        dataReader = new DataReader(serialPort.InputStream);                    

        listen = await ReadAsync(BufferLength);

        if (dataReader != null)
        {
            dataReader.DetachStream();
            dataReader.Dispose();
            dataReader = null;
        }

        return listen;
    }

    // function to read and interpret the data from serial port
    private async Task<byte[]> ReadAsync(uint ReadBufferLength)
    {
        Task<uint> loadAsyncTask;
        byte[] returnArray = new byte[ReadBufferLength];

        dataReader.InputStreamOptions = InputStreamOptions.Partial;

        loadAsyncTask = dataReader.LoadAsync(ReadBufferLength).AsTask();

        uint bytesRead = await loadAsyncTask;

        if (bytesRead > 0)
        {
            dataReader.ReadBytes(returnArray);
        }            

        return returnArray;
    }

    // write the data using the serial port
    public async Task<uint> WriteAsync(byte[] data)
    {   
        dataWriter.WriteBytes(data);

        Task<uint> storeAsyncTask = dataWriter.StoreAsync().AsTask();

        return await storeAsyncTask;            
    }
}
公共类ReadData
{
private ReadBlock ReadBlock=new ReadBlock();
公共异步任务ReadParaBlock()
{
//将任务加载到信号量中
等待ReadWriteAdapter.Current.semaphore.WaitAsync();
//开始写入设备
等待readBlock.Write();
//已放弃对已接收校验和的检查,因为测试已过时
//从设备开始读取
字节[]receiveDarray=wait readBlock.Read();
//从信号量中释放任务
readwritedapter.Current.semaphore.Release();
返回接收数据;
}
}
ReadBlock.cs

public class ReadData
{
    private ReadBlock readBlock = new ReadBlock();

    public async Task<byte[]> ReadParaBlock()
    {
        // Load task into the semaphore
        await ReadWriteAdapter.Current.semaphore.WaitAsync();

        // start writing to device
        await readBlock.Write();

        // dropped check of recieved checksum because obsolete for test

        // start reading from device
        byte[] recievedArray = await readBlock.Read();

        // release the task from semaphore
        ReadWriteAdapter.Current.semaphore.Release();

        return recievedArray;            
    }
}
public class ReadBlock
{

    public async Task<uint> Write()
    {
        // Command sent to device to get data
        byte[] WriteArray = System.Text.Encoding.ASCII.GetBytes("getdata");            

        return await ReadWriteAdapter.Current.WriteAsync(WriteArray);
    }

    public async Task<byte[]> Read()
    {
        byte[] ListenOut = await ReadWriteAdapter.Current.Listen(100);

        // dropped data conversion because obsolete for test

        return ListenOut;
    }
}
public class ReadWriteAdapter
{
    public SemaphoreSlim semaphore { get; private set; }

    private static readonly Object lockObject = new object();
    private static ReadWriteAdapter instance;
    private DataWriter dataWriter = null;
    private DataReader dataReader = null;
    private SerialDevice serialPort = null;

    public static ReadWriteAdapter Current
    {
        get
        {
            if (instance == null)
            {
                lock (lockObject)
                {
                    if (instance == null)
                    {
                        instance = new ReadWriteAdapter();
                    }
                }
            }
            return instance;
        }
    }

    private ReadWriteAdapter()
    {
        this.semaphore = new SemaphoreSlim(1, 1);
    }

    // initialize the serial port and configure it
    public async Task<string> Init()
    {  
        string aqs = SerialDevice.GetDeviceSelector();

        DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(aqs, null);

        if (devices.Any())
        {                
            string deviceId = devices[0].Id;

            serialPort = await SerialDevice.FromIdAsync(deviceId);

            serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.BaudRate = 19200;
            serialPort.Parity = SerialParity.None;
            serialPort.StopBits = SerialStopBitCount.One;
            serialPort.DataBits = 8;
            serialPort.Handshake = SerialHandshake.None;

            dataWriter = new DataWriter(serialPort.OutputStream);
            dataReader = new DataReader(serialPort.InputStream);                
        }

        return "found port";
    }

    // start to listen to the serial port
    public async Task<byte[]> Listen(uint BufferLength)
    {
        byte[] listen = new byte[BufferLength];

        dataReader = new DataReader(serialPort.InputStream);                    

        listen = await ReadAsync(BufferLength);

        if (dataReader != null)
        {
            dataReader.DetachStream();
            dataReader.Dispose();
            dataReader = null;
        }

        return listen;
    }

    // function to read and interpret the data from serial port
    private async Task<byte[]> ReadAsync(uint ReadBufferLength)
    {
        Task<uint> loadAsyncTask;
        byte[] returnArray = new byte[ReadBufferLength];

        dataReader.InputStreamOptions = InputStreamOptions.Partial;

        loadAsyncTask = dataReader.LoadAsync(ReadBufferLength).AsTask();

        uint bytesRead = await loadAsyncTask;

        if (bytesRead > 0)
        {
            dataReader.ReadBytes(returnArray);
        }            

        return returnArray;
    }

    // write the data using the serial port
    public async Task<uint> WriteAsync(byte[] data)
    {   
        dataWriter.WriteBytes(data);

        Task<uint> storeAsyncTask = dataWriter.StoreAsync().AsTask();

        return await storeAsyncTask;            
    }
}
公共类读块
{
公共异步任务写入()
{
//发送到设备以获取数据的命令
byte[]WriterRay=System.Text.Encoding.ASCII.GetBytes(“getdata”);
返回wait wait ReadWriteAdapter.Current.WriteAsync(WriteArray);
}
公共异步任务读取()
{
字节[]ListenOut=wait readwritedapter.Current.Listen(100);
//已放弃数据转换,因为测试已过时
返回ListenOut;
}
}
readwritedapter.cs

public class ReadData
{
    private ReadBlock readBlock = new ReadBlock();

    public async Task<byte[]> ReadParaBlock()
    {
        // Load task into the semaphore
        await ReadWriteAdapter.Current.semaphore.WaitAsync();

        // start writing to device
        await readBlock.Write();

        // dropped check of recieved checksum because obsolete for test

        // start reading from device
        byte[] recievedArray = await readBlock.Read();

        // release the task from semaphore
        ReadWriteAdapter.Current.semaphore.Release();

        return recievedArray;            
    }
}
public class ReadBlock
{

    public async Task<uint> Write()
    {
        // Command sent to device to get data
        byte[] WriteArray = System.Text.Encoding.ASCII.GetBytes("getdata");            

        return await ReadWriteAdapter.Current.WriteAsync(WriteArray);
    }

    public async Task<byte[]> Read()
    {
        byte[] ListenOut = await ReadWriteAdapter.Current.Listen(100);

        // dropped data conversion because obsolete for test

        return ListenOut;
    }
}
public class ReadWriteAdapter
{
    public SemaphoreSlim semaphore { get; private set; }

    private static readonly Object lockObject = new object();
    private static ReadWriteAdapter instance;
    private DataWriter dataWriter = null;
    private DataReader dataReader = null;
    private SerialDevice serialPort = null;

    public static ReadWriteAdapter Current
    {
        get
        {
            if (instance == null)
            {
                lock (lockObject)
                {
                    if (instance == null)
                    {
                        instance = new ReadWriteAdapter();
                    }
                }
            }
            return instance;
        }
    }

    private ReadWriteAdapter()
    {
        this.semaphore = new SemaphoreSlim(1, 1);
    }

    // initialize the serial port and configure it
    public async Task<string> Init()
    {  
        string aqs = SerialDevice.GetDeviceSelector();

        DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(aqs, null);

        if (devices.Any())
        {                
            string deviceId = devices[0].Id;

            serialPort = await SerialDevice.FromIdAsync(deviceId);

            serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
            serialPort.BaudRate = 19200;
            serialPort.Parity = SerialParity.None;
            serialPort.StopBits = SerialStopBitCount.One;
            serialPort.DataBits = 8;
            serialPort.Handshake = SerialHandshake.None;

            dataWriter = new DataWriter(serialPort.OutputStream);
            dataReader = new DataReader(serialPort.InputStream);                
        }

        return "found port";
    }

    // start to listen to the serial port
    public async Task<byte[]> Listen(uint BufferLength)
    {
        byte[] listen = new byte[BufferLength];

        dataReader = new DataReader(serialPort.InputStream);                    

        listen = await ReadAsync(BufferLength);

        if (dataReader != null)
        {
            dataReader.DetachStream();
            dataReader.Dispose();
            dataReader = null;
        }

        return listen;
    }

    // function to read and interpret the data from serial port
    private async Task<byte[]> ReadAsync(uint ReadBufferLength)
    {
        Task<uint> loadAsyncTask;
        byte[] returnArray = new byte[ReadBufferLength];

        dataReader.InputStreamOptions = InputStreamOptions.Partial;

        loadAsyncTask = dataReader.LoadAsync(ReadBufferLength).AsTask();

        uint bytesRead = await loadAsyncTask;

        if (bytesRead > 0)
        {
            dataReader.ReadBytes(returnArray);
        }            

        return returnArray;
    }

    // write the data using the serial port
    public async Task<uint> WriteAsync(byte[] data)
    {   
        dataWriter.WriteBytes(data);

        Task<uint> storeAsyncTask = dataWriter.StoreAsync().AsTask();

        return await storeAsyncTask;            
    }
}
公共类读写适配器
{
公共信号量LIM信号量{get;private set;}
私有静态只读对象lockObject=新对象();
私有静态读写适配器实例;
专用数据编写器DataWriter=null;
私有数据读取器DataReader=null;
专用SerialDevice serialPort=null;
公共静态读写适配器当前
{
得到
{
if(实例==null)
{
锁定(锁定对象)
{
if(实例==null)
{
instance=new readwritedapter();
}
}
}
返回实例;
}
}
私有ReadWriteAdapter()
{
this.semaphore=新的信号量lim(1,1);
}
//初始化串行端口并对其进行配置
公共异步任务Init()
{  
字符串aqs=SerialDevice.GetDeviceSelector();
DeviceInformation Collection devices=等待DeviceInformation.FindAllAsync(aqs,null);
if(devices.Any())
{                
字符串deviceId=设备[0]。Id;
serialPort=等待SerialDevice.FromIdAsync(deviceId);
serialPort.WriteTimeout=TimeSpan.From毫秒(1000);
serialPort.ReadTimeout=TimeSpan.FromMillistics(1000);
serialPort.BaudRate=19200;
serialPort.Parity=SerialParity.None;
serialPort.StopBits=SerialStopBitCount.One;
serialPort.DataBits=8;
serialPort.Handshake=SerialHandshake.None;
dataWriter=新的dataWriter(serialPort.OutputStream);
dataReader=新的dataReader(serialPort.InputStream);
}
返回“找到的端口”;
}
//开始监听串行端口
公共异步任务侦听(uint BufferLength)
{
字节[]侦听=新字节[BufferLength];
dataReader=新的dataReader(serialPort.InputStream);
listen=wait ReadAsync(BufferLength);
if(dataReader!=null)
{
dataReader.DetachStream();
dataReader.Dispose();
dataReader=null;
}
回听;
}
//用于从串行端口读取和解释数据的函数
专用异步任务ReadAsync(uint ReadBufferLength)
{
任务加载异步任务;
byte[]returnArray=新字节[ReadBufferLength];
dataReader.InputStreamOptions=InputStreamOptions.Partial;
loadAsyncTask=dataReader.LoadAsync(ReadBufferLength.AsTask();
uint bytesRead=等待加载异步任务;
如果(字节读取>0)
{
ReadBytes(返回数组);
}            
返回数组;
}
//使用串行端口写入数据
公共异步任务WriteAsync(字节[]数据)
{   
dataWriter.WriteBytes(