5 years, 5 months ago.

message on COM port is lost

Hello all, I would like to send some values thrue the COM port.

unsigned char message5[5];
message5[0]=0xE1;
message5[1]=0x01;
message5[2]=0x02;                 
message5[3]=0x03;                 
message5[4]=0x04;                 
pocitac.printf("%s\n",message5);

In this case everything is OK, I have got message length 6 characters (on the end is \n) But I need to send some data, which are changing:

unsigned char message5[5];
uint8_t pwm = 60;
message5[0]=0xE1;
message5[1]=0x01;
message5[2]=pwm;                 
message5[3]=0x03;                 
message5[4]=0x04;                 
pocitac.printf("%s\n",message5);

But in this case I always have got message length 4 characters (the \n is on 4. position). It looks that pwm value cut my message and positions message5[3]=0x03; message5[4]=0x04; are lost.

Do you have any idea, where is a problem and how to solve it?

Thank you very much for your help.

Lukas

1 Answer

5 years, 5 months ago.

printf("%s\n", message5) means you want to send a string as indicated by the formatter %s. The printf method expects a string that must end with a 0x00 character value. Your printf will fail if it does not find a 0x00 and end too early when pwm is 0. You should use a number of putc() calls instead of printf and make sure the right number of chars is transmitted.

Accepted Answer

Thank you very much, problem was solved :)

posted by Lukas Bier 26 Nov 2018