I’m trying to make some communication for my PIC devices and I use UDP for it.
I may send anything to socket, when i send one char like ‘R’ or any other. code understands it and everything works okay, but when im trying to send integer it doesnt work.
static UDP_SOCKET MySocket;
BYTE i;
This works:
if(!UDPIsGetReady(MySocket))
return;
UDPGet(&i);
if(i == 'R') SetDCPWM1(255);
UDPGet(&i);
if(i == 'G') SetDCPWM1(155);
This code doesn’t work:
UDPGet(&i);
SetDCPWM1(255-atoi(i));
It compiles, it doesnt crash, it just doesnt work.
The problem is a can send and recieve letters ( chars) but i cant do the same with integers. Like i may send ‘R’ and i recieve it, but i cant send ‘1’ or ’20’, but i really want to send an integer from 0 to 255.
First, the
atoifunction wants a string, and you are supplying it with a single byte. Secondly,atoionly accepts digits, if the value you receive is a non-digit then it would have returned zero.I suggest you try this instead:
The
i - '0'converts an ASCII digit to the actual value. I added theifstatement to make sure that the received digit really is a digit, otherwise the calculation would be wrong and may even end up negative number.