08-20-2012 05:23 AM
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??
08-20-2012 05:29 AM
what is the exact line / command that generates the error? and what are the parameter values you pass to the function?
08-20-2012 05:42 AM
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(OpenComConfig(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.
08-20-2012 06:13 AM
@MAPJ wrote:
I am not passing any parameter to function.
if(OpenComConfig(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,...
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?
08-20-2012 06:25 AM
Oh sorry I thought you are asking parameters for function where this code is written.
Yes for OpenComConfig() 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???
08-20-2012 07:09 AM
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. ""?
08-20-2012 07:16 AM
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.
08-20-2012 11:00 PM
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??
08-20-2012 11:04 PM
and yes this is correct.
availableCOMports[0] = 0
availableCOMports[1] = 1
availableCOMports[2] = 0
availableCOMports[3] = 0
availableCOMports[4] = 1
08-21-2012 01:39 AM
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??