C# 52. 调用dll库Ymodem操作(TCP)

SerialRxCallBackDelegate d1;
TcpClient tcpClient;
YmodemSendCallBackDelegate d2;
YmodemSend.fileStruct fileStruct1 = new YmodemSend.fileStruct();
YmodemSend ymodemSend;
TcpClientRxCallBackDelegate d3;
public Form1()
{
    InitializeComponent();
    d3 = new TcpClientRxCallBackDelegate(TcpClientRxCallBackFunc);
    tcpClient = new TcpClient();            
	d2 = new YmodemSendCallBackDelegate(callbackFunc);
    ymodemSend = new YmodemSend(d2);
}

public void YmodemCallbackFunc(CallBackStruct s1)
{            
    Invoke((EventHandler)(delegate
    {
        if((s1.type & (int)CallBackTypeEnum.TxRxData) > 0)      //需发送数据
        {
            tcpClient.Send(s1.dataBuf, 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;
        }
    }));
}

private void TcpClientRxCallBackFunc(byte[] buf, int length)
{
    Invoke((EventHandler)(delegate
    {
        ymodemSend.RxCallBack(buf, length);
    }));
}

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();
    }
}

private void buttonTcpOpen_Click(object sender, EventArgs e)
{
    int flag;
    string str;
    flag = tcpClient.StartClient(textBoxIP.Text.Trim(), Convert.ToInt32(this.textBoxPort.Text.Trim()), out str, TcpClientRxCallBackFunc);
    if (flag == 1)   //已连接
    {
        buttonTcpConnect.Text = str;
        buttonTcpConnect.BackColor = Color.LightCoral;
    }
    else if (flag == 2)  //超时等,未连接
    {
        buttonTcpConnect.Text = str;
        buttonTcpConnect.BackColor = Color.LimeGreen;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值