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();
}
}
C# 51. 调用dll库Ymodem操作(串口)
最新推荐文章于 2024-10-04 16:03:01 发布