我是新的串行接口,我的公司已经要求我设计一个API,配置陀螺仪-连接到WIFI芯片-使用C代码,通过发送十六进制命令通过UDP套接字到WIFI芯片。我确保遵守制造商手册提供的格式,但似乎我在串行接口方面缺少了一些东西。我使用以下代码:
#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 socketn");
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 %dn", 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]= ' ';
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.
}
我能够使用CuteCom串行配置陀螺仪(如果与WIFI芯片断开连接并连接到USB-TTL)。所以我知道硬件可以工作。当涉及到串行接口时,我错过了什么。如何去完成这样的任务呢?
你在评论中说过/问过
然而,在你的代码中,你在每个数据字节之间发送了一个NULL字节。我基本上试图发送以下内容:0xff 0xaa 0x1b 0x000x00一次一个字节。你认为NULL(字节的零)是造成这个问题?如果是,我该如何指示缓冲区的结束不用它?
temp_buf
包含2个字节,将第二个字节硬编码为