首页 > 解决方案 > .Net 中是否有任何用于 XModem 的第三方库可以帮助更新固件?

问题描述

我最近开发了一个.Net Console app可以与 sas Expander 卡通信的。

问题来了。

我想使用我的控制台应用程序更新卡中的固件。

我已经知道它可以通过Xmodem协议更新

并且可以通过使用成功ExtraPuTTy

发送 fwdl命令后ExtraPuTTy

在此处输入图像描述

发送y命令后ExtraPuTTy

在此处输入图像描述

选择File Transfersend

在此处输入图像描述

转账完成后,卡会重启并给出响应bp1>

在此处输入图像描述

SAS 卡已连接到RS232端口。

固件文件的扩展名为.FW.

我在.Net代码中尝试的内容:


using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Windows.Forms;
using System.Text;
using System.IO;




namespace ConsoleApp3
{
    class Program
    {
        [STAThread]
        static void Main()
        {
           
            
            string[] ports = SerialPort.GetPortNames();
            string com = "";
            Console.WriteLine("The following serial ports were found:");
            foreach (string port in ports)
            {
                Console.WriteLine(port);
                Console.WriteLine("Please choose which port name you want ?");
                com = Console.ReadLine();

            }
            SerialPort mySerialPort = new SerialPort(com);

            mySerialPort.BaudRate = 115200;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.RtsEnable = true;
            mySerialPort.DtrEnable = true;
            mySerialPort.ReadTimeout = 500000;
            mySerialPort.WriteTimeout = 1500;
            mySerialPort.NewLine = "\r";
            mySerialPort.Open();

            if (mySerialPort.IsOpen)
            {
                mySerialPort.Write("\r");
            }

            List<string> Commands = new List<string>();
            string command = "";
            int counter = 0;
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            while (true)
            {
                ConsoleKeyInfo consoleInput = Console.ReadKey();

                if (consoleInput.KeyChar == '\r')
                {
                    Commands.Add(command);
                    counter = Commands.Count - 1;
                    mySerialPort.Write(command + "\r");
                    if (command == "fwdl")
                    {
                        ClearCurrentConsoleLine();
                    }
                    if (command == "y")
                        {
                            String FileName = "";
                            OpenFileDialog openfile1 = new OpenFileDialog();
                            if (openfile1.ShowDialog() == DialogResult.OK)
                            {
                                FileName = openfile1.FileName;
                            }
                        }
                    
                    ClearCurrentConsoleLine();
                    command = "";
                }
                else if (consoleInput.Key == ConsoleKey.UpArrow)
                {
                    if ((counter >= 0) && (counter < Commands.Count))
                    {
                        ClearCurrentConsoleLine();
                        Console.Write(Commands[counter]);
                        foreach (string obj in Commands)
                        {
                            command = obj;
                        }
                        if (consoleInput.KeyChar == '\r')
                        {
                            Commands.Add(command);
                            counter = Commands.Count - 1;
                            mySerialPort.Write(command + "\r");
                            ClearCurrentConsoleLine();
                            command = "";
                        }
                    }

                    if (counter > 0)
                    {
                        counter--;
                    }

                    else
                    {
                        counter = Commands.Count - 1;
                    }
                }
                else
                {
                    command += consoleInput.KeyChar.ToString();
                }
              
            }          
        }
        private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();      // it might be better to use --> string indata = sp.ReadLine();
            Console.Write(indata);
        }

        private static void ClearCurrentConsoleLine()
        {
            int currentLineCursor = Console.CursorTop;
            Console.SetCursorPosition(6, Console.CursorTop);
            Console.Write(new string(' ', Console.WindowWidth));
            Console.SetCursorPosition(6, currentLineCursor);
        }
     
    }

}

我只找到固件文件并将固件文件的名称存储在变量中。

我尝试XModem c#在 Github 中搜索。

我发现的是https://github.com/yang-chanhong/XModem

我不确定该代码是否可以在我的控制台应用程序中使用。

.Net 中是否有任何用于 XModem 的第三方库可以帮助更新固件?

我的控制台应用程序正在使用.Net Framework 4.7.2.

..................................................... ...........

更新1:

在我的应用程序中添加app1.config文件后

在此处输入图像描述

并复制XmodeProtocol中的配置,

我输入using XModemProtocol;Program.cs.

在此处输入图像描述

错误出现了:

在此处输入图像描述

看起来XmodeProtocol 的配置不够清晰,无法实现。

我错过了XmodeProtocol的东西README.md吗?

..................................................... ……

更新 2:

我在XmodeProtocol上下载了源代码

并单击add reference

在此处输入图像描述

选择Project选择并按下Browse按钮。

在此处输入图像描述

我没有看到组件文件。

下一步我应该怎么做才能添加项目作为参考?

标签: c#serial-portconsole-applicationfirmware

解决方案


推荐阅读