引导加载程序流控制 C#
Bootloader Flow Control C#
我正在努力通过 RS485 对 MCU 板进行编程。我已经完成了访问引导加载程序的代码部分,没问题。我的问题出在这段代码中:
int xon_off = ComPort.ReadChar();
if (xon_off == send_data) {
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader("C:/Users/user/Desktop/x.hex");
while ((line = file.ReadLine()) != " ") // reads until end of file
{
write_line: line = file.ReadLine();
if (xon_off == send_data) {
ComPort.Write(line);
//System.Threading.Thread.Sleep(500);
counter++;
xon_off = ComPort.ReadByte(); // should be x_off
error_check = ComPort.ReadByte(); // will give line complete or error
xon_off = ComPort.ReadByte(); // should be x_on
} else if (xon_off == stop_data) {
read_again: xon_off = ComPort.ReadByte();
if (xon_off == send_data) {
goto write_line;
} else {
goto read_again;
}
}
}
我的问题是流量控制 (x_on/x_off/eof/etc)。当前代码的方式是,它可以发送页面错误,并且该工具一直发送,就像什么都没有一样,所以很明显我的 read/compare 语句已关闭。有人能帮我找出为什么当它发送页面错误时,我的代码认为它发送的是 x_on?
注意:x_on 是上面设置为 0x11 的变量,x_off 是上面设置为 0x13 的变量,只是为了说明。
注意:一旦我弄明白了这一点,我的下一步就是删除 goto 语句……我知道它们很糟糕,但它们在这里起作用。
好的,据我所知,更好的选择是:
首先,删除 goto 语句,它们很可怕,永远不应该使用。至于无法混合 chars/hex 值的问题,每个作为 int 引入的 char 都将分配给它的 hex 值(在程序中转换为 int32)。当引导加载程序发送 0x11 时,它很可能将其作为字符发送,它将作为不可打印的 ASCII 字符进入您的软件,但如果您使用调试器并查看它实际以什么形式进入,则仍然有 0x11。所以我的建议是做一个 readbyte,然后在软件中转换为 int32,然后为你想做的做一个 switch statement/state machine。
我正在努力通过 RS485 对 MCU 板进行编程。我已经完成了访问引导加载程序的代码部分,没问题。我的问题出在这段代码中:
int xon_off = ComPort.ReadChar();
if (xon_off == send_data) {
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader("C:/Users/user/Desktop/x.hex");
while ((line = file.ReadLine()) != " ") // reads until end of file
{
write_line: line = file.ReadLine();
if (xon_off == send_data) {
ComPort.Write(line);
//System.Threading.Thread.Sleep(500);
counter++;
xon_off = ComPort.ReadByte(); // should be x_off
error_check = ComPort.ReadByte(); // will give line complete or error
xon_off = ComPort.ReadByte(); // should be x_on
} else if (xon_off == stop_data) {
read_again: xon_off = ComPort.ReadByte();
if (xon_off == send_data) {
goto write_line;
} else {
goto read_again;
}
}
}
我的问题是流量控制 (x_on/x_off/eof/etc)。当前代码的方式是,它可以发送页面错误,并且该工具一直发送,就像什么都没有一样,所以很明显我的 read/compare 语句已关闭。有人能帮我找出为什么当它发送页面错误时,我的代码认为它发送的是 x_on?
注意:x_on 是上面设置为 0x11 的变量,x_off 是上面设置为 0x13 的变量,只是为了说明。
注意:一旦我弄明白了这一点,我的下一步就是删除 goto 语句……我知道它们很糟糕,但它们在这里起作用。
好的,据我所知,更好的选择是: 首先,删除 goto 语句,它们很可怕,永远不应该使用。至于无法混合 chars/hex 值的问题,每个作为 int 引入的 char 都将分配给它的 hex 值(在程序中转换为 int32)。当引导加载程序发送 0x11 时,它很可能将其作为字符发送,它将作为不可打印的 ASCII 字符进入您的软件,但如果您使用调试器并查看它实际以什么形式进入,则仍然有 0x11。所以我的建议是做一个 readbyte,然后在软件中转换为 int32,然后为你想做的做一个 switch statement/state machine。