LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ComWrt in rs232

Solved!
Go to solution
hi Folks, i am facing issue in writing data to COM port using ComWrt().i dont know exactly where the issues is. My scenario is like whenever com port is connected to my device, device will keep on sending some message. if i am requesting anything in-between to my device, device needs to send some response based on the request made. here i can read the message from my device through ComRdByte() , if i am sending any request msg to device it is not sending any response, i guess it means that my device is not able to undersand my message what i am sending(dont know exactly).Response for the requested message can be succes/error/ack msg but i am not getting any response from my device. i referred the below code to write msg to my device #include #include int i, ch; char src[20], dst[20]; strcpy (src, "0346000101012A4BC0"); for (i = 0; i < strlen (src) / 2; i++) { Scan (src, "%s[i*w2]>%i[r16]", i * 2, &ch); sprintf (dst[i], "%c", ch); } ComWrt (port, dst, strlen (src) / 2); which was posted in http://forums.ni.com/t5/LabWindows-CVI/add-0x-before-sending-RS232-messages/m-p/681366#M34853 any idea on where i am doing mistake??? pls help....
0 Kudos
Message 1 of 5
(5,106 Views)

While revising the code you posted (and the original one linked by you) I seem to have found a little mistake that may possibly explain why your code is not working correctly. In my opinion the for clause should be for (i = 0; i < strlen (src) / 2; i += 2) for the code to work correctly; in the original code infact characters 0-1 are read in the first run, 1-2 in the second and so on, while you should read 0-1 in the first run, 2-3 in the second and so on.

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 5
(5,065 Views)

 

Hi Roberto,

 

Thanks for your suggestion, but still i am facing problem,

 

i think the real problem is with 00,because when converting into ASCII, third byte  00 is noted as null character .so i was not able to read the entire message,finally in dst i am getting only 2 ascii character for 03 and 46 and rest will be omitted by seeing null character (00) . what is the solution to read the all characters. i am struggling to get solution. pls help me

 

 

int i, ch,senddata_len =0;
char src[30], dst[20];
char dst_data[100]={0};
unsigned char final_msg[100]={0};

 strcpy (src, "0346000101012A4BC0");
 senddata_len = strlen(src);

 for (i = 0; i < senddata_len ; i++)
 	{ 
	Scan (src, "%s[i*w2]>%i[r16]", i * 2, &ch); 
	sprintf (&dst[i], "%c", ch);

	//Fmt(dst_data,"%c",ch);

	//strcat(final_msg,dst_data);
 
	} 
	ComWrt (port, dst, senddata_len);

 

0 Kudos
Message 3 of 5
(5,041 Views)
Solution
Accepted by topic author raj129*

Hi,

 maybe this help 🙂

int i, ch,senddata_len =0;
char src[30];
unsigned char dst[20];
//it is nesscesary to work with binary data as unsigned, otherwise some signed/unsigned conversion happen
char dst_data[100]={0};
unsigned char final_msg[100]={0};

strcpy (src, "0346000101012A4BC0");
senddata_len = strlen(src);
senddata_len-=senddata_len%2;//use only lengtht in multiple of two(if input src has bad length "034"
for (i = 0; i < senddata_len ; i+=2)//process every two characters
     {
    //Scan (src, "%s[i*w2]>%i[r16]", i * 2, &ch);
    //Scan(...)posibly good command, but i am not familiar with "Scan
// and i think next code can be litle faster after some optimization"
    //{Warning:this code not check invalid characters like "03T600"}
    ch=0;
    if((src[i]>='0')&&(src[i]<='9'))ch=src[i]-'0';
    if((src[i]>='a')&&(src[i]<='f'))ch=src[i]-'a'+10;
    if((src[i]>='A')&&(src[i]<='F'))ch=src[i]-'A'+10;
    ch=ch<<4;//shift data by 4bits(the same as ch=ch*16;)
    if((src[i+1]>='0')&&(src[i+1]<='9'))ch|=src[i+1]-'0';
    if((src[i+1]>='a')&&(src[i+1]<='f'))ch|=src[i+1]-'a'+10;
    if((src[i+1]>='A')&&(src[i+1]<='F'))ch|=src[i+1]-'A'+10;
    //sprintf (&dst[i], "%c", ch);
    //do not use sprintf to contencate binary data
    dst[i/2]=ch;
    }

    //ComWrt (port, dst, senddata_len);
    //Binary data has only half size of its Ascci representation
    
    ComWrt (port, dst, senddata_len/2);
Message 4 of 5
(5,000 Views)

Hi OVR_CZ,

 

it really works well..

u have solved my issues.

Thank you very much Smiley Happy

0 Kudos
Message 5 of 5
(4,990 Views)