Przeglądaj źródła

C# BootLoader Base operate Complete

YJ 6 lat temu
rodzic
commit
1a90f50665

+ 1 - 0
Basic_Terminal/Basic_Terminal.csproj

@@ -50,6 +50,7 @@
50 50
     <Compile Include="Func\Crc16.cs" />
51 51
     <Compile Include="Func\Data_Handler.cs" />
52 52
     <Compile Include="Func\FileDownload.cs" />
53
+    <Compile Include="Func\Update_Serial.cs" />
53 54
     <Compile Include="Wnd\Debug.cs">
54 55
       <SubType>Form</SubType>
55 56
     </Compile>

+ 54 - 55
Basic_Terminal/Func/Bluecell_BootProtocol.cs

@@ -20,7 +20,13 @@ namespace Basic_Terminal
20 20
         bluecell_data,
21 21
 
22 22
     };
23
-    
23
+    enum Updateseq
24
+    {
25
+        Bluecell_Reset = 0,
26
+        Bluecell_Firmupdate_start,
27
+        Bluecell_Firmupdate_sending,
28
+        Bluecell_Firmupdate_end,
29
+    };
24 30
     class Bluecell_BootProtocol
25 31
     {
26 32
         Serial serial;
@@ -30,87 +36,80 @@ namespace Basic_Terminal
30 36
         public const byte Bluecell_Header2 = 0x55;//ASCII : U
31 37
         public const byte Bluecell_Header3 = 0x45;//ASCII : E
32 38
         /*bluecell type*/
33
-        public const byte Bluecell_Reset = 0x52;//ASCII : R
34
-        public const byte Bluecell_Firmupdate_start    = 0x55;//ASCII : U
35
-        public const byte Bluecell_Firmupdate_sending  = 0x53;//ASCII : S
36
-        public const byte Bluecell_Firmupdate_end      = 0x65;//ASCII : e
39
+
37 40
 
38 41
         public const int bluecell_Firmupdate_sendlength = 1024;
42
+        public const byte bluecell_header    = 4;
43
+        public const byte bluecell_type      = 1;
44
+        public const byte bluecell_length    = 2;
45
+        public const byte bluecell_updatecnt = 2;
46
+        public const byte bluecell_crc16     = 2;
47
+        public const byte bluecell_nessarybyte = bluecell_header + bluecell_type + bluecell_length + bluecell_updatecnt + bluecell_crc16;
39 48
         Crc16 crc16 = new Crc16();
40 49
 
41 50
         public int Bluecell_Firmupdate_sendlength() {
42 51
             return bluecell_Firmupdate_sendlength;
43 52
         }
44
-
45
-        public byte[] Boot_Reset(object serial)
53
+        private void BootHeaderput(ref byte[] fix_data)
46 54
         {
47
-            this.serial = (Serial)serial;
48
-            byte[] fix_data = new byte[11];
49
-            //Array.Clear(data, 0, data.Length);
50 55
             fix_data[(int)Bluepro_t.bluecell_header0] = Bluecell_Header0;
51 56
             fix_data[(int)Bluepro_t.bluecell_header1] = Bluecell_Header1;
52 57
             fix_data[(int)Bluepro_t.bluecell_header2] = Bluecell_Header2;
53 58
             fix_data[(int)Bluepro_t.bluecell_header3] = Bluecell_Header3;
54
-            fix_data[(int)Bluepro_t.bluecell_type]    = Bluecell_Reset;
59
+        }
60
+
61
+        public byte[] Boot_Reset(object serial,byte[] Updatedata)
62
+        {
63
+            this.serial = (Serial)serial;
64
+            byte[] fix_data = new byte[11];
65
+            //Array.Clear(data, 0, data.Length);
66
+            BootHeaderput(ref fix_data);
67
+            fix_data[(int)Bluepro_t.bluecell_type]    = (byte)Updateseq.Bluecell_Reset;
55 68
             fix_data[(int)Bluepro_t.bluecell_length_h]  = 0;
56 69
             fix_data[(int)Bluepro_t.bluecell_length_l]  = 5;
57
-            fix_data[(int)Bluepro_t.bluecell_updatecnt_h] = 0;
58
-            fix_data[(int)Bluepro_t.bluecell_updatecnt_l] = 0;
70
+            fix_data[(int)Bluepro_t.bluecell_updatecnt_h] = Convert.ToByte((Updatedata.Length & 0xFF00) >> 8);
71
+            fix_data[(int)Bluepro_t.bluecell_updatecnt_l] = Convert.ToByte((Updatedata.Length & 0x00FF));
59 72
 
60
-            fix_data[(int)Bluepro_t.bluecell_data + 0] = Convert.ToByte((crc16.CRC16_Generate(fix_data, fix_data.Length) & 0xFF00) >> 8);
61
-            fix_data[(int)Bluepro_t.bluecell_data + 1] = Convert.ToByte((crc16.CRC16_Generate(fix_data, fix_data.Length) & 0x00FF));
73
+            fix_data[(int)Bluepro_t.bluecell_data + 0] = Convert.ToByte((crc16.CRC16_Generate(fix_data, fix_data.Length - 4) & 0xFF00) >> 8);
74
+            fix_data[(int)Bluepro_t.bluecell_data + 1] = Convert.ToByte((crc16.CRC16_Generate(fix_data, fix_data.Length - 4) & 0x00FF));
62 75
             this.serial.Serial_DataSend(fix_data, fix_data.Length);
63 76
             return fix_data;            
64 77
         }
65
-        public byte[] Boot_DataStart()
78
+     
79
+        public byte[] Boot_DataSending(byte[] update_data,UInt16 length, UInt16 updatacnt)
66 80
         {
67 81
             //Array.Clear(data, 0, data.Length);
68
-            byte[] fix_data = new byte[11];
69
-            fix_data[(int)Bluepro_t.bluecell_header0] = Bluecell_Header0;
70
-            fix_data[(int)Bluepro_t.bluecell_header1] = Bluecell_Header1;
71
-            fix_data[(int)Bluepro_t.bluecell_header2] = Bluecell_Header2;
72
-            fix_data[(int)Bluepro_t.bluecell_header3] = Bluecell_Header3;
73
-            fix_data[(int)Bluepro_t.bluecell_type]    = Bluecell_Firmupdate_start;
74
-            fix_data[(int)Bluepro_t.bluecell_length_h]  = 0;
75
-            fix_data[(int)Bluepro_t.bluecell_length_l]  = 3;
76
-            fix_data[(int)Bluepro_t.bluecell_updatecnt_h] = 0;
77
-            fix_data[(int)Bluepro_t.bluecell_updatecnt_l] = 0;
78
-            fix_data[(int)Bluepro_t.bluecell_data + 0] = Convert.ToByte((crc16.CRC16_Generate(fix_data, fix_data.Length) & 0xFF00) >> 8);
79
-            fix_data[(int)Bluepro_t.bluecell_data + 1] = Convert.ToByte((crc16.CRC16_Generate(fix_data, fix_data.Length) & 0x00FF));
82
+            byte[] fix_data = new byte[bluecell_Firmupdate_sendlength];
83
+            //BootHeaderput(ref fix_data);
84
+            //length += 5;
85
+            Array.Copy(update_data, (int)Bluepro_t.bluecell_data, fix_data, (int)Bluepro_t.bluecell_data, length);
86
+            BootHeaderput(ref fix_data);
87
+            fix_data[(int)Bluepro_t.bluecell_type] = (byte)Updateseq.Bluecell_Firmupdate_sending;
88
+            fix_data[(int)Bluepro_t.bluecell_length_h] = Convert.ToByte((length & 0xFF00) >> 8);
89
+            fix_data[(int)Bluepro_t.bluecell_length_l] = Convert.ToByte((length & 0x00FF));
90
+            fix_data[(int)Bluepro_t.bluecell_updatecnt_h] = Convert.ToByte((updatacnt & 0xFF00) >> 8);
91
+            fix_data[(int)Bluepro_t.bluecell_updatecnt_l] = Convert.ToByte((updatacnt & 0x00FF));
92
+
93
+            //Array.Copy(update_data, 0, fix_data, (int)Bluepro_t.bluecell_data, length-3);
94
+            fix_data[length + 9] = Convert.ToByte((crc16.CRC16_Generate(fix_data, length + 5) & 0xFF00) >> 8);
95
+            fix_data[length + 10] = Convert.ToByte(crc16.CRC16_Generate(fix_data, length + 5) & 0x00FF);
80 96
             return fix_data;
81 97
         }
82
-        public byte[] Boot_DataSending(byte[] data,int length)
83
-        {
84
-            //Array.Clear(data, 0, data.Length);
85
-            byte[] fix_data = new byte[5];
86
-            fix_data[(int)Bluepro_t.bluecell_header0] = Bluecell_Header0;
87
-            fix_data[(int)Bluepro_t.bluecell_header1] = Bluecell_Header1;
88
-            fix_data[(int)Bluepro_t.bluecell_header2] = Bluecell_Header2;
89
-            fix_data[(int)Bluepro_t.bluecell_header3] = Bluecell_Header3;
90
-            fix_data[(int)Bluepro_t.bluecell_type]    = Bluecell_Firmupdate_sending;
91
-            fix_data[(int)Bluepro_t.bluecell_length_h] = 0;
92
-            fix_data[(int)Bluepro_t.bluecell_length_l] = 3;
93
-            fix_data[(int)Bluepro_t.bluecell_updatecnt_h] = 0;
94
-            fix_data[(int)Bluepro_t.bluecell_updatecnt_l] = 0;
95
-            fix_data[(int)Bluepro_t.bluecell_data + 0] = Convert.ToByte((length & 0xFF00) >> 8);
96
-            fix_data[(int)Bluepro_t.bluecell_data + 1] = Convert.ToByte((length & 0x00FF));
97
-            return data;
98
-        }
99
-        public byte[] Boot_DataEnd(byte[] data,int length)
98
+        public byte[] Boot_DataEnd(byte[] update_data, int length)
100 99
         {
101 100
             //Array.Clear(data, 0, data.Length);
102 101
             byte[] fix_data = new byte[11];
103
-            fix_data[(int)Bluepro_t.bluecell_header0] = Bluecell_Header0;
104
-            fix_data[(int)Bluepro_t.bluecell_header1] = Bluecell_Header1;
105
-            fix_data[(int)Bluepro_t.bluecell_header2] = Bluecell_Header2;
106
-            fix_data[(int)Bluepro_t.bluecell_header3] = Bluecell_Header3;
107
-            fix_data[(int)Bluepro_t.bluecell_type]    = Bluecell_Firmupdate_end;
108
-            fix_data[(int)Bluepro_t.bluecell_length_h] = 0;
109
-            fix_data[(int)Bluepro_t.bluecell_length_l] = 3;
102
+            Array.Copy(update_data, (int)Bluepro_t.bluecell_data, fix_data, (int)Bluepro_t.bluecell_data, length);
103
+            BootHeaderput(ref fix_data);
104
+            fix_data[(int)Bluepro_t.bluecell_type]    = (byte)Updateseq.Bluecell_Firmupdate_end;
105
+            fix_data[(int)Bluepro_t.bluecell_length_h] = Convert.ToByte((length & 0xFF00) >> 8);
106
+            fix_data[(int)Bluepro_t.bluecell_length_l] = Convert.ToByte((length & 0x00FF));
110 107
             fix_data[(int)Bluepro_t.bluecell_updatecnt_h] = 0;
111 108
             fix_data[(int)Bluepro_t.bluecell_updatecnt_l] = 0;
112
-            fix_data[(int)Bluepro_t.bluecell_data + 0] = Convert.ToByte((length & 0xFF00) >> 8);
113
-            fix_data[(int)Bluepro_t.bluecell_data + 1] = Convert.ToByte((length & 0x00FF));
109
+
110
+            //Array.Copy(update_data, 0, fix_data, (int)Bluepro_t.bluecell_data, length-3);
111
+            fix_data[length + 9] = Convert.ToByte((crc16.CRC16_Generate(fix_data, length + 5) & 0xFF00) >> 8);
112
+            fix_data[length + 10] = Convert.ToByte(crc16.CRC16_Generate(fix_data, length + 5) & 0x00FF);
114 113
             return fix_data;
115 114
         }
116 115
     }

+ 22 - 3
Basic_Terminal/Func/Crc16.cs

@@ -44,7 +44,7 @@ namespace Basic_Terminal
44 44
             0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
45 45
             0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
46 46
         };
47
-
47
+        const int POLYNOMIAL = 0x131; // P(x) = x^8 + x^5 + x^4 + 1 = 100110001
48 48
         public enum EtError
49 49
         {
50 50
             CHECKSUM_ERROR = 0,
@@ -57,7 +57,7 @@ namespace Basic_Terminal
57 57
         {
58 58
             byte dt = 0;
59 59
             ushort crc16 = 0;
60
-            ushort index = 0;
60
+            ushort index = 4;
61 61
             len *= 8;
62 62
             for (crc16 = (ushort)0x0000; len >= 8; len -= 8)
63 63
             {
@@ -91,7 +91,7 @@ namespace Basic_Terminal
91 91
         {
92 92
             byte dt = 0;
93 93
             ushort crc16 = 0;
94
-            ushort index = 0;
94
+            ushort index = 4;
95 95
 
96 96
             len *= 8;
97 97
             for (crc16 = (ushort)0x0000; len >= 8; len -= 8, index++)
@@ -121,5 +121,24 @@ namespace Basic_Terminal
121 121
             }
122 122
             return (crc16 == checksum ? EtError.CHECKSUM_ERROR : EtError.NO_ERROR);
123 123
         }
124
+
125
+        byte STH30_CreateCrc(byte[] data, byte nbrOfBytes)
126
+        {
127
+            byte bit;        // bit mask
128
+            int crc = 0xFF; // calculated checksum
129
+            byte byteCtr;    // byte counter
130
+            byte index = 1;
131
+            // calculates 8-Bit checksum with given polynomial
132
+            for (byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++)
133
+            {
134
+                crc ^= (data[index++]);
135
+                for (bit = 8; bit > 0; --bit)
136
+                {
137
+                    if (Convert.ToBoolean(crc & 0x80)) { crc = (crc << 1) ^ POLYNOMIAL; }
138
+                    else crc = (crc << 1);
139
+                }
140
+            }
141
+            return (byte)crc;
142
+        }
124 143
     }
125 144
 }

+ 34 - 23
Basic_Terminal/Func/Data_Handler.cs

@@ -8,41 +8,52 @@ namespace Basic_Terminal
8 8
 {
9 9
     class Data_Handler
10 10
     {
11
-        FileDownload fileDownload;
11
+        //FileDownload fileDownload;
12
+        Update_Serial fileDownload;
13
+        
12 14
         public void Recv_dataCheck(object fileDownload, byte[] data)
13 15
         {
14 16
             Boolean HeaderCheck;
15
-            DataSeq seq = DataSeq.UpdateResetOK;
17
+            byte seq = data[1];
16 18
             if (fileDownload != null)
17 19
             {
18
-                this.fileDownload = (FileDownload)fileDownload;
20
+              // this.fileDownload = (FileDownload)fileDownload;
21
+                this.fileDownload = (Update_Serial)fileDownload;
19 22
             }
20 23
             else
21 24
             {
22
-                this.fileDownload = new FileDownload();
25
+            //    this.fileDownload = new FileDownload();
26
+                this.fileDownload = new Update_Serial();
23 27
             }
24
-            HeaderCheck = this.fileDownload.Serial_HeaderCheck(data);
25
-            
26
-            int DataAckcnt = data[(int)Bluepro_t.bluecell_type + 1];
27
-
28
-            if (HeaderCheck == false)
29
-            {
30
-                return;
31
-            }
32
-            else
28
+            if (this.fileDownload.Update_ready == true)
33 29
             {
34 30
                 
35
-                switch (seq)
31
+               /* HeaderCheck = this.fileDownload.Serial_HeaderCheck(data);
32
+
33
+
34
+
35
+                if (HeaderCheck == false)
36 36
                 {
37
-                    case DataSeq.UpdateResetOK:
38
-                    case DataSeq.UpdateStartOK:
39
-                    case DataSeq.UpdateSendingOK:
40
-                        this.fileDownload.UpdateFileSend(data, DataAckcnt);
41
-                        break;
42
-                    case DataSeq.UpdateEndOK:
43
-                        break;
44
-                    default:
45
-                        break;
37
+                    return;
38
+                }
39
+                else*/
40
+                {
41
+
42
+                    switch (seq)
43
+                    {
44
+                        case 0x0a:
45
+                        case 0x0b:
46
+                        case 0x11:
47
+                        case 0x22:
48
+                            //int DataAckcnt = data[(int)Bluepro_t.bluecell_type + 1];
49
+                            //this.fileDownload.UpdateFileSend(data, DataAckcnt);
50
+                            this.fileDownload.Termianl__Operate(data);
51
+                            break;
52
+                        case 3:
53
+                            break;
54
+                        default:
55
+                            break;
56
+                    }
46 57
                 }
47 58
             }
48 59
         }

+ 39 - 12
Basic_Terminal/Func/FileDownload.cs

@@ -20,13 +20,16 @@ namespace Basic_Terminal
20 20
     {
21 21
         
22 22
         Bluecell_BootProtocol Bluecell_BootProtocol = new Bluecell_BootProtocol();
23
-
23
+        byte[] All_update_data;
24 24
         /***
25 25
          *Data File open
26 26
         */
27 27
         OpenFileDialog ofd;
28 28
         Serial serial;
29
-        public string ShowFileOpenDialog(object serial,object ofd)
29
+
30
+        public bool Update_ready = false;
31
+        
32
+        public string ShowFileOpenDialog(ref object serial,object ofd)
30 33
         {
31 34
             this.ofd = (OpenFileDialog)ofd;
32 35
             this.serial = (Serial)serial;
@@ -40,9 +43,15 @@ namespace Basic_Terminal
40 43
                 string fileName = this.ofd.SafeFileName;                //File명과 확장자를 가지고 온다.
41 44
                 string fileFullName = this.ofd.FileName;//File경로와 File명을 모두 가지고 온다.
42 45
                 string filePath = fileFullName.Replace(fileName, "");//File경로만 가지고 온다.
43
-                Bluecell_BootProtocol.Boot_Reset(serial);
46
+               
47
+
44 48
                 this.serial.FileDownloadClass_Get(this);
45
-                //UpdateFileSend(File.ReadAllBytes(this.ofd.FileName));
49
+                All_update_data = File.ReadAllBytes(this.ofd.FileName);
50
+                Bluecell_BootProtocol.Boot_Reset(this.serial, All_update_data);
51
+                //byte[] updatedata = Bluecell_BootProtocol.Boot_DataSending(All_update_data, (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11), (UInt16)0);
52
+                //this.serial.Serial_DataSend(updatedata);
53
+
54
+                Update_ready = true;
46 55
                 return fileFullName;
47 56
             }
48 57
             else if (dr == DialogResult.Cancel)//취소버튼 클릭시 또는 ESC키로 파일창을 종료 했을경우
@@ -51,6 +60,7 @@ namespace Basic_Terminal
51 60
             }
52 61
             return "";
53 62
         }
63
+
54 64
 #if false
55 65
         public void Recv_dataCheck(byte[] data)
56 66
         {
@@ -76,33 +86,50 @@ namespace Basic_Terminal
76 86
             }
77 87
         }
78 88
 #endif
89
+        int sourceindex = 0;
79 90
         public void UpdateFileSend(byte[] data,int cnt)
80 91
         {
92
+            if (this.serial == null)
93
+                return;
81 94
             /*Define*/
82
-            int Quotient = 0,remainder = 0;
83
-            int sourceindex = 0;
95
+            int Quotient = 0, remainder = 0;
96
+           
84 97
             byte[] updatedata = new byte[Bluecell_BootProtocol.bluecell_Firmupdate_sendlength];
85 98
             /*Filse size */
86
-            Quotient = data.Length / Bluecell_BootProtocol.bluecell_Firmupdate_sendlength;
87
-            remainder = data.Length % Bluecell_BootProtocol.bluecell_Firmupdate_sendlength;
99
+            Quotient = All_update_data.Length / (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11);
100
+            remainder = All_update_data.Length % (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11);
88 101
             /*file copy*/
89
-            Array.Copy(data, sourceindex, updatedata, 0,Bluecell_BootProtocol.bluecell_Firmupdate_sendlength);
102
+            // Header : 4byte + Type : 1byte + Length : 2byte + update_cnt : 2byte = 9byte  /* CRC :2byte  */ : Total : 11byte
103
+            Array.Copy(All_update_data, sourceindex, updatedata, Bluecell_BootProtocol.bluecell_nessarybyte - 2, (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11)); 
90 104
             /*file write*/
91 105
             if (remainder > 0 && cnt > Quotient)
92 106
             {
93 107
                 this.serial.Serial_DataSend(data, remainder);
108
+                sourceindex = 0;
109
+                All_update_data = null;
94 110
             }
95 111
             else
96 112
             {
97
-                this.serial.Serial_DataSend(data, cnt * Bluecell_BootProtocol.bluecell_Firmupdate_sendlength);
113
+                data = Bluecell_BootProtocol.Boot_DataSending(updatedata, (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11), (UInt16)cnt);
114
+                this.serial.Serial_DataSend(data,Bluecell_BootProtocol.bluecell_Firmupdate_sendlength);
98 115
                 /*send index modify*/
99
-                sourceindex = cnt * Bluecell_BootProtocol.bluecell_Firmupdate_sendlength;
116
+                sourceindex = (cnt + 1)  * (Bluecell_BootProtocol.bluecell_Firmupdate_sendlength - 11);
100 117
             }
101 118
         }
102
-             
119
+        public static string AsciiToHex(string asciiString)
120
+        {
121
+            StringBuilder builder = new StringBuilder();
122
+            foreach (char c in asciiString)
123
+            {
124
+                builder.Append(Convert.ToInt32(c).ToString("X"));
125
+            }
126
+            return builder.ToString();
127
+        }
128
+
103 129
         public Boolean Serial_HeaderCheck(byte[] data)
104 130
         {
105 131
             Boolean ret = false;
132
+
106 133
             if (data[(int)Bluepro_t.bluecell_header0] == Bluecell_BootProtocol.Bluecell_Header0
107 134
                 && data[(int)Bluepro_t.bluecell_header1] == Bluecell_BootProtocol.Bluecell_Header1
108 135
                 && data[(int)Bluepro_t.bluecell_header2] == Bluecell_BootProtocol.Bluecell_Header2

+ 50 - 9
Basic_Terminal/Func/Serial.cs

@@ -16,7 +16,8 @@ namespace Basic_Terminal
16 16
         private Debug Debug = new Debug();    // Teminal Text Wnd Open
17 17
         public string Serial_Name { get => serialPort.PortName;  set => serialPort.PortName = value;}
18 18
         Data_Handler data_Handler = new Data_Handler();
19
-        FileDownload fileDownload;
19
+        //FileDownload fileDownload;
20
+        Update_Serial fileDownload;
20 21
         public void Serial_Initialize(ref ComboBox cb_port)
21 22
         {
22 23
             
@@ -30,6 +31,7 @@ namespace Basic_Terminal
30 31
             }
31 32
             cb_port.EndUpdate();
32 33
             //SerialPort 초기 설정.
34
+            serialPort.Encoding = Encoding.GetEncoding("Windows-1252");
33 35
             cb_port.DataSource =  SerialPort.GetPortNames();
34 36
             serialPort.PortName = Serial_Name = cb_port.SelectedItem.ToString();
35 37
             serialPort.BaudRate = (int)115200;
@@ -68,17 +70,41 @@ namespace Basic_Terminal
68 70
             return arr_byteStr;
69 71
 #endif
70 72
         }
71
-
73
+       
72 74
         public void Serial_DataRecvFunction(object sender, SerialDataReceivedEventArgs e)
73 75
         {
74
-            
75
-            string data = serialPort.ReadExisting();
76
-            if(Debug.RadioButton_ascii.Checked == true)
77
-                Debug.Invoke(new StringSend(Debug.Data_Recv_Str), data);
76
+            if (Debug.RadioButton_ascii.Checked == true)
77
+            {
78
+                string data = serialPort.ReadExisting();
79
+                if (Debug.Created)
80
+                    if (Debug.RadioButton_ascii.Checked == true)
81
+                        Debug.Invoke(new StringSend(Debug.Data_Recv_Str), data);
82
+                    else
83
+                        Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(data, true));
84
+
85
+                data_Handler.Recv_dataCheck(this.fileDownload, Str2bytes(data));
86
+            }
78 87
             else
79
-                Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(data,true));
88
+            {
89
+                // 리스트 두개 사용
90
+                int nLnegth = serialPort.BytesToRead;
91
+                byte[] btdata = new byte[nLnegth];
92
+                serialPort.Read(btdata, 0, nLnegth);
80 93
 
81
-            data_Handler.Recv_dataCheck(this.fileDownload, Str2bytes(data));
94
+                if (nLnegth > 0)
95
+                {
96
+                    string tmpSTR = Encoding.Default.GetString(btdata).TrimEnd('\0');
97
+                    Debug.Invoke(new StringSend(Debug.Data_Recv_Str), Str2hex(tmpSTR, true));
98
+                    data_Handler.Recv_dataCheck(this.fileDownload, btdata);
99
+                   
100
+                    // 이부분에서 데이타 처리하는 부분으로 전송하여 사용하면 됨
101
+                }
102
+            }
103
+            /****
104
+             *메모리 누수 방지용 코드
105
+             */
106
+            System.GC.Collect(0, GCCollectionMode.Forced);
107
+            System.GC.WaitForFullGCComplete();
82 108
         }
83 109
         public Boolean Serial_PortOpen(ref Button Btn_Portonoff)
84 110
         {
@@ -134,7 +160,22 @@ namespace Basic_Terminal
134 160
         }
135 161
         public void FileDownloadClass_Get(object filedownload)
136 162
         {
137
-            this.fileDownload = (FileDownload)filedownload;
163
+            //this.fileDownload = (FileDownload)filedownload;
164
+            this.fileDownload = (Update_Serial)filedownload;
165
+        }
166
+        public void Test_Serial()
167
+        {
168
+            byte[] tempdata = new byte[1024];
169
+            for (int i = 0; i < 255; i++)
170
+            {
171
+
172
+                tempdata[i] = Convert.ToByte(i);
173
+                
174
+                
175
+            }
176
+            this.serialPort.Write(tempdata, 0, 255);
177
+            //this.serialPort.Write(tempdata.ToString());
178
+
138 179
         }
139 180
     }
140 181
 }

+ 267 - 0
Basic_Terminal/Func/Update_Serial.cs

@@ -0,0 +1,267 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.IO;
4
+using System.Linq;
5
+using System.Text;
6
+using System.Threading.Tasks;
7
+
8
+
9
+using System.Windows.Forms;
10
+
11
+namespace Basic_Terminal
12
+{
13
+    class Update_Serial
14
+    {
15
+        public byte[] Firmware_byte_load;
16
+        byte Data_Request_Val = 0;
17
+        int FirmTotalcnt = 0;
18
+        int FirmCurrcnt = 0;
19
+        int PreFirmSendingcnt = 0;
20
+        int FirmSendingcnt = 0;
21
+        int pretempupdateret = 0;
22
+        bool update_ready = false;
23
+        const byte blucell_stx = 0xbe;
24
+        const byte blucell_etx = 0xeb;
25
+        const byte Termianl_Controller_Update_Ack = 0x11;
26
+        const byte Termianl_Controller_Update_Nak = 0x22;
27
+        const byte Termianl_Controller_FirmwareUpdateLen = 250;
28
+        const byte Termianl_Reset = 0x0A;
29
+        const byte Termianl_BootStart = 0x0B;
30
+        const int POLYNOMIAL = 0x131; // P(x) = x^8 + x^5 + x^4 + 1 = 100110001
31
+        Bluecell_BootProtocol Bluecell_BootProtocol = new Bluecell_BootProtocol();
32
+        Download_bar download_Bar = new Download_bar();
33
+
34
+
35
+        OpenFileDialog ofd;
36
+        Serial serial;
37
+
38
+
39
+        private byte Data_Request_Func
40
+        {
41
+            get
42
+            { return Data_Request_Val; }
43
+            set
44
+            { Data_Request_Val = value; }
45
+
46
+        }
47
+
48
+        public bool Update_ready { get => update_ready; set => update_ready = value; }
49
+
50
+
51
+        /***
52
+*Data File open
53
+***/
54
+        public string ShowFileOpenDialog(object serial, object ofd)
55
+        {
56
+            byte[] tempdata = new byte[5];
57
+            this.ofd = (OpenFileDialog)ofd;
58
+            this.serial = (Serial)serial;
59
+            //파일오픈창 생성 및 설정
60
+            this.ofd.Title = "업데이터 파일 탐색기";
61
+            this.ofd.FileName = "STM32F103_Termianl_Controller";
62
+            this.ofd.Filter = "bin 파일 (*.bin) | *.bin; | 모든 파일 (*.*) | *.*";
63
+
64
+            //파일 오픈창 로드
65
+            DialogResult dr = this.ofd.ShowDialog();
66
+
67
+            //OK버튼 클릭시
68
+            if (dr == DialogResult.OK)
69
+            {
70
+                //File명과 확장자를 가지고 온다.
71
+                string fileName = this.ofd.SafeFileName;
72
+                //File경로와 File명을 모두 가지고 온다.
73
+                string fileFullName = this.ofd.FileName;
74
+                //File경로만 가지고 온다.
75
+                string filePath = fileFullName.Replace(fileName, "");
76
+
77
+                //출력 예제용 로직
78
+                //  label1.Text = "File Name  : " + fileName;
79
+                // label2.Text = "Full Name  : " + fileFullName;
80
+                // label3.Text = "File Path  : " + filePath;`
81
+                //File경로 + 파일명 리턴
82
+                this.serial.FileDownloadClass_Get(this);
83
+                Data_Request_Func = 1;
84
+
85
+                Firmware_byte_load = File.ReadAllBytes(this.ofd.FileName);
86
+                FirmTotalcnt = Firmware_byte_load.Length / Termianl_Controller_FirmwareUpdateLen;
87
+                // for (int i = 0; i < Termianl_Controller_FirmwareUpdateLen; i++)
88
+
89
+                FirmCurrcnt = 0;
90
+                PreFirmSendingcnt = 0;
91
+                FirmSendingcnt = 0;
92
+                Update_ready = true;
93
+                tempdata[0] = 0xbe;
94
+                tempdata[1] = Termianl_Reset;
95
+                tempdata[2] = 0x02;
96
+                tempdata[3] = STH30_CreateCrc(tempdata, tempdata[2]);
97
+                tempdata[4] = 0xeb;
98
+                /*try
99
+                {
100
+                    serialPort1.Write(tempdata, 0, 5);
101
+                }
102
+                catch { MessageBox.Show("Port Open Failed!!!"); }*/
103
+                this.serial.Serial_DataSend(tempdata, 5);
104
+                try
105
+                {
106
+                   // Controller_Debug.Controller_TX_TextLoad(tempdata);
107
+                }
108
+                catch { }
109
+                //download_Bar.ShowDialog();
110
+                /*Update_label.Visible = true;
111
+                progressBar1.Visible = true;*/
112
+                return fileFullName;
113
+            }
114
+            //취소버튼 클릭시 또는 ESC키로 파일창을 종료 했을경우
115
+            else if (dr == DialogResult.Cancel)
116
+            {
117
+                return "";
118
+            }
119
+
120
+            return "";
121
+        }
122
+
123
+        public void FirmwareUpdateCheck(byte check)
124
+        {
125
+
126
+            byte[] temp_data = new byte[Termianl_Controller_FirmwareUpdateLen + 5];
127
+            byte cnt = 3;
128
+            int lastlen = 0;
129
+            // FirmTotalcnt = Firmware_byte_load.Length / Termianl_Controller_FirmwareUpdateLen;
130
+            lastlen = Firmware_byte_load.Length % Termianl_Controller_FirmwareUpdateLen;
131
+            temp_data[0] = blucell_stx;
132
+            temp_data[1] = 0xDD;
133
+            if (check == Termianl_BootStart)
134
+            {
135
+                System.Threading.Thread.Sleep(2000);
136
+            }
137
+            if (Termianl_Controller_Update_Ack == check || Termianl_BootStart == check)
138
+            {
139
+                PreFirmSendingcnt = FirmSendingcnt;
140
+                if (FirmCurrcnt != FirmTotalcnt)
141
+                {
142
+                    temp_data[2] = Termianl_Controller_FirmwareUpdateLen + 2;
143
+                    for (int i = 0; i < Termianl_Controller_FirmwareUpdateLen; i++)
144
+                    {
145
+                        temp_data[cnt++] = Firmware_byte_load[FirmSendingcnt++];
146
+                    }
147
+                    FirmCurrcnt++;
148
+                    temp_data[Termianl_Controller_FirmwareUpdateLen + 3] = STH30_CreateCrc(temp_data, temp_data[2]);
149
+                    temp_data[Termianl_Controller_FirmwareUpdateLen + 4] = blucell_etx;
150
+                }
151
+                else
152
+                {
153
+                    Update_ready = false;
154
+                    temp_data[1] = 0xEE;
155
+                    temp_data[2] = Convert.ToByte(lastlen + 2);
156
+                    for (int i = 0; i < lastlen; i++)
157
+                    {
158
+                        temp_data[cnt++] = Firmware_byte_load[FirmSendingcnt++];
159
+                    }
160
+                    temp_data[temp_data[2] + 1] = STH30_CreateCrc(temp_data, temp_data[2]);
161
+                    temp_data[temp_data[2] + 2] = blucell_etx;
162
+                    /*ownload_Bar.ShowDialog();
163
+                    download_Bar.Progressbar("0 %");
164
+                    download_Bar.Close();*/
165
+                    /*Update_label.Text = "0%";
166
+                    Update_label.Visible = false;
167
+                    progressBar1.Visible = false;
168
+                    progressBar1.Value = 0;*/
169
+                    //  MessageBox.Show("Update Complete !!");
170
+                }
171
+
172
+            }
173
+            else
174
+            {
175
+                FirmSendingcnt = PreFirmSendingcnt;
176
+                if (FirmCurrcnt != FirmTotalcnt)
177
+                {
178
+                    temp_data[2] = Termianl_Controller_FirmwareUpdateLen + 2;
179
+                    for (int i = 0; i < Termianl_Controller_FirmwareUpdateLen; i++)
180
+                    {
181
+                        temp_data[cnt++] = Firmware_byte_load[FirmSendingcnt++];
182
+
183
+                    }
184
+                    FirmCurrcnt++;
185
+                    temp_data[Termianl_Controller_FirmwareUpdateLen + 3] = STH30_CreateCrc(temp_data, temp_data[2]);
186
+                    temp_data[Termianl_Controller_FirmwareUpdateLen + 4] = blucell_etx;
187
+                }
188
+                else
189
+                {
190
+                    Update_ready = false;
191
+                    temp_data[1] = 0xEE;
192
+                    temp_data[2] = Convert.ToByte(lastlen + 2);
193
+                    for (int i = 0; i < lastlen; i++)
194
+                    {
195
+                        temp_data[cnt++] = Firmware_byte_load[FirmSendingcnt++];
196
+                    }
197
+                    temp_data[temp_data[2] + 1] = STH30_CreateCrc(temp_data, temp_data[2]);
198
+                    temp_data[temp_data[2] + 2] = blucell_etx;
199
+                    /* Update_label.Visible = false;
200
+                     progressBar1.Visible = false;*/
201
+                    download_Bar.Close();
202
+                }
203
+            }
204
+            int tempupdateret = Firmware_byte_load.Length / 100;
205
+            tempupdateret = FirmSendingcnt / tempupdateret;
206
+            //CheckForIllegalCrossThreadCalls = false;
207
+            //Update_label.Text = Convert.ToString(tempupdateret) + "%";
208
+            if (pretempupdateret != tempupdateret)
209
+            {
210
+
211
+                for (int i = 0; i < tempupdateret - pretempupdateret; i++)
212
+                {
213
+                   // progressBar1.PerformStep();
214
+                }
215
+
216
+                pretempupdateret = tempupdateret;
217
+            }
218
+
219
+            //serialPort1.Write(temp_data, 0, temp_data[2] + 3);
220
+            this.serial.Serial_DataSend(temp_data, temp_data[2] + 3);
221
+            try
222
+            {
223
+               // Controller_Debug.Controller_TX_TextLoad(temp_data);
224
+            }
225
+            catch { }
226
+        }
227
+
228
+        public void Termianl__Operate(byte[] Text)
229
+        {
230
+            byte Type = 0;
231
+            Type = Text[Bluecell_BootProtocol.bluecell_type];
232
+            switch (Type)
233
+            {
234
+               
235
+                case Termianl_BootStart:
236
+                case Termianl_Controller_Update_Ack:
237
+                case Termianl_Controller_Update_Nak:
238
+                    if (Update_ready == true)
239
+                        FirmwareUpdateCheck(Type);
240
+                    break;
241
+               
242
+                default:
243
+                    // MessageBox.Show("다시 시도 해주세요.");
244
+                    break;
245
+            }
246
+        }
247
+
248
+        byte STH30_CreateCrc(byte[] data, byte nbrOfBytes)
249
+        {
250
+            byte bit;        // bit mask
251
+            int crc = 0xFF; // calculated checksum
252
+            byte byteCtr;    // byte counter
253
+            byte index = 1;
254
+            // calculates 8-Bit checksum with given polynomial
255
+            for (byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++)
256
+            {
257
+                crc ^= (data[index++]);
258
+                for (bit = 8; bit > 0; --bit)
259
+                {
260
+                    if (Convert.ToBoolean(crc & 0x80)) { crc = (crc << 1) ^ POLYNOMIAL; }
261
+                    else crc = (crc << 1);
262
+                }
263
+            }
264
+            return (byte)crc;
265
+        }
266
+    }
267
+}

+ 4 - 0
Basic_Terminal/Wnd/Download_bar.cs

@@ -16,5 +16,9 @@ namespace Basic_Terminal
16 16
         {
17 17
             InitializeComponent();
18 18
         }
19
+        public void Progressbar(String val)
20
+        {
21
+            Update_label.Text = val;
22
+        }
19 23
     }
20 24
 }

+ 7 - 3
Basic_Terminal/Wnd/Main_Form.cs

@@ -18,25 +18,29 @@ namespace Basic_Terminal
18 18
     {
19 19
         Serial serial = new Serial(); // Uart Open
20 20
         OpenFileDialog ofd = new OpenFileDialog();
21
-
21
+        Update_Serial file = new Update_Serial();
22 22
         public Main_Form()
23 23
         {
24 24
             InitializeComponent();
25 25
             serial.Serial_Initialize(ref comboBox_Port);
26 26
         }
27
-
27
+       
28 28
         private void Firmware_Update_Click(object sender, EventArgs e)
29 29
         {
30
-            FileDownload file = new FileDownload();
30
+            
31
+            //FileDownload file = new FileDownload();
31 32
             if (Ascii_checkBox.Checked == true)
32 33
                 Ascii_checkBox.Checked = false;
33 34
             file.ShowFileOpenDialog(serial, ofd);
34 35
         }
35 36
         private void Crc16_Check_Click(object sender, EventArgs e)
36 37
         {
38
+#if false
37 39
             Crc16 crc16 = new Crc16();
38 40
             byte[] tempdata = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0A };
39 41
             label1.Text = crc16.CRC16_Generate(tempdata, 11).ToString();
42
+#endif
43
+            this.serial.Test_Serial();
40 44
         }
41 45
 
42 46
         private void Button_PortOpen_Click(object sender, EventArgs e)