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