fwrite different following fread in a tcpip connection? [matlab]










0














In the process of writing a class to connect 2 instances of matlab together. the instances will be on separate computers but I'm currently testing on 1 computer.



Currently I'm able to establish a connection between both matlabs and I'm able to send/receive messages between them.



code:



classdef connectcompstogether<handle
properties
serverIP
clientIP
tcpipServer
tcpipClient
Port = 4000;
bsize = 8;
Message
end

methods
function gh = connectcompstogether(~)
% gh.serverIP = '127.0.0.1';
gh.serverIP = 'localhost';
gh.clientIP = '0.0.0.0';
end

function SetupServer(gh)
gh.tcpipServer = tcpip(gh.clientIP,gh.Port,'NetworkRole','Server');
set(gh.tcpipServer,'OutputBufferSize',gh.bsize);
fopen(gh.tcpipServer);
display('Established Connection')
end

function SetupClient(gh)
gh.tcpipClient = tcpip(gh.serverIP,gh.Port,'NetworkRole','Client');
set(gh.tcpipClient,'InputBufferSize',gh.bsize);
set(gh.tcpipClient,'Timeout',30);
fopen(gh.tcpipClient);
display('Established Connection')
end
function CloseClient(gh)
fclose(gh.tcpipClient);
end
end
methods
function sendmessage(gh,message)
fwrite(gh.tcpipServer,message,'double');
end

function recmessage(gh)
gh.Message = fread(gh.tcpipClient,gh.bsize);
end
end
end


matlab 1



gh = connectcompstogether;
gh.SetupServer();
gh.sendmessage(555);


matlab 2



gh = connectcompstogether;
gh.SetupClient();
gh.recmessage();


the message sent is an 8 bit double 555.
However when looking at the received message it turns out to be a matrix



64
129
88


don't understand what is going on as the examples I have been following don't have this problem.



and to add context. I'm trying to connect 2 matlabs through TCP-IP so I can control one instance with another. my plan is to have a second matlab waiting for command codes and execute specified functions when the 1st matlab requests.










share|improve this question


























    0














    In the process of writing a class to connect 2 instances of matlab together. the instances will be on separate computers but I'm currently testing on 1 computer.



    Currently I'm able to establish a connection between both matlabs and I'm able to send/receive messages between them.



    code:



    classdef connectcompstogether<handle
    properties
    serverIP
    clientIP
    tcpipServer
    tcpipClient
    Port = 4000;
    bsize = 8;
    Message
    end

    methods
    function gh = connectcompstogether(~)
    % gh.serverIP = '127.0.0.1';
    gh.serverIP = 'localhost';
    gh.clientIP = '0.0.0.0';
    end

    function SetupServer(gh)
    gh.tcpipServer = tcpip(gh.clientIP,gh.Port,'NetworkRole','Server');
    set(gh.tcpipServer,'OutputBufferSize',gh.bsize);
    fopen(gh.tcpipServer);
    display('Established Connection')
    end

    function SetupClient(gh)
    gh.tcpipClient = tcpip(gh.serverIP,gh.Port,'NetworkRole','Client');
    set(gh.tcpipClient,'InputBufferSize',gh.bsize);
    set(gh.tcpipClient,'Timeout',30);
    fopen(gh.tcpipClient);
    display('Established Connection')
    end
    function CloseClient(gh)
    fclose(gh.tcpipClient);
    end
    end
    methods
    function sendmessage(gh,message)
    fwrite(gh.tcpipServer,message,'double');
    end

    function recmessage(gh)
    gh.Message = fread(gh.tcpipClient,gh.bsize);
    end
    end
    end


    matlab 1



    gh = connectcompstogether;
    gh.SetupServer();
    gh.sendmessage(555);


    matlab 2



    gh = connectcompstogether;
    gh.SetupClient();
    gh.recmessage();


    the message sent is an 8 bit double 555.
    However when looking at the received message it turns out to be a matrix



    64
    129
    88


    don't understand what is going on as the examples I have been following don't have this problem.



    and to add context. I'm trying to connect 2 matlabs through TCP-IP so I can control one instance with another. my plan is to have a second matlab waiting for command codes and execute specified functions when the 1st matlab requests.










    share|improve this question
























      0












      0








      0







      In the process of writing a class to connect 2 instances of matlab together. the instances will be on separate computers but I'm currently testing on 1 computer.



      Currently I'm able to establish a connection between both matlabs and I'm able to send/receive messages between them.



      code:



      classdef connectcompstogether<handle
      properties
      serverIP
      clientIP
      tcpipServer
      tcpipClient
      Port = 4000;
      bsize = 8;
      Message
      end

      methods
      function gh = connectcompstogether(~)
      % gh.serverIP = '127.0.0.1';
      gh.serverIP = 'localhost';
      gh.clientIP = '0.0.0.0';
      end

      function SetupServer(gh)
      gh.tcpipServer = tcpip(gh.clientIP,gh.Port,'NetworkRole','Server');
      set(gh.tcpipServer,'OutputBufferSize',gh.bsize);
      fopen(gh.tcpipServer);
      display('Established Connection')
      end

      function SetupClient(gh)
      gh.tcpipClient = tcpip(gh.serverIP,gh.Port,'NetworkRole','Client');
      set(gh.tcpipClient,'InputBufferSize',gh.bsize);
      set(gh.tcpipClient,'Timeout',30);
      fopen(gh.tcpipClient);
      display('Established Connection')
      end
      function CloseClient(gh)
      fclose(gh.tcpipClient);
      end
      end
      methods
      function sendmessage(gh,message)
      fwrite(gh.tcpipServer,message,'double');
      end

      function recmessage(gh)
      gh.Message = fread(gh.tcpipClient,gh.bsize);
      end
      end
      end


      matlab 1



      gh = connectcompstogether;
      gh.SetupServer();
      gh.sendmessage(555);


      matlab 2



      gh = connectcompstogether;
      gh.SetupClient();
      gh.recmessage();


      the message sent is an 8 bit double 555.
      However when looking at the received message it turns out to be a matrix



      64
      129
      88


      don't understand what is going on as the examples I have been following don't have this problem.



      and to add context. I'm trying to connect 2 matlabs through TCP-IP so I can control one instance with another. my plan is to have a second matlab waiting for command codes and execute specified functions when the 1st matlab requests.










      share|improve this question













      In the process of writing a class to connect 2 instances of matlab together. the instances will be on separate computers but I'm currently testing on 1 computer.



      Currently I'm able to establish a connection between both matlabs and I'm able to send/receive messages between them.



      code:



      classdef connectcompstogether<handle
      properties
      serverIP
      clientIP
      tcpipServer
      tcpipClient
      Port = 4000;
      bsize = 8;
      Message
      end

      methods
      function gh = connectcompstogether(~)
      % gh.serverIP = '127.0.0.1';
      gh.serverIP = 'localhost';
      gh.clientIP = '0.0.0.0';
      end

      function SetupServer(gh)
      gh.tcpipServer = tcpip(gh.clientIP,gh.Port,'NetworkRole','Server');
      set(gh.tcpipServer,'OutputBufferSize',gh.bsize);
      fopen(gh.tcpipServer);
      display('Established Connection')
      end

      function SetupClient(gh)
      gh.tcpipClient = tcpip(gh.serverIP,gh.Port,'NetworkRole','Client');
      set(gh.tcpipClient,'InputBufferSize',gh.bsize);
      set(gh.tcpipClient,'Timeout',30);
      fopen(gh.tcpipClient);
      display('Established Connection')
      end
      function CloseClient(gh)
      fclose(gh.tcpipClient);
      end
      end
      methods
      function sendmessage(gh,message)
      fwrite(gh.tcpipServer,message,'double');
      end

      function recmessage(gh)
      gh.Message = fread(gh.tcpipClient,gh.bsize);
      end
      end
      end


      matlab 1



      gh = connectcompstogether;
      gh.SetupServer();
      gh.sendmessage(555);


      matlab 2



      gh = connectcompstogether;
      gh.SetupClient();
      gh.recmessage();


      the message sent is an 8 bit double 555.
      However when looking at the received message it turns out to be a matrix



      64
      129
      88


      don't understand what is going on as the examples I have been following don't have this problem.



      and to add context. I'm trying to connect 2 matlabs through TCP-IP so I can control one instance with another. my plan is to have a second matlab waiting for command codes and execute specified functions when the 1st matlab requests.







      matlab tcp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 8:09









      Hojo.Timberwolf

      393114




      393114






















          1 Answer
          1






          active

          oldest

          votes


















          1














          tcpip/fread default precision is uchar, so by default fread will output a column array of 8-bit unsigned integers.



          You either need to specifiy that a double is expected:



          %size divided by 8, as one 8-byte value is expected rather than 8 1-byte values
          gh.Message = fread(gh.tcpipClient,gh.bsize/8,'double');


          Or typecast the uint8 array to a double:



          rawMessage = fread(gh.tcpipClient,gh.bsize); %implicit: rawMessage is read in 'uchar' format 
          % cast rawMessage as uint8 (so that each value is stored on a single byte in memory, cancel MATLAB automatic cast to double)
          % then typecast to double (tell MATLAB to re-interpret the bytes in memory as double-precision floats)
          % a byteswap is necessary as bytes are sent in big-endian order while native endianness for most machines is little-endian
          gh.Message = swapbytes(typecast(uint8(rawMessage),'double'));





          share|improve this answer






















          • Thanks for the explanation. That explains why I'm receiving an array. The first example given fixed the problem. I tried the second though and typecast appeared to not have an affect on the rawMessage. any reason why?
            – Hojo.Timberwolf
            Nov 12 at 8:53






          • 1




            Perhaps Matlab reads the input as bytes, then casts it as double (i.e. outputs an array of double precision floats with integer values in the 0-255 range). So, rawMessage should be manually cast as uint8. I will edit the answer.
            – Brice
            Nov 12 at 9:29










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258054%2ffwrite-different-following-fread-in-a-tcpip-connection-matlab%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          tcpip/fread default precision is uchar, so by default fread will output a column array of 8-bit unsigned integers.



          You either need to specifiy that a double is expected:



          %size divided by 8, as one 8-byte value is expected rather than 8 1-byte values
          gh.Message = fread(gh.tcpipClient,gh.bsize/8,'double');


          Or typecast the uint8 array to a double:



          rawMessage = fread(gh.tcpipClient,gh.bsize); %implicit: rawMessage is read in 'uchar' format 
          % cast rawMessage as uint8 (so that each value is stored on a single byte in memory, cancel MATLAB automatic cast to double)
          % then typecast to double (tell MATLAB to re-interpret the bytes in memory as double-precision floats)
          % a byteswap is necessary as bytes are sent in big-endian order while native endianness for most machines is little-endian
          gh.Message = swapbytes(typecast(uint8(rawMessage),'double'));





          share|improve this answer






















          • Thanks for the explanation. That explains why I'm receiving an array. The first example given fixed the problem. I tried the second though and typecast appeared to not have an affect on the rawMessage. any reason why?
            – Hojo.Timberwolf
            Nov 12 at 8:53






          • 1




            Perhaps Matlab reads the input as bytes, then casts it as double (i.e. outputs an array of double precision floats with integer values in the 0-255 range). So, rawMessage should be manually cast as uint8. I will edit the answer.
            – Brice
            Nov 12 at 9:29















          1














          tcpip/fread default precision is uchar, so by default fread will output a column array of 8-bit unsigned integers.



          You either need to specifiy that a double is expected:



          %size divided by 8, as one 8-byte value is expected rather than 8 1-byte values
          gh.Message = fread(gh.tcpipClient,gh.bsize/8,'double');


          Or typecast the uint8 array to a double:



          rawMessage = fread(gh.tcpipClient,gh.bsize); %implicit: rawMessage is read in 'uchar' format 
          % cast rawMessage as uint8 (so that each value is stored on a single byte in memory, cancel MATLAB automatic cast to double)
          % then typecast to double (tell MATLAB to re-interpret the bytes in memory as double-precision floats)
          % a byteswap is necessary as bytes are sent in big-endian order while native endianness for most machines is little-endian
          gh.Message = swapbytes(typecast(uint8(rawMessage),'double'));





          share|improve this answer






















          • Thanks for the explanation. That explains why I'm receiving an array. The first example given fixed the problem. I tried the second though and typecast appeared to not have an affect on the rawMessage. any reason why?
            – Hojo.Timberwolf
            Nov 12 at 8:53






          • 1




            Perhaps Matlab reads the input as bytes, then casts it as double (i.e. outputs an array of double precision floats with integer values in the 0-255 range). So, rawMessage should be manually cast as uint8. I will edit the answer.
            – Brice
            Nov 12 at 9:29













          1












          1








          1






          tcpip/fread default precision is uchar, so by default fread will output a column array of 8-bit unsigned integers.



          You either need to specifiy that a double is expected:



          %size divided by 8, as one 8-byte value is expected rather than 8 1-byte values
          gh.Message = fread(gh.tcpipClient,gh.bsize/8,'double');


          Or typecast the uint8 array to a double:



          rawMessage = fread(gh.tcpipClient,gh.bsize); %implicit: rawMessage is read in 'uchar' format 
          % cast rawMessage as uint8 (so that each value is stored on a single byte in memory, cancel MATLAB automatic cast to double)
          % then typecast to double (tell MATLAB to re-interpret the bytes in memory as double-precision floats)
          % a byteswap is necessary as bytes are sent in big-endian order while native endianness for most machines is little-endian
          gh.Message = swapbytes(typecast(uint8(rawMessage),'double'));





          share|improve this answer














          tcpip/fread default precision is uchar, so by default fread will output a column array of 8-bit unsigned integers.



          You either need to specifiy that a double is expected:



          %size divided by 8, as one 8-byte value is expected rather than 8 1-byte values
          gh.Message = fread(gh.tcpipClient,gh.bsize/8,'double');


          Or typecast the uint8 array to a double:



          rawMessage = fread(gh.tcpipClient,gh.bsize); %implicit: rawMessage is read in 'uchar' format 
          % cast rawMessage as uint8 (so that each value is stored on a single byte in memory, cancel MATLAB automatic cast to double)
          % then typecast to double (tell MATLAB to re-interpret the bytes in memory as double-precision floats)
          % a byteswap is necessary as bytes are sent in big-endian order while native endianness for most machines is little-endian
          gh.Message = swapbytes(typecast(uint8(rawMessage),'double'));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 10:14

























          answered Nov 12 at 8:29









          Brice

          1,359110




          1,359110











          • Thanks for the explanation. That explains why I'm receiving an array. The first example given fixed the problem. I tried the second though and typecast appeared to not have an affect on the rawMessage. any reason why?
            – Hojo.Timberwolf
            Nov 12 at 8:53






          • 1




            Perhaps Matlab reads the input as bytes, then casts it as double (i.e. outputs an array of double precision floats with integer values in the 0-255 range). So, rawMessage should be manually cast as uint8. I will edit the answer.
            – Brice
            Nov 12 at 9:29
















          • Thanks for the explanation. That explains why I'm receiving an array. The first example given fixed the problem. I tried the second though and typecast appeared to not have an affect on the rawMessage. any reason why?
            – Hojo.Timberwolf
            Nov 12 at 8:53






          • 1




            Perhaps Matlab reads the input as bytes, then casts it as double (i.e. outputs an array of double precision floats with integer values in the 0-255 range). So, rawMessage should be manually cast as uint8. I will edit the answer.
            – Brice
            Nov 12 at 9:29















          Thanks for the explanation. That explains why I'm receiving an array. The first example given fixed the problem. I tried the second though and typecast appeared to not have an affect on the rawMessage. any reason why?
          – Hojo.Timberwolf
          Nov 12 at 8:53




          Thanks for the explanation. That explains why I'm receiving an array. The first example given fixed the problem. I tried the second though and typecast appeared to not have an affect on the rawMessage. any reason why?
          – Hojo.Timberwolf
          Nov 12 at 8:53




          1




          1




          Perhaps Matlab reads the input as bytes, then casts it as double (i.e. outputs an array of double precision floats with integer values in the 0-255 range). So, rawMessage should be manually cast as uint8. I will edit the answer.
          – Brice
          Nov 12 at 9:29




          Perhaps Matlab reads the input as bytes, then casts it as double (i.e. outputs an array of double precision floats with integer values in the 0-255 range). So, rawMessage should be manually cast as uint8. I will edit the answer.
          – Brice
          Nov 12 at 9:29

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258054%2ffwrite-different-following-fread-in-a-tcpip-connection-matlab%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3