System.dll中发生类型'System.TimeoutException'的第一次机会异常“

问题描述 投票:0回答:1

Hiii,我总是在port.Read中出现错误-> ret_val = port.Read(readBuffer, offset, CommandDefine.MAX_SIZE.USB_DATA_2048_BYTES - offset);

public bool Send(COMMAND request)
{

    bool return_status = false;

    Console.Write("\n Send : {0:x4} \n", request.COMMAND_ID);

    Thread.Sleep(300);
    try
    {
        WriteCommandAsByte(request.ToByteArray());

    }
    catch (Exception ex)
    {

        Console.Write("\n Exception: \n {0}", ex.Message);
        return_status = false;

    }
    int offset = 0;

    int try_count = 0;
    while (try_count < CommandDefine.MAX_TRY_COUNT)
    {
        Console.Write("\n Try : {0}", try_count + 1);
        while (true)
        {
            int ret_val = 0;
            try
            {
                ret_val = port.Read(readBuffer, offset, CommandDefine.MAX_SIZE.USB_DATA_2048_BYTES - offset);
                Console.Write("\n ret_val2 {0}", ret_val);
                if (ret_val + offset >= 10)
                {
                    byte[] size_arr = new byte[sizeof(ushort)];
                    Buffer.BlockCopy(readBuffer, 5, size_arr, 0, sizeof(ushort));
                    ushort size = Common.ByteToUint16(size_arr);
                    Console.Write("\n Size2 {0}", size);
                    int bytes_to_recieve = size + 9;
                    Console.Write("\n Bytes_to_recieve2 {0}", bytes_to_recieve);
                    if (offset + ret_val >= bytes_to_recieve)
                    {
                        byte[] b = new byte[bytes_to_recieve];
                        Array.Copy(readBuffer, b, bytes_to_recieve);

                        COMMAND sample = USB.BufferToCommand(b);

                        if (IsValidBuff(sample))
                        {
                            var returnedValue = App.Current.Dispatcher.Invoke(new newDelegate(MainWindow.processBuffer), b, request);
                            return_status = (bool)returnedValue;
                            if (return_status)
                            {
                                if (request.COMMAND_ID == CommandDefine.CommandID.GET_THRSLD_SET_REQ)
                                {
                                    App.MAIN_WINDOW.DataContext = null;
                                    App.MAIN_WINDOW.DataContext = App.ViewModel;

                                }
                                break;
                            }
                        }
                        else
                        {
                            return_status = false;
                            break;
                        }

                    }
                    else
                    {
                        offset += ret_val;

                    }

                }
                else
                {
                    offset += ret_val;
                }

            }
            catch (TimeoutException)
            {
                return_status = false;
                break;
            }
        }
        if (return_status)
            break;
        try_count++;
    }
    return return_status;
}
c# wpf visual-studio-2013
1个回答
0
投票

我想您的端口是SerialPort

检查文档:

https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.read

显示TimeoutException有原因:

No characters were available to read.

您应使用SerialPort.BytesToRead确定是否有任何字节要读取。

© www.soinside.com 2019 - 2024. All rights reserved.