Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey everyone.

I have a little problem here.

What I'm trying to do is sending data on serial port. I've written the necessary codes, but there appears a debug error.

The error text is this..

Run - Time Check Failure #2, Stack around the variable "a" was corrupted.

The Code that I wrote is below:

C#
    char a = 1;

    printf("Press a key: \n");

    scanf("%d", &a) ;

    switch(a)
    {
        case 1:
            {
                char data1[8] = "___id:";
                int size1 = 8;
                ser.write(data1,size1);
                break;
            }
        case 2:
            {
                char data2[8] = "___sr:";
                int size2 = 8;
                ser.write(data2,size2);
                break;
            }

        case 3:
            {
                char data3[8] = "___np:";
                int size3 = 8;
                ser.write(data3,size3);
                break;
            }
        case 4:
            {
                char data4[8] = "_cocn:";
                int size4 = 8;
                ser.write(data4,size4);
                break;
            }
}



The Code for write through serial port is given below:

C#
int SerPort::write(const char *data, unsigned int size)
{
    unsigned long ret;

    if (myPort != INVALID_HANDLE_VALUE && myStatus == STATUS_OPEN)
    {
        if (!WriteFile(myPort, data, size, &ret, NULL))
        {
            printf("SerPort::write: Error on writing.\n");
            return -1;
        }
        return ret;
    }

    printf("SerPort::write: Connection invalid.\n");
    return -1;
}


Does anyone has an idea?
My best Regards...
Posted
Updated 4-Aug-11 8:02am
v3

You should use
C++
    int a = 1;
 
    printf("Press a key: \n");
 
    scanf("%d", &a) ;
//...
 
Share this answer
 
Comments
Guyverthree 4-Aug-11 9:45am    
+5 this is correct
Un_NaMeD 4-Aug-11 13:08pm    
Thanks for the advice, what your said is true. My 5!
C pallini is correct the problem that you are having is that a char is only a single byte.

an int can be 2 4 or even sometimes 8 bytes in the length this is why the stack is corrupting.
You are trying to put 2/4/8 bytes into the address space of a single byte.

This then overwrites the data futher down the stack address space.
 
Share this answer
 
v2
Comments
Un_NaMeD 4-Aug-11 13:07pm    
Thank you for your definition of stack corruption. My 5!
This may not be the solution to your stack corruption but there is another problem in that code.

All of your strings you are sending over the serial line are declared to be 8 characters and you transmit 8 characters but the strings are only 6 characters (7 if you count the null at the end).

You allocations are OK but the "receiver" may not like either the NULL character or the fact that after the first 7 characters, the 8th character is junk (leftover from the stack's previous use).

in this code:
C#
char data1[8] = "___id:";
int size1 = 8;
ser.write(data1,size1);

data[0] = '_';
data[1] = '_';
data[2] = '_';
data[3] = 'i';
data[4] = 'd';
data[5] = ';';
data[6] = '\0'; (null)
data[7] = (unitialized / leftover junk character)

and you blindly send 8 characters out the serial port. I can't tell you what the right thing to do is or how to re-write your code because I do not know what the other "program / device" reading the data on the serial line is expecting but I'm willing to bet that it's not expecting "junk".
 
Share this answer
 
v3
Comments
Un_NaMeD 4-Aug-11 13:12pm    
Hi sir.
I didn't get what you mean at the last sentence. Could you explain it with details?
Also, How can I rewrite my code according to your sayings. Could you show mr an example?
Thanks...
Chuck O'Toole 4-Aug-11 13:39pm    
see my updated solution above
Un_NaMeD 4-Aug-11 13:52pm    
Got it man. Thank you...
This line causing the problem,

scanf("%d", &a) ;

Replace with this,

scanf("%c", &a) ;

so if you take a as character your switch case also has to be changed(may be case 1 to case '1'). so you can declare a as int, if memory is not a problem:).
 
Share this answer
 
v2
Comments
Un_NaMeD 4-Aug-11 8:27am    
Hey sir,
I've made the adjustment, nothing has changed...
From your code i can think only the problem lies in that line.Try to rebuild the project and check.Also since you are using c++, try to use cin, cout.
Un_NaMeD 4-Aug-11 13:06pm    
Ohh my, I see it now :)

It really gets on my nerves when can't see this kind of problems.
Thank you :)
Chuck O'Toole 4-Aug-11 10:21am    
He really can't change it to %c because he later uses "a" in a switch statement and the cases are 1, 2, 3, 4, none of which are single characters (well, they are ctrl-A, ctrl-B, ctrl-C, and ctrl-D). In any event, he needs an integer for the code to work as he expects. Also, he needs to use a more meaningful prompt as "press a key" will not get him the required value for the switch statement to work.
printf("Press a key [1..4]: \n");
switch(_getch()-'0')
{
	case 1: /* ... */ break;
	case 2: /* ... */ break;
	case 3: /* ... */ break;
	case 4: /* ... */ break;
}

Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900