void CMainDlg::OnSave() { CString sFile; int fhLabel = 0; LPSTRUCT_LABELITEM lpLabelItem = NULL; unsigned char cTemp; UpdateData(TRUE); lpLabelItem = (LPSTRUCT_LABELITEM)listLabel.GetAt(pos); if(sLabel != lpLabelItem->szLabel) { free(lpLabelItem->szLabel); lpLabelItem->cLength_label = sLabel.GetLength(); lpLabelItem->szLabel = (LPTSTR)malloc(lpLabelItem->cLength_label + 1); memset(lpLabelItem->szLabel,0,lpLabelItem->cLength_label); strncpy(lpLabelItem->szLabel,sLabel,lpLabelItem->cLength_label); lpLabelItem->szLabel[lpLabelItem->cLength_label] = '\0'; GenerateChecksum(lpLabelItem); } if(!BrowseForFile(&sFile)) return; if((fhLabel = _open(sFile,_O_BINARY | _O_CREAT | _O_APPEND | _O_WRONLY,_S_IWRITE)) == -1) return; _chsize(fhLabel,0); cTemp = sVersion.GetLength(); _write(fhLabel,&cTemp,1); _write(fhLabel,sVersion,sVersion.GetLength()); cTemp += GenerateChecksum(sVersion,sVersion.GetLength()); _write(fhLabel,&cTemp,1); pos = listLabel.GetHeadPosition(); while(pos != NULL) { lpLabelItem = (LPSTRUCT_LABELITEM)listLabel.GetNext(pos); _write(fhLabel,&lpLabelItem->cLength_id,1); _write(fhLabel,lpLabelItem->szID,lpLabelItem->cLength_id); _write(fhLabel,&lpLabelItem->cLength_label,1); _write(fhLabel,lpLabelItem->szLabel,lpLabelItem->cLength_label); GenerateChecksum(lpLabelItem); _write(fhLabel,&lpLabelItem->cChecksum,1); } _close(fhLabel); }
void CMainDlg::OnNext() { CString sText; LPSTRUCT_LABELITEM lpLabelItem = NULL; int iLoop; UpdateData(TRUE); lpLabelItem = (LPSTRUCT_LABELITEM)listLabel.GetAt(pos); if(sLabel != lpLabelItem->szLabel) { free(lpLabelItem->szLabel); lpLabelItem->cLength_label = sLabel.GetLength(); lpLabelItem->szLabel = (LPTSTR)malloc(lpLabelItem->cLength_label + 1); memset(lpLabelItem->szLabel,0,lpLabelItem->cLength_label); strncpy(lpLabelItem->szLabel,sLabel,lpLabelItem->cLength_label); lpLabelItem->szLabel[lpLabelItem->cLength_label] = '\0'; GenerateChecksum(lpLabelItem); } sID.Empty(); listLabel.GetNext(pos); if(pos == NULL) { pos = listLabel.GetTailPosition(); return; } lpLabelItem = (LPSTRUCT_LABELITEM)listLabel.GetAt(pos); for(iLoop = 0;iLoop < lpLabelItem->cLength_id;iLoop++) { sText.Format("%02X ",(unsigned char)lpLabelItem->szID[iLoop]); sID += sText; } sLabel = lpLabelItem->szLabel; // sType = lpLabelItem->szType; sChecksum = lpLabelItem->cChecksum; UpdateData(FALSE); usIndex++; sText.Format("ecuExplorer Label Editor - %i of %i",usIndex,listLabel.GetCount()); SetWindowText(sText); }
/***************************************************************** ** Searches for the last NMEA 0183 message within the buffer ** *****************************************************************/ void msgSearch() { int readPos = writePos; int count = 0; int i; // See if the last char received was a LF character if (fifoBuffer[readPos] == (char)10) { if (--readPos < 0) readPos = 255; //Work back until we find a $ while (readPos != writePos) { count++; if (fifoBuffer[readPos] == '$') { //Extract the message char message[count]; char * crc; for (i = 0; i < count; i++) { message[i] = fifoBuffer[readPos]; readPos = (readPos + 1) % 255; //Ensure the checksum is included if ((i > 3) && (message[i-2] == '*')) { i++; break; } } message[i] = 0; //Null char for strlen // Verify the crc matches crc = GenerateChecksum(message); if ((crc[0] == message[strlen(message)-2]) && (crc[1] == message[strlen(message)-1])) { char source[3]; char messageID[4]; char content[73]; //Raise the message received flag. for (i = 0; i < strlen(message) - 2; i++) { if (i < 3) source[i] = message[i+1]; else if (i < 6) messageID[i-3] = message[i]; else if ((i > 6)) { if (message[i] == '*') { content[i-7] = 0; break; } content[i-7] = message[i]; } } //Clean up, and raise the message received flag source[2] = 0; messageID[3] = 0; //ROS_INFO("MSG SRC: %s, ID: %s, CONTENTS: %s", source, messageID, content); std::istringstream oss(std::string((char *)content)); std::string word; int index = 0; while(getline(oss, word, ',')) { if (index == 0) reed_status.data = (char)atoi(word.c_str()); else if (index == 1) battery_voltage1.data = (float)strtod(word.c_str(), NULL); else if (index == 2) battery_voltage2.data = (float)strtod(word.c_str(), NULL); else if (index == 3) battery_voltage3.data = (float)strtod(word.c_str(), NULL); else if (index == 4) battery_voltage4.data = (float)strtod(word.c_str(), NULL); else if (index == 5) ambient_temperature.data = (float)strtod(word.c_str(), NULL); else if (index == 6) motor1_temperature.data = (float)strtod(word.c_str(), NULL); else if (index == 7) motor2_temperature.data = (float)strtod(word.c_str(), NULL); else if (index == 8) fitpc_temperature.data = (float)strtod(word.c_str(), NULL); else if (index == 9) router_temperature.data = (float)strtod(word.c_str(), NULL); else if (index == 10) roboard_temperature.data = (float)strtod(word.c_str(), NULL); else if (index == 11) bt_shutdown.data = (char)atoi(word.c_str()); index++; } } else { // The checksum failed. Ignore the message. ROS_ERROR("CRC Failed"); } //Clear the buffer for (i = 0; i < 255; i++) fifoBuffer[i] = 0; break; } if (--readPos < 0) readPos = 255; } } }
unsigned char dialogEditor_dataitem::GenerateChecksum(LPSTRUCT_LIVEBITITEM lpDataItem) { unsigned char cReturn = 0; cReturn = lpDataItem->cByte; cReturn += lpDataItem->cBit; cReturn += lpDataItem->cLength_name; cReturn += GenerateChecksum(lpDataItem->szName,lpDataItem->cLength_name); cReturn += GenerateChecksum(lpDataItem->ulAddress_high); cReturn += GenerateChecksum(lpDataItem->ulAddress_low); cReturn += lpDataItem->cType; cReturn += GenerateChecksum(lpDataItem->usOperand_addition); cReturn += GenerateChecksum(lpDataItem->usOperand_subtract); cReturn += GenerateChecksum(lpDataItem->usOperand_multiplier); cReturn += GenerateChecksum(lpDataItem->usOperand_divisor); cReturn += GenerateChecksum(lpDataItem->usDecimals); cReturn += lpDataItem->cLength_unit; cReturn += GenerateChecksum(lpDataItem->szUnit,lpDataItem->cLength_unit); cReturn += GenerateChecksum(lpDataItem->usLength_description); cReturn += GenerateChecksum(lpDataItem->szDescription,lpDataItem->usLength_description); return cReturn; }
void dialogEditor_dataitem::OnSave() { int fhOutput = -1; CString sError; int iLoop = 0; int iSub = 0; unsigned char cTemp; LPSTRUCT_LIVEBITITEM lpDataItem = NULL; unsigned long* lpOffset = NULL; POSITION pos = NULL; SaveChanges(); BrowseForFile(&sError,"Select a filename for the Data Item Data File",OFN_OVERWRITEPROMPT); if((fhOutput = _open(sError,_O_BINARY | _O_CREAT | _O_WRONLY,_S_IWRITE)) == -1) return; if((_chsize(fhOutput,0)) == -1) return; // Live Data File Format // 1 bytes [Version Length] // x bytes [Version] // 1 bytes [Version Checksum] // [..] // 1 bytes [Byte] // 1 bytes [Bit] // 1 bytes [Name Length] // x bytes [Name] // 4 bytes [Address High] // 4 bytes [Address Low] // 1 bytes [Type] // 2 bytes [Operand Addition] // 2 bytes [Operand Subtract] // 2 bytes [Operand Multiplier] // 2 bytes [Operand Divisor] // 2 bytes [Decimals] // 1 bytes [Unit Length] // x bytes [Unit] // 2 bytes [Description Length] // x bytes [Description] // 1 bytes [Checksum] // [..] cTemp = (char)sVersion.GetLength(); _write(fhOutput,&cTemp,1); _write(fhOutput,(LPCTSTR)sVersion,cTemp); cTemp += GenerateChecksum(sVersion,sVersion.GetLength()); _write(fhOutput,&cTemp,1); pos = listDataItem.GetHeadPosition(); while(pos != NULL) { lpDataItem = (LPSTRUCT_LIVEBITITEM)listDataItem.GetNext(pos); _write(fhOutput,&lpDataItem->cByte,1); _write(fhOutput,&lpDataItem->cBit,1); _write(fhOutput,&lpDataItem->cLength_name,1); _write(fhOutput,lpDataItem->szName,lpDataItem->cLength_name); _write(fhOutput,&lpDataItem->ulAddress_high,4); _write(fhOutput,&lpDataItem->ulAddress_low,4); _write(fhOutput,&lpDataItem->cType,1); _write(fhOutput,&lpDataItem->usOperand_addition,2); _write(fhOutput,&lpDataItem->usOperand_subtract,2); _write(fhOutput,&lpDataItem->usOperand_multiplier,2); _write(fhOutput,&lpDataItem->usOperand_divisor,2); _write(fhOutput,&lpDataItem->usDecimals,2); _write(fhOutput,&lpDataItem->cLength_unit,1); _write(fhOutput,lpDataItem->szUnit,lpDataItem->cLength_unit); _write(fhOutput,&lpDataItem->usLength_description,2); _write(fhOutput,lpDataItem->szDescription,lpDataItem->usLength_description); cTemp = GenerateChecksum(lpDataItem); _write(fhOutput,&cTemp,1); } }