I am new to serial interfacing and my company has asked me to design an API that configures a gyroscope -connected to a WIFI chip- using a C code, by sending the hex commands over a UDP socket to the WIFI chip. I made sure to adhere to the format provided by the manufacturer's manual but it seems that i am missing something when it comes to the serial interfacing aspect. I am using this code to do so:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "port.h"
#include <time.h>
void waitFor (unsigned int );
unsigned char head1 = 0xff; //head
unsigned char head2 = 0xaa; //head2
unsigned char saveadd = 0x00; //saveconfig address
unsigned char savevalue = 0x00; //save config 0x01 is factory reset
unsigned char endd = 0x00; //end
int
main(int argc, char **argv)
{
struct sockaddr_in myaddr; /* our address */
struct sockaddr_in remaddr; /* remote address */
remaddr.sin_addr.s_addr = inet_addr("192.168.1.1");
remaddr.sin_port = htons(8889);
socklen_t addrlen = sizeof(remaddr); /* length of addresses */
int recvlen; /* # bytes received */
int fd; /* our socket */
int msgcnt = 0; /* count # of messages we received */
remaddr.sin_family = AF_INET;
/* create a UDP socket */
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket\n");
return 0;
}
/* bind the socket to any valid IP address and a specific port */
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(SERVICE_PORT);
if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
perror("bind failed");
return 0;
}
printf("waiting on port %d\n", SERVICE_PORT);
// Hex command that turns a builtin LED on
unsigned char comPart1 = 0xFF;
unsigned char comPart2 = 0xAA;
unsigned char comPart3 = 0x1b;
unsigned char comPart4 = 0x00;
unsigned char comPart5 = 0x00;
unsigned char commands [5]; /* unsigned char array to hold command */
commands [0] = comPart1;
commands [1] = comPart2;
commands [2] = comPart3;
commands [3] = comPart4;
commands [4] = comPart5;
unsigned char temp_buf [2]; /*buffer used to send hex command*/
temp_buf [0]= (unsigned char) 0x00;
temp_buf [1]= '\0';
for (int i = 0; i < 5 ; i++)
{
temp_buf [0] = commands[i];
if (sendto(fd, temp_buf, sizeof (temp_buf), 0, (struct sockaddr *)&remaddr, addrlen) < 0)
perror("sendto");
}
waitFor (0.5); /*delay specified by user manual*/
printf ("%s \n", "Now saving the configration");
//save commands as specified by user manual
unsigned char saveConfig [5];
saveConfig [0] = head1;
saveConfig [1] = head2;
saveConfig [2] = saveadd;
saveConfig [3] = savevalue;
saveConfig [4] = endd;
for (int i = 0; i < 5 ; i++)
{
temp_buf [0] = saveConfig [i];
if (sendto(fd, temp_buf, sizeof (temp_buf), 0, (struct sockaddr *)&remaddr, addrlen) < 0)
perror("sendto");
waitFor (0.05);
}
waitFor (0.45);
}
void waitFor (unsigned int secs) {
unsigned int retTime = time(0) + secs; // Get finishing time.
while (time(0) < retTime); // Loop until it arrives.
}
I was able to configure the gyroscope serially (if disconnected from the WIFI chip and connected to a USB-TTL) using CuteCom. So i know that the hardware works. What am I missing when it comes to serial interfaces. How would one go about achieving such a task?
sendto) tosizeof (temp_buf)which is 2. If you want to send a single byte per datagram, set the parameter to1. What happens if you send the whole 5 byte command in a single datagram, e.g.:sendto(fd, commands, 5, 0, (struct sockaddr *)&remaddr, addrlen)?gcc, at a minimum use:-Wall -Wextra -pedanticI also use:-Wconversion -std=gnu99Then you will get a sizeable list of of warnings from the compiler. For fixes, since parametersargcandargvare not used, the appropriate signature formain()isint main( void )There are also several unused variables.time()is eitherNULLor the address of a variable with typetime_tThe0is an iffy fit for NULL, Strongly suggest make the parameterNULL