从Unity To Arduino发送许多变量

时间:2017-03-22 13:05:28

标签: c# string unity3d arduino serial-port

我正在尝试向我的“masterArduino”发送许多注意事项。 因为SerialPort对象只发送字符串。我尝试了很多东西,包括:

  • 从int创建一个字符串(因为string.length的大小是动态的,所以不起作用)。
  • 然后我尝试将这些int转换为chars,这是因为所有值都在0-255之间,然后将char放入一个字符串并发送它。

这种作品。但是,我认为char世界中没有0值。所以数据不对。但必须有更好的方法吗?

<cfscript>
    mySheet = spreadSheetNew("My Sheet");
    spreadSheetAddRow(mySheet, "'Col. A','Col. B','Col. C'");
    for(i=1; i <= RandRange(1, 100); i++){
        spreadSheetAddRow(mySheet, "'Row A#i#','Row B#i#','Row C#i#'");
    }
    spreadSheetFormatRow(mySheet, {bold = true, fontsize = 24}, 1);
    spreadSheetFormatRows(mySheet, {fontsize = 16}, "2-#mySheet.rowcount#");
    cfheader(name = "Content-Disposition", value = 'inline; fileName="test.xls"');
    cfcontent(type="application/vnd.ms-excel", variable="#spreadSheetReadBinary(mySheet)#");
</cfscript>

任何帮助将不胜感激!谢谢!

Arduino代码:

void sendInfo() {
    for (var i = 0; i < peltiers.Length; i++) {
        char tempHot = (char) peltiers[i].GetComponent<Peltier>().hot;
        char charTemp = (char) peltiers[i].GetComponent<Peltier>().temp;
        peltierInfo += tempHot.ToString();           
        peltierInfo += charTemp.ToString();                  
    } 
    sp.WriteLine(peltierInfo);
    Debug.Log(peltierInfo);
    sp.BaseStream.Flush();
    peltierInfo = "";         
}

2 个答案:

答案 0 :(得分:1)

为了能够发送任何整数,首先将它们编码为一个字符串,用某些东西将它们分开(例如'\0' char),然后解码该字符串。

void sendInfo() {
    ...
    peltierInfo += peltiers[i].GetComponent<Peltier>().hot.ToString();
    peltierInfo += '\0';
    peltierInfo += peltiers[i].GetComponent<Peltier>().temp.ToString();
    peltierInfo += '\0';
    ...
}

void loop() {
    int serialIndex = 0;  
    int i2cIndex = 0;
    // set to how many digits there can be in an incoming number plus 1
    int maxNumberLen = 20; 
    char buffer[20];
    // position at which we now put a char that makes up our number
    char* currentCharPtr = buffer;
    while (0 < Serial.available()) { // loop through all the received bytes 
       char bufferByte = 0;     
       bufferByte = Serial.read();
       *currentCharPtr = bufferByte;
       // move pointer forward
       currentCharPtr ++;
       // end of a number in string
       if (bufferByte == '\0') {
           printf("Got number %s\n", buffer);
           // atoi parses string to int
           serialBuffer[serialIndex] = atoi(buffer); 
           serialIndex ++;                    
           if(serialIndex%12==0 && serialIndex != 0){
              sendBytes(0);
           }
           // fill buffer with zeros after we found a number
           memset(buffer, 0, 20);
           currentCharPtr = buffer;
       }
    } 
     //sendBytes(0);  
     delay(50);

 }

答案 1 :(得分:0)

感谢您的回复,您的回答正是我如何运作的。我只是略有不同,并没有时间上传这篇文章。什么方式更好,还是仅仅是品味问题?

统一

body {
    width: 100%;
    height: 100%;
    font-family: "Lora", "Helvetica Neue", Helvetica, Arial, sans-serif;
    color: white;
    background-color: black;
    margin: 0;
    padding: 0;
}

.intro {
    display: table;
    width: 100%;
    height: auto;
    padding: 100px 0;
    text-align: center;
    color: white;
}

.intro .background-image {
    z-index: -1;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: url(https://wallpaperbrowse.com/media/images/html-color-codes-color-tutorials-hero-00e10b1f.jpg);
    -webkit-filter: blur(10px);
    filter: blur(10px);
}

.intro .intro-body {
  z-index: 9999;
  display: table-cell;
  vertical-align: middle;
}

Arduino的

 void sendInfo()
{
    for (int i = 0; i < peltiers.Length; i++)
    {
        peltierInfo += ",";
        peltierInfo += peltiers[i].GetComponent<Peltier>().hot.ToString();
        peltierInfo += ",";
        peltierInfo += peltiers[i].GetComponent<Peltier>().temp.ToString("D3");
    }
    //Debug.Log(peltierInfo);
    sp.WriteLine(peltierInfo);
    sp.BaseStream.Flush();
    peltierInfo = "";
}

感谢您的帮助!