gergalern February 2016

Bootloader Flow Control C#

I'm working to program a MCU board over RS485. I have the section of code done to access the bootloader no problem. My problem is in this code:

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

My issue is with the flow control (x_on/x_off/eof/etc). The way the current code is, it can send a page error, and the tool keeps sending like it was nothing, so obviously my read/compare statements are off. Can someone help me find out why when it sends a page error, my code thinks it's sending an x_on?

NOTE: x_on is a variable above set to 0x11 and x_off is a var above set to 0x13 just to clarify.

NOTE: Once I get this figured out, my next step is to remove the goto statements...they're gross I know, but they worked for here.

Answers


wgdvs February 2016

Ok so from what I can understand, here's what the better option is: First off, do remove the goto statements, they're horrindious and should never be used. As for the issue of not being able to mix chars/hex values, every char when brought in as an int will be assigned it's hex value (convert to int32 in the program). When the bootloader sends the 0x11, it will more than likely send it as a char, which will come into your software as an unprintable ASCII char, but still have the 0x11 if you use your debugger and see what it's actually coming in as. So my suggestion is to do a readbyte, then do a convert to int32 in software, and do a switch statement/state machine for what you want done.

Post Status

Asked in February 2016
Viewed 3,259 times
Voted 6
Answered 1 times

Search




Leave an answer