如何从Matlab套接字客户端向C套接字服务器发送连续数据?

时间:2019-10-14 18:45:11

标签: python c matlab sockets redpitaya

我正在与RedPitaya开发委员会合作,该委员会运行Linux。该应用程序需要在板上运行的套接字服务器与在Matlab上运行的远程客户端之间进行通信。我正在使用的C服务器代码已探查可与Python客户端一起使用,因此,我试图通过自己的修改来重新创建该Python客户端,使其适合我的需求。

我要传递四个数字作为数据,但是只有一个正传递给服务器。

系统使用uint32格式的数字工作,每个字节对系统都有不同的含义,我尝试过更改Matlab客户端发送的数据类型,更改操作是同步还是异步类型,将其作为字符串发送,但是只是发送的第一个数据似乎是从服务器获取响应的

这是C服务器中接收数据的部分:

 if(bind(sock_server, (struct sockaddr *)&addr, sizeof(addr)) < 0)
 {
   perror("bind");
   return EXIT_FAILURE;
 }
 listen(sock_server, 1024);
 printf("Listening on port serverPort ...\n");

 while(1)
{

if((sock_client = accept(sock_server, NULL, NULL)) < 0)
{
  perror("accept");
  return EXIT_FAILURE;
}
while(1) //MSG_WAITALL
{
  //sleep(1);
  rx = recv(sock_client, (char *)&command, 4, MSG_DONTWAIT);
  if (rx == 0) {
     break;
     measuring = 0;
  }
  if(rx > 0) {
     value = command & 0xfffffff;
     switch(command >> 28)
     {
       case 1: /* Trigger Delay - Timing */
            timing = command & 0xff;
                            printf("timing: %zu \n",timing);
            break;

       case 2: /* NSAMPLES */
            if ((command & 0xff) < 10)
                nsamples = (command & 0xff);
            else
            nsamples = 10;
                            printf("samples: %zu \n",nsamples);
            break;

       case 3: /* NAVERAGES */
            naverages = command & 0xff;
                            printf("averages: %zu \n",naverages);
            break;

       case 0: /* fire */

            // set trigger and NSAMPLES set NAVERAGES and stop measurement
            *((int32_t *)(cfg + 0)) = (STOP) + (timing<<8) + (nsamples<<16) + (naverages<<24);

            //sleep(0.1); // wait 0.1 second

            time_begin = clock();
            // start measurement
            *((int32_t *)(cfg + 0)) ^= 1;
            measuring = 1;
            break;
     }
  }

Python客户端以这种方式发送数据:

if self.idle: return
self.socket.write(struct.pack('<I', 1<<28 | self.cbTrigger.currentIndex()))
self.socket.write(struct.pack('<I', 2<<28 | self.cbNOS.currentIndex()))
self.socket.write(struct.pack('<I', 3<<28 | self.cbNOA.currentIndex()))

matlab客户端非常简单:

t=tcpip('RedPitayaIP',serverPort,'NetworkRole','client')
pause(1);
fopen(t);

我以这种方式发送数据:

fwrite(t,variable_trigger_or,'uint32')
fwrite(t,variable_samples_or,'uint32')
fwrite(t,variable_average_or,'uint32')

Python客户端发送数据,并且客户端给出以下响应:

Litening to port serverPort
timing: 16
samples: 10
averages: 0

Matlab客户端只能得到这个

Listening to port serverPort
timing: 16

所以,如果有人可以帮助我了解正在发生的事情,或者如果我缺少一些东西,我对此并不陌生,但也不会成为专家

0 个答案:

没有答案