我在C中有一个看起来像这样的结构:
typedef u_int8_t NN;
typedef u_int8_t X;
typedef int16_t S;
typedef u_int16_t U;
typedef char C;
typedef struct{
X test;
NN test2[2];
C test3[4];
U test4;
} Test;
Run Code Online (Sandbox Code Playgroud)
我已经将字段的结构和写入值声明如下:
Test t;
int t_buflen = sizeof(t);
memset( &t, 0, t_buflen);
t.test = 0xde;
t.test2[0]=0xad; t.test2[1]=0x00;
t.test3[0]=0xbe; t.test3[1]=0xef; t.test3[2]=0x00; t.test3[3]=0xde;
t.test4=0xdeca;
Run Code Online (Sandbox Code Playgroud)
我通过UDP将此结构发送到服务器.目前我在本地测试时工作正常,但是我现在需要将这个结构从我的little-endian机器发送到big-endian机器.我真的不确定怎么做.
我已经研究过使用htons但我不确定这是否适用于这种情况,因为它似乎只定义为unsigned ints16位或32位,如果我理解正确的话.
我编写了一个UDP发送/接收函数来发送一个结构并监听另一个结构.字节必须按特定顺序发送,但这正好我正在使用#pragma pack(1).我现在唯一的问题是,如果结构中出现任何Null值(0x00),则Null消失后的其余数据.
我想有一些相当简单的事情,我做错了,但这是我的代码:
typedef u_int8_t NN;
typedef u_int8_t X;
typedef int32_t S;
typedef u_int32_t U;
typedef char C;
typedef struct{
X test;
NN test2[2];
C test3[4];
S test4;
} Test;
int main(int argc, char** argv)
{
Test t;
memset( &t, 0, sizeof(t));
t.test = 0xde;
t.test2[0]=0xad; t.test2[1]=0x00;
t.test3[0]=0xbe; t.test3[1]=0xef; t.test3[2]=0xde; t.test3[3]=0xca;
t.test4=0xde;
LogOnResponse response;
udp_send_receive(&t, &response);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我的发送/接收功能:
int send_and_receive(void* message, void* reply, int do_send, int expect_reply)
{
struct sockaddr_in …Run Code Online (Sandbox Code Playgroud) 我已经为我正在处理的项目添加了一个Unix域套接字.套接字有一个简单的功能,它只是广播代码从另一个设备中提取的数据,其思路是其他应用程序将能够从套接字读取这些数据.
我写了一个简单的服务器代码,当我在笔记本电脑上运行代码时,使用Ubuntu 10.04 VM,它运行得非常好.但是,当我将代码复制到嵌入式设备上时,我正在使用代码失败,当我的应用程序尝试写入套接字时代码退出.
在/var/log/messages我看到以下消息:
Dec 2 15:12:17 box local1.info my-app[17338]: Socket Opened
Dec 2 15:12:17 box local1.err my-app[17338]: Socket Failed
Dec 2 15:12:17 box local1.err my-app[17338]: Protocol wrong type for socket
Dec 2 15:12:38 box local1.info ./server[17178]: accept failed: Invalid argument
Run Code Online (Sandbox Code Playgroud)
这是服务器代码:
#include <stdio.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include<syslog.h>
#define SV_SOCK_PATH "/tmp/rtig.sock" //path to be used by socket
#define BUF_SIZE 256 //Max length of string listened to
#define BACKLOG 5
int main(int argc, …Run Code Online (Sandbox Code Playgroud) 这很简单,我无法弄清楚.这是代码:
def combine(h0, gyro, vx, bias, len=400.0):
for i in range(len(gyro)):
print i
a = combine(3.0, [0.1, 0.2,0.3], [0.1, 0.2,0.3], 0.1)
这给了我一个错误:
File "indoor.py", line 84, in a = combine(3.0, [0.1, 0.2,0.3], [0.1, 0.2,0.3], 0.1) File "indoor.py", line 28, in combine for i in range(len(gyro)): TypeError: 'float' object is not callable
我猜它会变得非常简单,但我看不出错误!