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;
}
}
C# 52. 调用dll库Ymodem操作(TCP)
最新推荐文章于 2024-04-20 10:11:45 发布