Subversion Repositories HomeAutomation

Rev

Rev 270 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 270 Rev 800
1
// canBootloader.cpp : Defines the entry point for the console application.
1
// canBootloader.cpp : Defines the entry point for the console application.
2
//
2
//
3
 
3
 
4
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp
4
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp
5
 
5
 
6
 
6
 
7
#define STRICT
7
#define STRICT
8
#include <tchar.h>
8
#include <tchar.h>
9
#include <windows.h>
9
#include <windows.h>
10
#include <stdio.h>
10
#include <stdio.h>
11
//#include "Serial.h"
11
//#include "Serial.h"
12
#include "HexFile.h"
12
#include "HexFile.h"
13
#include "CanProtoMessage.h"
13
#include "CanProtoMessage.h"
14
//#include "SerialReader.h"
14
//#include "SerialReader.h"
15
#include "DownloadState.h"
15
#include "DownloadState.h"
16
#include "SerialConnection.h"
16
#include "SerialConnection.h"
17
 
17
 
18
bool parseInput(char instr[]);
18
bool parseInput(char instr[]);
19
bool serialSetConnected(bool connected);
19
bool serialSetConnected(bool connected);
20
bool isConnected(void);
20
bool isConnected(void);
21
 
21
 
22
#define INPUT_BUFFER_SIZE 128
22
#define INPUT_BUFFER_SIZE 128
23
#define FILEPATH1 "F:/MCP/projects/CAN/canNode/_output/canNode.hex"
23
#define FILEPATH1 "F:/MCP/projects/CAN/canNode/_output/canNode.hex"
24
#define FILEPATH2 "F:/MCP/projects/CAN/canDebug/canDebug.hex"
24
#define FILEPATH2 "F:/MCP/projects/CAN/canDebug/canDebug.hex"
25
 
25
 
26
//static CSerial serial;
26
//static CSerial serial;
27
static SerialConnection scp(TEXT("COM2"),SerialConnection::EBaud115200,SerialConnection::EData8,SerialConnection::EParNone,SerialConnection::EStop1);
27
static SerialConnection scp(TEXT("COM2"),SerialConnection::EBaud115200,SerialConnection::EData8,SerialConnection::EParNone,SerialConnection::EStop1);
28
static HexFile hf;
28
static HexFile hf;
29
 
29
 
30
int _tmain(int argc, _TCHAR* argv[])
30
int _tmain(int argc, _TCHAR* argv[])
31
{
31
{
32
 
32
 
33
    char inbuff[INPUT_BUFFER_SIZE];
33
    char inbuff[INPUT_BUFFER_SIZE];
34
 
34
 
35
    printf("Main: ");
35
    printf("Main: ");
36
 
36
 
37
    // Forever input read loop.
37
    // Forever input read loop.
38
    do
38
    do
39
    {
39
    {
40
        // Read user input.
40
        // Read user input.
41
        gets_s(inbuff,INPUT_BUFFER_SIZE);
41
        gets_s(inbuff,INPUT_BUFFER_SIZE);
42
    } while(parseInput(inbuff));
42
    } while(parseInput(inbuff));
43
 
43
 
44
    // Close the port again
44
    // Close the port again
45
    if(isConnected()) serialSetConnected(false);
45
    if(isConnected()) serialSetConnected(false);
46
 
46
 
47
    return 0;
47
    return 0;
48
}
48
}
49
 
49
 
50
 
50
 
51
bool parseInput(char instr[])
51
bool parseInput(char instr[])
52
{
52
{
53
    LONG    lLastError = ERROR_SUCCESS;
53
    LONG    lLastError = ERROR_SUCCESS;
54
 
54
 
55
    // Make instr lower.
55
    // Make instr lower.
56
    _strlwr_s(instr,INPUT_BUFFER_SIZE);
56
    _strlwr_s(instr,INPUT_BUFFER_SIZE);
57
 
57
 
58
    if (strcmp(instr,"exit")==0) return false; // If exit, exit.
58
    if (strcmp(instr,"exit")==0) return false; // If exit, exit.
59
    else if (strcmp(instr,"go")==0)
59
    else if (strcmp(instr,"go")==0)
60
    {
60
    {
61
        // Download program.
61
        // Download program.
62
        if (hf.isValid())
62
        if (hf.isValid())
63
            printf("GO!\n");
63
            printf("GO!\n");
64
        else
64
        else
65
            printf("Un-GO!\n");
65
            printf("Un-GO!\n");
66
 
66
 
67
        // First connect
67
        // First connect
68
        scp.open();
68
        scp.open();
69
        //serialSetConnected(true);
69
        //serialSetConnected(true);
70
        // download program
70
        // download program
71
        downloadStart(&scp,&hf);
71
        downloadStart(&scp,&hf);
72
 
72
 
73
    }
73
    }
74
    else if (strcmp(instr,"load debug")==0)
74
    else if (strcmp(instr,"load debug")==0)
75
    {
75
    {
76
        // Load debug app.
76
        // Load debug app.
77
    }
77
    }
78
    else if (strcmp(instr,"load")==0)
78
    else if (strcmp(instr,"load")==0)
79
    {
79
    {
80
        // load regular app.
80
        // load regular app.
81
        if (hf.loadHex(FILEPATH1,true)) { printf("File %s loaded sucessfully!\n",hf.filepath); printf("File size: %lu, upper addr: %lu, lower addr: %lu\n",hf.getLength(),hf.getAddrUpper(),hf.getAddrLower()); }
81
        if (hf.loadHex(FILEPATH1,true)) { printf("File %s loaded sucessfully!\n",hf.filepath); printf("File size: %lu, upper addr: %lu, lower addr: %lu\n",hf.getLength(),hf.getAddrUpper(),hf.getAddrLower()); }
82
        else { printf(hf.lastError); }
82
        else { printf(hf.lastError); }
83
 
83
 
84
    }
84
    }
85
    /*
85
    /*
86
    else if (strcmp(instr,"put")==0)
86
    else if (strcmp(instr,"put")==0)
87
    {
87
    {
88
        // Put dummy packet
88
        // Put dummy packet
89
 
89
 
90
        unsigned char canmsg[17];
90
        unsigned char canmsg[17];
91
        canmsg[0]=253;
91
        canmsg[0]=253;
92
        canmsg[16]=250;
92
        canmsg[16]=250;
93
        if (isConnected())
93
        if (isConnected())
94
            {
94
            {
95
            lLastError = serial.Write(canmsg,17);
95
            lLastError = serial.Write(canmsg,17);
96
            if (lLastError != ERROR_SUCCESS)
96
            if (lLastError != ERROR_SUCCESS)
97
            {
97
            {
98
                printf("Unable to send data\n");
98
                printf("Unable to send data\n");
99
            }
99
            }
100
        } else { printf("Err! No connected to serial port.\n"); }
100
        } else { printf("Err! No connected to serial port.\n"); }
101
    }
101
    }
102
*/
102
*/
103
 
103
 
104
    else if (strcmp(instr,"conn")==0)
104
    else if (strcmp(instr,"conn")==0)
105
    {
105
    {
106
        // Connect to serial port.
106
        // Connect to serial port.
107
 
107
 
108
        if (isConnected()) { printf("Err! Alredy connected to serial port.\n"); }
108
        if (isConnected()) { printf("Err! Alredy connected to serial port.\n"); }
109
        else { serialSetConnected(true); }
109
        else { serialSetConnected(true); }
110
    }
110
    }
111
    else if (strcmp(instr,"diss")==0)
111
    else if (strcmp(instr,"diss")==0)
112
    {      
112
    {      
113
        // Dissconnect from serial port.
113
        // Dissconnect from serial port.
114
 
114
 
115
        if (!isConnected()) { printf("Err! Not connected to serial port.\n"); }
115
        if (!isConnected()) { printf("Err! Not connected to serial port.\n"); }
116
        else { serialSetConnected(false); }
116
        else { serialSetConnected(false); }
117
    }
117
    }
118
    else if (strcmp(instr,"test2")==0)
118
    else if (strcmp(instr,"test2")==0)
119
    {
119
    {
120
        //serialReaderStart(&serial);
120
        //serialReaderStart(&serial);
121
    }
121
    }
122
    else if (strcmp(instr,"test2 stop")==0)
122
    else if (strcmp(instr,"test2 stop")==0)
123
    {
123
    {
124
        //serialReaderStop();
124
        //serialReaderStop();
125
    }
125
    }
126
    else if (strcmp(instr,"test3")==0)
126
    else if (strcmp(instr,"test3")==0)
127
    {
127
    {
128
        DCB dcb;
128
        DCB dcb;
129
        HANDLE hCom;
129
        HANDLE hCom;
130
        bool fSuccess;
130
        bool fSuccess;
131
 
131
 
132
 
132
 
133
       hCom = ::CreateFile(_T("COM2"),
133
       hCom = ::CreateFile(_T("COM2"),
134
                        GENERIC_READ | GENERIC_WRITE,
134
                        GENERIC_READ | GENERIC_WRITE,
135
                        0,    // must be opened with exclusive-access
135
                        0,    // must be opened with exclusive-access
136
                        0, // no security attributes
136
                        0, // no security attributes
137
                        OPEN_EXISTING, // must use OPEN_EXISTING
137
                        OPEN_EXISTING, // must use OPEN_EXISTING
138
                        FILE_FLAG_OVERLAPPED,    // not overlapped I/O
138
                        FILE_FLAG_OVERLAPPED,    // not overlapped I/O
139
                        0  // hTemplate must be NULL for comm devices
139
                        0  // hTemplate must be NULL for comm devices
140
                        );
140
                        );
141
 
141
 
142
       if (hCom == INVALID_HANDLE_VALUE)
142
       if (hCom == INVALID_HANDLE_VALUE)
143
       {
143
       {
144
           // Handle the error.
144
           // Handle the error.
145
           printf ("Err: %d.\n",GetLastError());
145
           printf ("Err: %d.\n",GetLastError());
146
       }
146
       }
147
 
147
 
148
 
148
 
149
        // Build on the current configuration, and skip setting the size
149
        // Build on the current configuration, and skip setting the size
150
       // of the input and output buffers with SetupComm.
150
       // of the input and output buffers with SetupComm.
151
 
151
 
152
       fSuccess = GetCommState(hCom, &dcb);
152
       fSuccess = GetCommState(hCom, &dcb);
153
 
153
 
154
       if (!fSuccess)
154
       if (!fSuccess)
155
       {
155
       {
156
          // Handle the error.
156
          // Handle the error.
157
          printf ("GetCommState failed with error %d.\n", GetLastError());
157
          printf ("GetCommState failed with error %d.\n", GetLastError());
158
       }
158
       }
159
 
159
 
160
        // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
160
        // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.
161
 
161
 
162
       dcb.BaudRate = CBR_57600;     // set the baud rate
162
       dcb.BaudRate = CBR_57600;     // set the baud rate
163
       dcb.ByteSize = 8;             // data size, xmit, and rcv
163
       dcb.ByteSize = 8;             // data size, xmit, and rcv
164
       dcb.Parity = NOPARITY;        // no parity bit
164
       dcb.Parity = NOPARITY;        // no parity bit
165
       dcb.StopBits = ONESTOPBIT;    // one stop bit
165
       dcb.StopBits = ONESTOPBIT;    // one stop bit
166
 
166
 
167
       fSuccess = SetCommState(hCom, &dcb);
167
       fSuccess = SetCommState(hCom, &dcb);
168
 
168
 
169
       if (!fSuccess)
169
       if (!fSuccess)
170
       {
170
       {
171
          // Handle the error.
171
          // Handle the error.
172
          printf ("SetCommState failed with error %d.\n", GetLastError());
172
          printf ("SetCommState failed with error %d.\n", GetLastError());
173
       }
173
       }
174
 
174
 
175
       printf ("Serial port successfully reconfigured.\n");
175
       printf ("Serial port successfully reconfigured.\n");
176
       
176
       
177
 
177
 
178
 
178
 
179
    }
179
    }
180
    else if (strcmp(instr,"test4")==0)
180
    else if (strcmp(instr,"test4")==0)
181
    {
181
    {
182
 
182
 
183
        scp.open();
183
        scp.open();
184
   
184
   
185
        char data[] = "hej";
185
        char data[] = "hej";
186
        unsigned char data2[17];
186
        unsigned char data2[17];
187
 
187
 
188
        CanProtoMessage cpm(0x01,0x01,0x01,0x01,1,(unsigned char *)data);
188
        CanProtoMessage cpm(0x01,0x01,0x01,0x01,1,(unsigned char *)data);
189
       
189
       
190
        cpm.getBytes(data2);
190
        cpm.getBytes(data2);
191
 
191
 
192
        scp.write(data2,17);
192
        scp.write(data2,17);
193
 
193
 
194
        //serialReaderStartSerCon(&scp);
194
        //serialReaderStartSerCon(&scp);
195
 
195
 
196
        //scp.close();
196
        //scp.close();
197
    }
197
    }
198
    else if(strcmp(instr,"tick")==0)
198
    else if(strcmp(instr,"tick")==0)
199
    {
199
    {
200
      printf ("actual:%ld\n", GetTickCount());
200
      printf ("actual:%ld\n", GetTickCount());
201
 
201
 
202
    }
202
    }
203
    else if (strcmp(instr,"test")==0)
203
    else if (strcmp(instr,"test")==0)
204
    {
204
    {
205
        // Debug for testing canProtoMessage
205
        // Debug for testing canProtoMessage
206
 
206
 
207
        unsigned char dat[8];
207
        unsigned char dat[8];
208
       
208
       
209
        for (int i=0;i<8;i++) dat[i]=0x10+i*2;
209
        for (int i=0;i<8;i++) dat[i]=0x10+i*2;
210
       
210
       
211
        unsigned char dat2[17];
211
        unsigned char dat2[17];
212
 
212
 
213
        CanProtoMessage cpm1(0x07,0x91,0x10,0x87,8,&dat[0]);
213
        CanProtoMessage cpm1(0x07,0x91,0x10,0x87,8,&dat[0]);
214
       
214
       
215
        cpm1.getBytes(&dat2[0]);
215
        cpm1.getBytes(&dat2[0]);
216
   
216
   
217
 
217
 
218
        CanProtoMessage cpm2(&dat2[0],1);
218
        CanProtoMessage cpm2(&dat2[0],1);
219
   
219
   
220
        if (cpm1.equals(&cpm2)) printf("OK!\n"); else printf("Not OK!\n");
220
        if (cpm1.equals(&cpm2)) printf("OK!\n"); else printf("Not OK!\n");
221
 
221
 
222
    }
222
    }
223
    else
223
    else
224
    {
224
    {
225
        printf("Command not found.\n");
225
        printf("Command not found.\n");
226
    }
226
    }
227
 
227
 
228
    printf("Main: ");
228
    printf("Main: ");
229
 
229
 
230
    return true;
230
    return true;
231
}
231
}
232
 
232
 
233
 
233
 
234
bool serialSetConnected(bool connected)
234
bool serialSetConnected(bool connected)
235
{
235
{
236
    /*
236
    /*
237
    LONG    lLastError = ERROR_SUCCESS;
237
    LONG    lLastError = ERROR_SUCCESS;
238
 
238
 
239
    if (connected)
239
    if (connected)
240
    {
240
    {
241
        // Attempt to open the serial port (COM1)
241
        // Attempt to open the serial port (COM1)
242
        lLastError = serial.Open(_T("COM2"),0,0,true);
242
        lLastError = serial.Open(_T("COM2"),0,0,true);
243
        if (lLastError != ERROR_SUCCESS)
243
        if (lLastError != ERROR_SUCCESS)
244
        {
244
        {
245
            printf("Unable to open COM-port\n");
245
            printf("Unable to open COM-port\n");
246
            return false;
246
            return false;
247
        }
247
        }
248
        // Setup the serial port (9600,N81) using hardware handshaking
248
        // Setup the serial port (9600,N81) using hardware handshaking
249
        lLastError = serial.Setup(CSerial::EBaud115200,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
249
        lLastError = serial.Setup(CSerial::EBaud115200,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
250
        if (lLastError != ERROR_SUCCESS)
250
        if (lLastError != ERROR_SUCCESS)
251
        {
251
        {
252
                printf("Unable to set COM-port setting\n");
252
                printf("Unable to set COM-port setting\n");
253
            return false;
253
            return false;
254
        }
254
        }
255
        // Setup handshaking
255
        // Setup handshaking
256
        lLastError = serial.SetupHandshaking(CSerial::EHandshakeOff);
256
        lLastError = serial.SetupHandshaking(CSerial::EHandshakeOff);
257
        if (lLastError != ERROR_SUCCESS)
257
        if (lLastError != ERROR_SUCCESS)
258
        {
258
        {
259
            printf("Unable to set COM-port handshaking\n");
259
            printf("Unable to set COM-port handshaking\n");
260
            return false;
260
            return false;
261
        }
261
        }
262
 
262
 
263
        return true;
263
        return true;
264
    }
264
    }
265
    else
265
    else
266
    {
266
    {
267
        serial.Close();
267
        serial.Close();
268
        return true;
268
        return true;
269
    }
269
    }
270
*/
270
*/
271
    return false;
271
    return false;
272
 
272
 
273
}
273
}
274
 
274
 
275
bool isConnected(void)
275
bool isConnected(void)
276
{
276
{
277
    //return serial.IsOpen();
277
    //return serial.IsOpen();
278
    return true;
278
    return true;
279
}
279
}