{ public class SerialPortCommunication { string filename = null; private Form1 mainForm; //Reference to The Main Form //long TimeNow=DateTime.Now.ToFileTimeUtc(); //string UploadedFileName = "C:\\Documents and Settings\\mpeloso\\Desktop\\" + TimeNow.ToString() + ".txt"; //bool BSLMemAccessWarning=true; int _err = 1; const int SMALL_RAM_model = 0; //Ram Model Small const int LARGE_RAM_model = 1; //Ram Model Large const int loadedModel = 0x4567; const int ERR_VERIFY_FAILED = 98; const int ERR_ERASE_CHECK_FAILED = 97; const int MAX_FRAME_COUNT = 16; const int MAX_ERR_COUNT = 5; const int ACTION_PROGRAM = 0x01; const int ACTION_VERIFY = 0x02; const int ACTION_ERASE_CHECK = 0x04; const int ACTION_PASSWD = 0x08; const int ACTION_ERASE_CHECK_FAST = 0x10; const int ERR_NONE = 0; const int BSL_TXPWORD = 0x10; //Command To Send Password const int BSL_TXBLK = 0x12; //Command to Transmit Block const int BSL_RXBLK = 0x14; //Command to Recieve Block const int BSL_MERAS = 0x18; //Command to Mass Erase const int BSL_ECHECK = 0x1C; const int BSL_MEMOFFSET = 0x21; const int ERR_BSL_SYNC = 99; const int ERR_COM = 1; //Error Communication const int ERR_RX_HDR_TIMEOUT = 6; //Error Hardware Timeout const int ERR_RX_NAK = 7; //Error Password Not Accepted const int ERR_CMD_NOT_COMPLETED = 8; //Error Command Not Completed const int ERR_CMD_FAILED = 9; //Error Command Failed const int CMD_FAILED = 0x70; const int DATA_FRAME = 0x80; const int DATA_ACK = 0x90; //Data Acknowledged const int DATA_NAK = 0xA0; //Data Not Accepted const int DEFAULT_TIMEOUT = 500; const int DEFAULT_PROLONG = 10; const int MAX_FRAME_SIZE = 256; const int MAX_DATA_BYTES = 250; static uint bslerrbuf = 0x021E; static int maxData = 240; //Keeps the Data Frame from overrunning private int byteCtr = 0; //Byte Counter private byte rxHeader = 0; private uint prolongFactor = DEFAULT_PROLONG; private uint timeout = DEFAULT_TIMEOUT; private int lastError; private byte[] blkout = new byte[MAX_DATA_BYTES]; /* Transmit buffer */ private byte seqNo, reqNo; private byte[] rxFrame = new byte[MAX_FRAME_SIZE]; private byte[] blkin = new byte[MAX_DATA_BYTES]; /* Receive buffer */ private int error = ERR_NONE; private int meraseCycles = 20; bool ProgramF = true; bool MSP430X = false; const int MODE_BSL = 1; string passwdFile = null; const ushort protocolMode = MODE_BSL; const byte BSL_SYNC = 0x80; private SerialPort _serialPort; //Reference to SerialPort private List AllPorts; public SerialPortCommunication(Form1 parentForm,string PortName)