Big bug in stl? Why does std::iostream read more than specified - fstream

#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
int argSiz;
void* argBuf;
std::fstream fStream;
fStream.open("E:/Fts/Proj/Test/DFVisionConsole_Proj/DFVisionConsole/solution/DFDevDefault.dfv", std::fstream::in | std::fstream::out | std::fstream::app);
fStream.read((char*)&argSiz, 4);
while(1)
{
long pos = fStream.tellg();
fStream.read((char*)&argSiz, 4);
long pos2 = fStream.tellg();
argBuf = malloc(argSiz);
fStream.read((char*)argBuf, argSiz);
long pos3 = fStream.tellg();
long mai = fStream.tellg();
}
return 0;
}
When
fStream.read((char*)&argSiz, ETypebufSizeSize);//argSiz = 24103
long pos2 = fStream.tellg();//size2 = 130
then
long pos3 = fStream.tellg();//pos3 = 24247
It shouldn't be 24247, 130 + 24103 = 24233.
Why does fstream read more than specified ?
You can download the test file DFDevDefualt.dfv
https://sourceforge.net/projects/genericlist/files/?source=navbar
Please help.

Related

Access .csv file from a dll

I am trying to access a file inside my DLL, but fopen always returns NULL. I am not very familiar with DLL's and I do not know how to debug this problem. I tried to run my program with executable and it runs without an error. I am posting my .c and .h files below, any help appreciated.
ReadFile1.c:
#include "ReadFile1.h"
#define MAX_STR_LEN 257
#define MAX_Color_Maps 17
int __declspec(dllexport) __stdcall ReadF(char* ColorMap)
{
FILE* bookFile;
/* allocation of the buffer for every line in the File */
char* buf = malloc(MAX_STR_LEN);
char* tmp;
/* if the space could not be allocated, return an error */
if (buf == NULL)
{
printf("No memory\n");
return 1;
}
if ((bookFile = fopen("Colormaps2.csv", "r")) == NULL) //Reading a file
{
printf("File could not be opened.\n");
printf("%s",strerror(errno));
return 1;
}
struct Color_decider Colors[MAX_Color_Maps];
int i = 1;
fgets(buf, 256, bookFile);
tmp = strtok(buf, ";");
char* temp1;
temp1= strdup(tmp);
strncpy(Colors[0].Color_maps, temp1, strlen(temp1));
while (i<17)
{
tmp = strtok(NULL, ";");
temp1 = strdup(tmp);
strncpy(Colors[i].Color_maps, temp1, strlen(temp1));
i++;
}
for (int k = 1; k < 17; k++)
{
if (!strcmp(Colors[k].Color_maps, ColorMap)) // Find the input column.
{
for (int j = 0; j < 256; j++)
{
fgets(buf, 256, bookFile);
tmp = strtok(buf, ";");
for (int l = 1; l < k * 3; l++)
tmp = strtok(NULL, ";");
tmp = strtok(NULL, ";");
temp1 = strdup(tmp);
Colors->C[j][0] = atoi(temp1);
tmp = strtok(NULL, ";");
temp1 = strdup(tmp);
Colors->C[j][1] = atoi(temp1);
tmp = strtok(NULL, ";");
temp1 = strdup(tmp);
Colors->C[j][2] = atoi(temp1);
printf("%d\t", Colors->C[j][0]);
printf("%d\t", Colors->C[j][1]);
printf("%d\n", Colors->C[j][2]);
}
break;
}
}
free(buf);
fclose(bookFile);
return 0;
}
ReadFile1.h
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
struct Color_decider {
char Color_maps[10];
int C[256][3];
};
char *strdup(char *org)
{
int org_size;
static char *dup;
char *dup_offset;
/* Allocate memory for duplicate */
org_size = strlen(org);
dup = (char *)malloc(sizeof(char)*org_size+1);
if( dup == NULL)
return( (char *)NULL);
/* Copy string */
dup_offset = dup;
while(*org)
{
*dup_offset = *org;
dup_offset++;
org++;
}
*dup_offset = '\0';
return(dup);
}
int __declspec(dllexport) __stdcall ReadF(char* ColorMap);

GNU Radio circular buffer manipulation

I encountered the following error
gr::log :WARN: tpb_thread_body - asynchronous message buffer overflowing, dropping message
Out of serendipity, I ran into this GNU Radio presentation on
Youtube.
The presenter mentioned an OOT block he called "buffer" that is capable of eliminating the "buffer overflowing" error. Apparently, this block plays with different sample rates and the so-called "circular buffers". I haven't worked with circular buffers myself. Any ideas on circular buffers or any hints on how to build this buffer block are welcome.
EDIT
Below is the flowgraph that generates the error. As it was suggested in the comments, the culprits could be the message processing blocks (red-circled) namely generateCADU (for generating standard CCSDS frames) and processCADU (for extracting CADUs from a data stream).
The implementation file of the generateCADU block is given below
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gnuradio/io_signature.h>
#include "generateCADU_impl.h"
#include "fec/ReedSolomon/ReedSolomon.h"
#include "fec/Scrambler/Scrambler.h"
namespace gr {
namespace ccsds {
generateCADU::sptr
generateCADU::make(int frameLength,std::string sync, int scramble, int rs, int intDepth)
{
return gnuradio::get_initial_sptr
(new generateCADU_impl(frameLength, sync, scramble, rs, intDepth));
}
/*
* The private constructor
*/
generateCADU_impl::generateCADU_impl(int frameLength,std::string sync, int scramble, int rs, int intDepth)
: gr::sync_block("generateCADU",
gr::io_signature::make(1, 1, sizeof(unsigned char)),
gr::io_signature::make(0, 0, 0)),
d_frameLength(frameLength),d_scramble(scramble == 1),d_rs(rs >= 1), d_basis(rs >= 2), d_intDepth(intDepth)
{
set_output_multiple(d_frameLength);
//Registering output port
message_port_register_out(pmt::mp("out"));
d_sync = parse_string(sync);
}
/*
* Our virtual destructor.
*/
generateCADU_impl::~generateCADU_impl()
{
}
unsigned char
generateCADU_impl::parse_hex(char c)
{
if ('0' <= c && c <= '9') return c - '0';
if ('A' <= c && c <= 'F') return c - 'A' + 10;
if ('a' <= c && c <= 'f') return c - 'a' + 10;
std::abort();
}
std::vector<unsigned char>
generateCADU_impl::parse_string(const std::string & s)
{
if (s.size() % 2 != 0) std::abort();
std::vector<unsigned char> result(s.size() / 2);
for (std::size_t i = 0; i != s.size() / 2; ++i)
result[i] = 16 * parse_hex(s[2 * i]) + parse_hex(s[2 * i + 1]);
return result;
}
int
generateCADU_impl::work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const unsigned char *in = (const unsigned char *) input_items[0];
//Reed-Solomon and Scrambler objects
ReedSolomon RS(16,d_intDepth,d_basis);// False = conventional, True = dual-basis
Scrambler S;
//Buffers
unsigned char *frameBuffer1 = (unsigned char*)malloc(d_frameLength*sizeof(unsigned char));
std::vector<unsigned char> frameBuffer2;
//The work function engine
for(int i = 0; (i + d_frameLength) < noutput_items; i += d_frameLength)
{
//Copying data from input stream
memcpy(frameBuffer1,in + i + d_frameLength,d_frameLength);
//Copying frame into std::vector buffer
frameBuffer2.insert(frameBuffer2.begin(),frameBuffer1, frameBuffer1 + d_frameLength);
//Optional scrambling and Reed-Solomon
if (d_rs) RS.Encode_RS(frameBuffer2);
if (d_scramble) S.Scramble(frameBuffer2);
//Insert sync word
frameBuffer2.insert(frameBuffer2.begin(), d_sync.begin(), d_sync.end());
//Transmitting PDU
pmt::pmt_t pdu(pmt::cons(pmt::PMT_NIL,pmt::make_blob(frameBuffer2.data(),frameBuffer2.size())));
message_port_pub(pmt::mp("out"), pdu);
//Clear buffer
frameBuffer2.clear();
}
// Tell runtime system how many output items we produced.
return noutput_items;
}
} /* namespace ccsds */
} /* namespace gr */
And here is the processCADU block. This block uses tags generated by the synchronizeCADU (which is simply a wrapper for the correlate_access_tag block) to extract CADUs
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gnuradio/io_signature.h>
#include "processCADU_impl.h"
#include "fec/ReedSolomon/ReedSolomon.h"
#include "fec/Scrambler/Scrambler.h"
namespace gr {
namespace ccsds {
processCADU::sptr
processCADU::make(int frameLength, int scramble, int rs, int intDepth, std::string tagName)
{
return gnuradio::get_initial_sptr
(new processCADU_impl(frameLength, scramble, rs, intDepth, tagName));
}
/*
* The private constructor
*/
processCADU_impl::processCADU_impl(int frameLength, int scramble, int rs, int intDepth, std::string tagName)
: gr::sync_block("processCADU",
gr::io_signature::make(1, 1, sizeof(unsigned char)),
gr::io_signature::make(0, 0, 0)),
d_frameLength(frameLength),d_scramble(scramble == 1),d_rs(rs >= 1), d_basis(rs >= 2), d_intDepth(intDepth)
{
//Multiple input
set_output_multiple(d_frameLength * 8);
//Registering output port
message_port_register_out(pmt::mp("out"));
if (d_rs) d_frameLength += 32 * d_intDepth;
//SEtting tag name
key = pmt::mp(tagName);
}
/*
* Our virtual destructor.
*/
processCADU_impl::~processCADU_impl()
{
delete d_pack;
}
int
processCADU_impl::work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const unsigned char *in = (const unsigned char *) input_items[0];
unsigned char *out = (unsigned char *) output_items[0];
void *msg_data = NULL;
unsigned char frame_data[d_frameLength];
unsigned char frame_len = 0;
std::vector<unsigned char> frameBuffer;
//Reed-Solomon and Scrambler objects
ReedSolomon RS(16,d_intDepth,d_basis);// False = conventional, True = dual-basis
std::vector<int> errors;//errors.push_back(0);
Scrambler S;
d_tags.clear();
d_pack = new blocks::kernel::pack_k_bits(8);
this->get_tags_in_window(d_tags, 0, 0, noutput_items,key);
for(d_tags_itr = d_tags.begin(); d_tags_itr != d_tags.end(); d_tags_itr++) {
// Check that we have enough data for a full frame
if ((d_tags_itr->offset - this->nitems_read(0)) > (noutput_items - (d_frameLength) * 8))
{
return (d_tags_itr->offset - this->nitems_read(0) - 1);
}
//Pack bits into bytes
d_pack->pack(frame_data, &in[d_tags_itr->offset - this->nitems_read(0)], d_frameLength);
//Copying frame into std::vector buffer
frameBuffer.insert(frameBuffer.begin(),frame_data, frame_data + d_frameLength);
//Optional scrambling and Reed-Solomon
if (d_scramble) S.Scramble(frameBuffer);
//if (d_rs) RS.Decode_RS(frameBuffer,errors);
//If there is Reed-Solomon decoding
if(d_rs)
{
RS.Decode_RS(frameBuffer,errors);
if (RS.Success(errors)) // Success
{
//std::cout << "Success" << std::endl;
pmt::pmt_t pdu(pmt::cons(pmt::PMT_NIL,pmt::make_blob(frameBuffer.data(),frameBuffer.size())));
message_port_pub(pmt::mp("out"), pdu);
/*for(int i=0; i < errors.size(); i++)
{
//std::cout << "Number of Errors : " << errors.at(i) << std::endl << std::endl;
}*/
}
else // Failure
{
std::cout << "RS failure" << std::endl;
}
}
else{
pmt::pmt_t pdu(pmt::cons(pmt::PMT_NIL,pmt::make_blob(frameBuffer.data(),frameBuffer.size())));
message_port_pub(pmt::mp("out"), pdu);
}
//Clear buffers
frameBuffer.clear();
errors.clear();
}
// Tell runtime system how many output items we produced.
return noutput_items;
}
} /* namespace ccsds */
} /* namespace gr */
Regards,
M
Thanks to #MarcusMüller suggestion, using the tagged_stream paradigma as opposed to PDUs solved the problem. I was able to transmit 47 terabytes of data without any problems. Below is the code for the newly implemented block.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gnuradio/io_signature.h>
#include "genCADU_impl.h"
namespace gr {
namespace ccsds {
genCADU::sptr
genCADU::make(int frameLength,std::string sync, int scramble, int rs, int intDepth, std::string len_tag_key)
{
return gnuradio::get_initial_sptr
(new genCADU_impl(frameLength, sync, scramble, rs, intDepth, len_tag_key));
}
/*
* The private constructor
*/
genCADU_impl::genCADU_impl(int frameLength,std::string sync, int scramble, int rs, int intDepth, std::string len_tag_key)
: gr::tagged_stream_block("genCADU",
gr::io_signature::make(1, 1, sizeof(unsigned char)),
gr::io_signature::make(1, 1, sizeof(unsigned char)),len_tag_key),
d_frameLength(frameLength),d_scramble(scramble == 1),d_rs(rs >= 1), d_basis(rs >= 2), d_intDepth(intDepth)
{
//Synchronization pattern
d_sync = parse_string(sync);
//Reed-Solomon and Scrambler objects
RS = new ReedSolomon(16,d_intDepth,d_basis);// False = conventional, True = dual-basis
S = new Scrambler();
}
/*
* Our virtual destructor.
*/
genCADU_impl::~genCADU_impl()
{
delete RS;
delete S;
}
int
genCADU_impl::calculate_output_stream_length(const gr_vector_int &ninput_items)
{
int noutput_items = (d_rs) ? d_frameLength + 32*d_intDepth + d_sync.size() : d_frameLength + d_sync.size();
return noutput_items ;
}
unsigned char
genCADU_impl::parse_hex(char c)
{
if ('0' <= c && c <= '9') return c - '0';
if ('A' <= c && c <= 'F') return c - 'A' + 10;
if ('a' <= c && c <= 'f') return c - 'a' + 10;
std::abort();
}
std::vector<unsigned char>
genCADU_impl::parse_string(const std::string & s)
{
if (s.size() % 2 != 0) std::abort();
std::vector<unsigned char> result(s.size() / 2);
for (std::size_t i = 0; i != s.size() / 2; ++i)
result[i] = 16 * parse_hex(s[2 * i]) + parse_hex(s[2 * i + 1]);
return result;
}
int
genCADU_impl::work (int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const unsigned char *in = (const unsigned char *) input_items[0];
unsigned char *out = (unsigned char *) output_items[0];
int total_len;
//Copy pdu from circular buffer to local buffer
buffer.insert(buffer.end(), in, in + d_frameLength);
//Optional scrambling and Reed-Solomon. TO DO: Turbo and LDPC
if (d_rs) RS->Encode_RS(buffer);
if (d_scramble) S->Scramble(buffer);
//Insert sync word
buffer.insert(buffer.begin(), d_sync.begin(), d_sync.end());
//Copy from local buffer to circular buffer
std::copy(buffer.begin(),buffer.end(),out);
//Clear the local buffer
total_len = buffer.size();
buffer.clear();
// Tell runtime system how many output items we produced.
return total_len;
}
} /* namespace ccsds */
} /* namespace gr */
Regards,
M.

To Ascii conversion error

#include "stdafx.h"
typedef unsigned char byte;
typedef unsigned char BYTE;
using namespace System::Runtime::InteropServices;
using namespace System;
public ref class CRsiComBase {
public :
static int ToAsciiHex(BYTE* pToBuf, BYTE* pFromBuf, int iStartingIndex, int iLength);
static int ToAsciiHex(cli::array<byte>^ baToBuf, cli::array<byte>^ baFromBuf, int iStartingIndex, int iLength);
static cli::array<BYTE>^ hexTable = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
};
int CRsiComBase::ToAsciiHex(BYTE* pToBuf, BYTE* pFromBuf, int iStartingIndex, int iLength)
{
BYTE* pOutBuf = pToBuf;
for (int j = 0; j < iStartingIndex; j++)
{
*pOutBuf++ = pFromBuf[j];
}
for (int i = iStartingIndex; i < iLength; i++) {
BYTE b = pFromBuf[i];
BYTE hb = b >> 4;
BYTE lb = b & 0x0F;
*pOutBuf++ = hexTable[(int)hb];
*pOutBuf++ = hexTable[(int)lb];
}
return static_cast<int>(pOutBuf - pToBuf); // Length of message in pToBuf;
}
int CRsiComBase::ToAsciiHex(cli::array<byte>^ baToBuf, cli::array<byte>^ baFromBuf, int iStartingIndex, int iLength) {
int iRtn = 0;
int fromLength = sizeof(baFromBuf);
IntPtr pFromBuf = Marshal::AllocHGlobal(fromLength);
Marshal::Copy(baFromBuf, 0, pFromBuf, fromLength);
BYTE* pbFromBuf = (BYTE*)pFromBuf.ToPointer();
int toLength = sizeof(baToBuf);
BYTE* pbToBuf = new BYTE[toLength];
iRtn = ToAsciiHex(pbToBuf, pbFromBuf, iStartingIndex, iLength);
Marshal::Copy(static_cast<IntPtr>(pbToBuf), baToBuf, 0, iLength * 2);
Marshal::FreeHGlobal(pFromBuf);
delete[] pbToBuf;
return iRtn;
}
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
cli::array<byte>^ fromArray = gcnew cli::array<byte>(8);
fromArray[0] = 1;
fromArray[1] = 2;
fromArray[2] = 3;
fromArray[3] = 4;
fromArray[4] = 5;
fromArray[5] = 6;
fromArray[6] = 7;
fromArray[7] = 8;
cli::array<byte>^ toArray = gcnew cli::array<byte>(16);
CRsiComBase::ToAsciiHex(toArray, fromArray, 0, 8);
Console::WriteLine(L"{0}", toArray[0]);
Console::WriteLine(L"{0}", toArray[1]);
Console::WriteLine(L"{0}", toArray[2]);
Console::WriteLine(L"{0}", toArray[3]);
Console::WriteLine(L"{0}", toArray[4]);
Console::WriteLine(L"{0}", toArray[5]);
Console::WriteLine(L"{0}", toArray[6]);
Console::WriteLine(L"{0}", toArray[7]);
return 0;
}
For the above code i am getting error ]1
In the above code i am trying to convert Byte to AsciiHex .
I am not able to figure why delete is causing Heap corruption ?
If i remove delete it will cause exceptions at later part
I am getting error at "delete[] pbToBuf"; line ! please help me by pointing what i am doing wrong :(
sizeof paToBuf ist not the size of the array.
Use the method Length to get the logical size of the array.

Atmega 32, Program to drive motor , How to take input integer from user

I am trying to write a simple program to take input from user by hterm, when User enters "motor" & "25" the motor will rotate in 25 clockwise and 25 anticlockwise direction
//Define clock-speed and include necessary headers
#define F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>
#include <inttypes.h>
#include <avr/io.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdint.h>
#include <util/delay.h>
#include <ctype.h>
#define F_CPU 16000000UL
#define BAUD 9600UL
char cmd[40];
void uart_init(void) // initializing UART
{
UBRRH = 0;
UBRRL = ((F_CPU+BAUD*8)/(BAUD*16)-1);
UCSRC |= 0x86; // 8N1 Data
UCSRB = 0x18; // Receiving and Transmitting
}
int uart_putch(char ch, FILE *stream) // Function for sending Data to PC
{
if (ch == '\n')
uart_putch('\r', stream);
while (!(UCSRA & (1<<UDRE)));
UDR=ch;
return 0;
}
int uart_getch(FILE *stream) // Function for receiving Data from PC
{
unsigned char ch; while (!(UCSRA & (1<<RXC)));
ch=UDR;
uart_putch(ch,stream); // Echo the output back to the terminal
return (tolower(ch));
}
FILE uart_str = FDEV_SETUP_STREAM(uart_putch, uart_getch, _FDEV_SETUP_RW); // Important, not deleting
void loeschen() // Delete the String
{
int strnglen = 0;
while (strnglen < 41 && cmd[strnglen] != 0)
{
cmd[strnglen]= 0;
strnglen++;
}
}
// Define the stepping angle
// Note: Divide by 2 if you are doing half-stepping. for filter test 1.8 defult
#define MIN_STEP 1.8
/* Define an array containing values to be sent at the required Port - for Full-stepping
<first four bits> - <last four bits> = <decimal equivalent>
00000001 = 1 ; 01000000 = 4
00000100 = 4 ; 00010000 = 16
00000010 = 2 ; 00001000 = 8
00001000 = 8 ; 00100000 = 32
*/
unsigned short control_array_full[4] = {4,16,8,32};
/* Define an array containing values to be sent at the required Port - for Half-stepping
<first four bits> - <last four bits> = <decimal equivalent>
0000-1001 = 8 + 1 = 9 ; 0010-0100 = 32 + 4 =36
0000-0001 = 1 ; 0000-0100 = 4
0000-0101 = 4 + 1 = 5 ; 00010100 = 16 + 4 = 20
00000100 = 4 ; 00010000 = 16
00000110 = 4 + 2 = 6 ; 00011000 = 16+8=24
0000-0010 = ; 00-001000 = 8
0000-1010 = 8 + 2 = 10 ; 00-101000 = 40
0000-1000 = 8 ; 00-100000 = 32
*/
unsigned short control_array_half[8] = {36,4,20,16,24,8,40,32};
// Adjust this delay to control effective RPM
// Do not make it very small as rotor will not be able to move so fast
// Currently set at 100ms
void delay()
{
_delay_ms(100);
}
void move_clockwise(unsigned short revolutions){
int i=0;
for (i=0; i < (revolutions* 360 /MIN_STEP) ; i++)
{
//Note: Take modulo (%) with 8 when half-stepping and change array too
PORTD = control_array_half[i % 4];
delay();
}
}
void move_anticlockwise(unsigned short revolutions){
int i;
for (i = (revolutions* 360 /MIN_STEP); i > 0 ; i--){
//Note: Take modulo (%) with 8 when half-stepping and change array too
PORTD = control_array_half[i % 4];
delay();
}
}
int main()
{
// Enter infinte loop
// Make changes here to suit your requirements
uart_init(); // initializing UART
stdout = stdin = &uart_str; // Necessary to compare whole Strings
while(1){
scanf("%s",&cmd); // Read String from Data Register
printf ("Please enter number of motor rotation for clockwise and anticlockwise");
items_read = scanf ("%d", &numbers[i]); // Read integer for motor revolution
if(strcmp(cmd, "motor") == 0)
{
DDRD = 0b00111110; //Set PORTD 4 bits for output
//Enter number of revolutions required in brackets
move_clockwise(items_read);
move_anticlockwise(items_read);
}
DDRD = 0b00000000;
}
loeschen();
}
Now, The problem is that when I will delete these lines from main()
items_read = scanf ("%d", &numbers[i]);
scanf ("%d",&i);
& make items_read in move_clockwise(items_read); as:
move_clockwise(25);
move_anticlockwise(25);
Then when user enters "motor" then motor is running move_clockwise(25); but move_anticlockwise(25); is not running, what I would like is to take both "motor", number for clockwise and number for anticlockwise....
I would really appreciate if anyone can help me with this!
Thanks in advance!
First, in my opinion you're only clearing "cmd" in loeschen(), but you never assining any value.
Second "cmd" is NOT any type of UART dataregister.
DDRD is DataDirectionRegister D, that means you can set some pin to input or output mode.
Use PORTD to set a pin high or low, in example PORTD |= 1<<PD0; to set Port D Pin 0 to high.
I guess you prefer german documentation, because you named one function "loeschen()" ;-), so why don't you visit mikrocontroller.net AVR GCC Tutorial (UART)?
If you like more technic detailed youtube-stuff, et voila: Introduction to UART
RXT (ATMEGA16) - for example:
//////////////////////////////////////////////////////////////////////////
// Definitions
//////////////////////////////////////////////////////////////////////////
#define BAUD 9600UL
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD)
#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
#error Baud to high
#endif
#define UART_MAX_STRING_LENGHT 20 // Max lenght
//////////////////////////////////////////////////////////////////////////
// UART-Receive-Variables
//////////////////////////////////////////////////////////////////////////
volatile uint8_t uart_str_complete = 0; // FLAG - String received
volatile uint8_t uart_str_count = 0; // Current position
volatile char uart_string[UART_MAX_STRING_LENGHT + 1] = ""; // received string
//////////////////////////////////////////////////////////////////////////
// ISR-UART
//////////////////////////////////////////////////////////////////////////
ISR(USART_RXC_vect)
{
unsigned char nextChar;
nextChar = UDR; // read data from buffer
if(uart_str_complete == 0) // UART-String is currently usen
{
if(nextChar != '\n' && nextChar != '\r' && uart_str_count < UART_MAX_STRING_LENGHT)
{
uart_string[uart_str_count] = nextChar;
uart_str_count++;
}
else
{
uart_string[uart_str_count] = '\0';
uart_str_count = 0;
uart_str_complete = 1;
}
}
}
//////////////////////////////////////////////////////////////////////////
// Init UART
//////////////////////////////////////////////////////////////////////////
void Init_UART_Async()
{
UBRRH = UBRR_VAL >> 8;
UBRRL = UBRR_VAL & 0xFF;
UCSRB |= (1<<TXEN); // UART TX high
UCSRB |= (1<<RXEN); // UART RX high
UCSRB |= (1<<RXCIE); // UART RX Interrupt enable
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // Asynchron 8N1
sei(); // Enable interrups
}
//////////////////////////////////////////////////////////////////////////
// Main
//////////////////////////////////////////////////////////////////////////
int main(void)
{
Init_UART_Async();
while(1)
{
HandleCommunication(); // <-- Handle your communication ;-)
// and to some other cool stuff here
}
}
//////////////////////////////////////////////////////////////////////////
// Handle Communication
//////////////////////////////////////////////////////////////////////////
void HandleCommunication()
{
if(uart_str_complete == 1)
{
strcpy(received_string, uart_string); // copy received string
strcpy(uart_string, ""); // empty uart-string
uart_str_complete = 0; // set flag to 0
// handle your communication
}
}
After understanding how UART is working, you should check your codeparts
like "scanf("%s",&cmd);" in an console-application - that's easier to find some errors.
I hope this helps you a little, but I guess the best solution is when you're knowing what you're doing.
-Crazy

Getting this error when using wxList

I got this error when building my project
D:/codelite_workspace/Easy_GO2/main_frame.cpp:48: error:
'wxMyListNode' has not been declared
and this is my main_frame.h
#ifndef MAIN_FRAME_H
#define MAIN_FRAME_H
#include <panels.h>
#include <choose_tree.h>
#include "quicksearch.h"
#include "dealer_data2.h"
#include <wx/wxprec.h>
#include <wx/panel.h>
#include <wx/menu.h>
#include <wx/list.h>
class Main_Frame : public wxFrame{
public:
Main_Frame(const wxString& title);
Insert *insert_panel;
All *view;
quickSearch *q;
Choose_Tree *choose;
wxPanel *parent;
wxBoxSizer *hbox;
wxMenuBar *menubar;
wxMenu *file;
WX_DECLARE_LIST(Dealer_Data2, MyList);
WX_DECLARE_HASH_MAP(wxString, MyList, wxStringHash, wxStringEqual, MyHash);
MyHash h1;
int currentPanel;
};
const int INSERT_PANEL = 110;
const int VIEW_PANEL = 111;
const int QUICK_SEARCH = 112;
const int CURRENT_INSERT = 1;
const int CURRENT_ALL = 2;
#endif
main_frame.cpp
#include "main_frame.h"
#include <wx/listimpl.cpp>
WX_DEFINE_LIST(MyList);
Main_Frame::Main_Frame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(1600, 750))
{
parent = new wxPanel(this, wxID_ANY);
hbox = new wxBoxSizer(wxHORIZONTAL);
//choose = new Choose_Tree(parent);
insert_panel = new Insert(parent);
view = new All(parent);
insert_panel->dealer_id->Append(wxT("1- Test"));
insert_panel->dealer_id->Append(wxT("2- wahba"));
insert_panel->dealer_id->Append(wxT("3- Ahmed"));
insert_panel->dealer_id->Append(wxT("4- Mohamed"));
currentPanel = CURRENT_INSERT;
//hbox->Add(choose, 1, wxEXPAND | wxALL, 5);
hbox->Add(insert_panel, 1, wxEXPAND | wxALL, 5);
view->Hide();
insert_panel->Show();
parent->SetSizer(hbox);
//GetSizer()->Layout();
menubar = new wxMenuBar;
file = new wxMenu;
file->Append(INSERT_PANEL, wxT("&Insert"));
file->Append(VIEW_PANEL, wxT("&All"));
file->Append(QUICK_SEARCH, wxT("&Quick Search"));
menubar->Append(file, wxT("&File"));
SetMenuBar(menubar);
this->Centre();
}
Any help? I will be grateful. :)
Here is a description of how to use WX_DEFINE_LIST
http://docs.wxwidgets.org/2.8/wx_wxlist.html
You have written
WX_DEFINE_LIST(MyList);
but you have not defined MyList anywhere!