我正在为将使用ZeroMQ套接字传输数据并发布多部分消息的设备编写数据记录系统。
我的程序需要用C语言编写。
我确实设法编写了一个完全正常工作的Python脚本。 这看起来像这样:
<link href="https://www.magictoolbox.com/static/magiczoomplus/magiczoomplus.css" rel="stylesheet"/>
<script src="https://www.magictoolbox.com/static/magiczoomplus/magiczoomplus.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="product_zoom" role="dialog" tabindex="-1" style="display: none;" class="modal fade cboxElement">
<button data-dismiss="modal" title="Close" class="mz-button mz-button-close"></button>
</div>
<div class="modal-body">
<div class="col-sx-12 col-md-3 col-lg-offset-1 col-lg-3">
<h2>Jacket</h2>
<p><i>244662</i>black</p>
</div>
<div class="col-sx-12 col-md-7 col-lg-5"><a id="jeans" data-options="zoomMode: off; variableZoom: true; zoomPosition: inner; upscale: true; cssClass: white-bg fl-zoom; expand: window" href="https://dnsv4oxp52xck.cloudfront.net/244/2446620001_png_zoom_1.jpg" class="MagicZoom"><img src="https://dnsv4oxp52xck.cloudfront.net/244/2446620001_png_zoom_1.jpg" style="max-width: 2000px; max-height: 2000px;" class="main-visual"></a>
<div class="thumbs-container"><a data-image="https://dnsv4oxp52xck.cloudfront.net/244/2446620001_png_zoom_1.jpg" href="https://dnsv4oxp52xck.cloudfront.net/244/2446620001_png_zoom_1.jpg" data-zoom-id="jeans" class="mz-thumb"><img src="https://dnsv4oxp52xck.cloudfront.net/244/2446620001_png_zoom_1.jpg" class="main-visual"></a></div>
</div>
</div>
<script src="js/vendor/magiczoomplus.js" async></script>
<script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-50ed24e34032223b" async></script>
这是我第一次使用ZMQ并尝试将此Python代码移植到C是一个真正的挑战。我现在编写的C代码将运行,但无法正常运行:
Dim FirstSplit as String()
FirstSplit = Name.Split(",")
fname = FirstSplit(0).Trim()
但是,这段代码会提供正确的传入数据大小,但是当显示数据时,它显示为不可读的字符(符号等等)。我将需要像在Python中一样将输入数据作为一个字符串,以便进一步处理它。
答案 0 :(得分:0)
我发现Python中的numpy数组被转换为包含int的字符串数组,并且它们是非常难以为C程序解释的值。
通过操纵接收的数据,我能够提取正确的值:
#include "zhelpers.h"
int main (int argc, char *argv [])
{
int i = 0;
void *context = zmq_ctx_new();
void *subscriber = zmq_socket(context,ZMQ_SUB);
int rc = zmq_connect(subscriber,"tcp://localhost:50290");
assert(rc == 0);
char* filter = "ray.";
rc = zmq_setsockopt(subscriber,ZMQ_SUBSCRIBE, filter, sizeof(filter));
assert(rc == 0);
int nextFrame = 0;
while (1)
{
zmq_msg_t message;
zmq_msg_init (&message);
zmq_msg_recv (&message, subscriber,0);//, ZMQ_NOBLOCK);
// Process the message frame
int size = zmq_msg_size(&message);
char *string = malloc(size + 1);
memcpy(string, zmq_msg_data(&message), size);
if (nextFrame)
{
zmq_msg_close (&message);
printf("Raw data: ");
string[size] = 0;
//printf("Data: %s\n", string);
for ( i = 0; i < size; i+=2)
{
printf("%d ", (int16_t) ((string[i] & 0xff) +((string[i+1] & 0xff)<<8)));
}
}
if (strcmp(string, "ray.rawdata") != 0)
{
nextFrame = 0;
break;
}
else
{
nextFrame = 1;
zmq_msg_close (&message);
string[size] = 0;
printf("Prefix: %s\n", string);
if (!zmq_msg_more (&message))
break; // Last message frame
}
}}
return 0;
}
请注意,这只是从Python接收numpy数组时的问题 - 只要它是一个心跳字符串或其他什么,就没有问题。