LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

comport error -6

 

I am getting com port error -6 in following code.

 

//****************************************************************************************************************************************

 

           uCHAR   i = 0;

            char        ComName[8] = {0};

 

            uCHAR   recMessage[250];

            int     notifyCnt     = 7;

            int     comInQueue    = 0;

           

           

           uCHAR       tmepMessage[]   = {0x80, TARGET_ECU_ADDRESS, SOURCE_TESTER_ADDRESS, 0x02,

                                                                                        StartDiagnosticSession, 0xF2, 0x00};

           

            tmepMessage[6]  = CheckSumComputeAtCOMthread(&tmepMessage[0], 6);

                                   

            while(1)

             {

           

             selectedCOMport  =  0;

                       

                        if(availableCOMports[i])

                        {

                                    clearArrayTimerThread(&recMessage[0], 250);

                                    strcpy(ComName,"");

                                    strcmp(ComName,"COM");

                                    strcat(ComName,&i);

                                   

                                   if(OpenComConfig(i, &ComName[0], 38400, 0, 8, 1, 250, -1) < 0)

                                    {

                                                printf("Error in com open\n");

                                                break;

                                    }

 

                                    //No Handshaking

                                    SetCTSMode(selectedCOMport, LWRS_HWHANDSHAKE_OFF);                       

           

                                    //  Make sure Serial buffers are empty

                          FlushInQ  (selectedCOMport);

                          SetComTime(selectedCOMport, 10);

                                   

                                   

                                    // Sending request

                                               

                        printf("\n");

                        for(i=0; i<notifyCnt; i++)

                                    printf("%x ", tmepMessage[i]);

 

                                    // Send data to COM port

                                    if(ComWrt (selectedCOMport, tmepMessage, notifyCnt) == 13)

                                    {

                       

                                    }

           

                          Delay(0.6);

                                   

                                    comInQueue = GetInQLen (selectedCOMport);

                                   

                                    if(ComRd (selectedCOMport, recMessage , comInQueue) > 0)    

                                    {

                       

                                                printf("\n");

                                                for(i=0; i<comInQueue; i++)

                                                            printf("%x ", recMessage[i]);

                                   

                       

                                                // Make sure Serial buffers are empty

                                                FlushInQ  (selectedCOMport);

                                    }

           

                        }

                        i++; 

            }

                                   

            return ;

}

//****************************************************************************************************

 

 what could be the problem??

0 Kudos
Message 1 of 10
(6,387 Views)

what is the exact line / command that generates the error? and what are the parameter values you pass to the function?

0 Kudos
Message 2 of 10
(6,386 Views)

I am not passing any parameter to function.

 

I had checked available ports in availableCOMports array and then opening each port and checking which port is connected to

ECU.

 

 

That is I am checking com ports which are connected to ECU.

 

error here:

 if(OpenComConfi​g(i, &ComName[0], 38400, 0, 8, 1, 250, -1) < 0)

                                    {

                                                printf("Error in com open\n");

                                                break;

                                    }

 

 

 

I am not able to open any com for sending message.

 

 

0 Kudos
Message 3 of 10
(6,382 Views)

@MAPJ wrote:

I am not passing any parameter to function.

 

 if(OpenComConfi​g(i, &ComName[0], 38400, 0, 8, 1, 250, -1) < 0)

                                    {

                                                printf("Error in com open\n");

                                                break;

                                    }


Well, I do see some parameters, i, ComName,...Smiley Wink

 

If I am not mistaken, you initialize i with 0, and this should give a library function error (return value == -2): Invalid port number because if you pass a NULL pointer or an empty string for device name (and you seem to do so), the library uses device names depending on the port number you specify - but there is no COM 0....

 

So you are not seeing this error but -6 only?


 

0 Kudos
Message 4 of 10
(6,375 Views)

Oh sorry I thought you are asking parameters for function where this code is written.

 

Yes for OpenComConfi​g() function,

I = 1 then name is "COM1".

 

Actually its going in if for (that is calling opencomconfig)

I = 1

I = 4 only.

 

Names are COM1 and COM4.

 

So chances of error in that .

 

Error is -6  that serial com not available.

 

What is this serial com not available??

 

My cable is connected to 2 ports.

 

And I have one more software where its showing com1 and com 4 are available and this function is working there.

 

So is there pre-requisite for calling this function???

 

0 Kudos
Message 5 of 10
(6,372 Views)

Ok, so your definition is:

 

availableCOMports[0] = 0

availableCOMports[1] = 1

availableCOMports[2] = 0

availableCOMports[3] = 0

availableCOMports[4] = 1

 

But I do not understand the following lines of your code:

 

strcpy(ComName,"");

strcmp(ComName,"COM");

 

I would assume that you wanted to write something like

 

strncat(ComName,"COM");

 

But why don't you leave ComName empty, i.e. ""?

 

Message 6 of 10
(6,366 Views)

I think you need to revise the ANSI C string manipulation functions a little.

 

To generate the port name, you have:

strcpy(ComName,"");
strcmp(ComName,"COM");
strcat(ComName,&i);

 

which frankly doesn't make much sense. strcmp() is the string compare function; and passing strcat() the address of a uChar variable and magically expecting that to become a string is a bit too hopeful.

 

You would be bettter with, e.g.:

sprintf(ComName, "COM%d", i);

 

You are also using the variable i in for loops within the outer while loop, whilst still expecting it to be the right value when you get to the i++ statement at the end of the loop.

--
Martin
Certified CVI Developer
Message 7 of 10
(6,364 Views)

Oh yes MSAXON,

I did string manipulation there which is nt making any sense.

Actually I wanted to do STRCPY.

My mistake.

Thanks for pointing it.

 

forget my previous code.

 

Hi Wolfgang ,

I did wrong string manipulation.

I just want to open com port no 1 for communication.

So I did following simple code.

Its again saying error -6 which is serial com port nt available.

 

And actually com1 is available.

And No other application is using it.

 

This new sample trial code to open com port.

 

 

 

#include <rs232.h>

#include<stdio.h>         

 

 

void main()

{

                                 if(OpenComConfig(1, "COM1", 38400, 0, 8, 1, 250, -1) < 0)

                                    {

                                                printf("Error in com open\n");

                                               

                                    }

 

                                     getchar();

            return ;             

}

 

 

now what could be the error??

 

 

0 Kudos
Message 8 of 10
(6,352 Views)

and yes this is correct.

 

availableCOMports[0] = 0

availableCOMports[1] = 1

availableCOMports[2] = 0

availableCOMports[3] = 0

availableCOMports[4] = 1

 

 

0 Kudos
Message 9 of 10
(6,351 Views)

I got solution.

 

It had problem here

in first code

:  strcat(ComName,&I);

 

 

and second code is also running when I read what are default names of ports in Windows.

Accordingly I changed it.

 

And Yes thanks to  msaxon for pointing string manipulation mistake.

And  msaxon my that loop while(1) was trial so that at least it go into while so I set it 1

o.w condition is there for breaking loop.I completed it.

 

 

Also thanks to Wolfgang for suggestion of setting com port name as NULL ("").

It works for any port no if you are not assigning names to port No's.

 

But I am still not getting what problem it had with strcat??

when I commented that line , my first code opened the com port. 

 

is it memory problem??

0 Kudos
Message 10 of 10
(6,342 Views)