C# 51. 调用dll库Ymodem操作(串口)

  SerialRxCallBackDelegate d1;
  Serial scom;
  YmodemSendCallBackDelegate d2;
  YmodemSend.fileStruct fileStruct1 = new YmodemSend.fileStruct();
  YmodemSend ymodemSend;
  public Form1()
  {
      InitializeComponent();
      d1 = new SerialRxCallBackDelegate(SerialRxCallBackFunc);
      scom = new Serial(9600,10000,d1);
      d2 = new YmodemSendCallBackDelegate(callbackFunc);
      ymodemSend = new YmodemSend(d2);
  }

public void YmodemCallbackFunc(CallBackStruct s1)
{            
    Invoke((EventHandler)(delegate
    {
        if((s1.type & (int)CallBackTypeEnum.TxRxData) > 0)      //需发送数据
        {
            scom.Send(s1.dataBuf,0,s1.dataLength);
        }
        if((s1.type & (int)CallBackTypeEnum.btn) > 0)           //修改按钮
        {
            buttonUpgrade.Text = s1.btnStr;
        }
        if((s1.type & (int)CallBackTypeEnum.AppendInfo) > 0)     //增加提示
        {
            textBoxOutput2.Text += s1.infoStr;
        }
        else if ((s1.type & (int)CallBackTypeEnum.OverWriteInfo) > 0)     //更新提示
        {
            textBoxOutput2.Text = s1.infoStr;
        } 
        if((s1.upgradeStatus == UpgradeStatusEnum.UpgradeFail) || (s1.upgradeStatus == UpgradeStatusEnum.UpgradeSuccess))
        {
            ymodemSend.YmodemStatusReset();
            upgradeFlag = false;
        }
    }));
}

public void SerialRxCallBackFunc(byte[] buf, int len)
{
    Invoke((EventHandler)(delegate
    {
        ymodemSend.RxCallBack(buf,len);
    }
      )
    );

}

private void comboBoxComm_DropDown(object sender, EventArgs e)
{
    //获取电脑当前可用串口并添加到选项列表中
    comboBoxComm.Items.Clear(); // 清除串口
    comboBoxComm.Items.AddRange(scom.GetAllScomName());
}

private string g_filePath;

private void buttonOpenfile_Click(object sender, EventArgs e)
{
    string path = null;
    path = fileUtil.OpenFile("bin");
    textBoxOutput1.Text = path;
    byte[] fileBuf = new byte[200 * 1024];
    int fileLength;
    if (path != null)
    {
        g_filePath = path;
        bool ret = fileUtil.ReadFile(out fileBuf, out fileLength, path, 100 * 1024);
        if(ret == true)
        {


        }
    }
}

private bool OpenBin(ref YmodemSend.fileStruct fileStruct1)
{
    byte[] fileBuf = new byte[200 * 1024];
    int fileLength;
    bool ret = fileUtil.ReadFile(out fileBuf, out fileLength, g_filePath, 100 * 1024);
    if (ret == true)
    {
        fileStruct1.fileBuf = new byte[200 * 1024];
        fileStruct1.fileName = new byte[256];
        Array.Copy(fileBuf, fileStruct1.fileBuf, fileLength);
        fileStruct1.fileLength = fileLength;
        string fileName = fileUtil.GetFileName(g_filePath);
        byte[] name = System.Text.Encoding.Default.GetBytes(fileName);
        Array.Copy(name, fileStruct1.fileName, fileName.Length);
        fileStruct1.fileNameLength = fileName.Length;
        return true;
    }
    else
    {
        return false;
    }
}

private bool upgradeFlag = false;

private void buttonUpgrade_Click(object sender, EventArgs e)
{
    if (upgradeFlag == false)
    {
        if(OpenBin(ref fileStruct1) == true)
        {
            yymodemSend.YmodemSendSet(fileStruct1, YmodemCallbackFunc);
            ymodemSend.ButtonDeal();
            upgradeFlag = true;
        }
    }
    else
    {
        upgradeFlag = false;
        ymodemSend.ButtonDeal();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值