Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TeHuster · Mar 22, 2017 at 01:38 PM · stringintarduinoserialportascii

Send many ints from Unity To Arduino.

Hi Guys,

I'm trying to send many ints to my "masterArduino". Because the SerialPort object only send strings. I've tried many things including: - Creating a string from ints ( didn't work because the size of the string.length is dynamic). - Then i tried to convert these ints to chars, this because all values are between 0-255, then put the char into a string and send it.

This sort of works. However i think there is no value for 0 in char world. So the data is not right. But there must be a better way?

 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 = "";         
     }

Any help would be greatly appreciated! Thanks!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TeHuster · Mar 22, 2017 at 12:03 PM 0
Share
 Arduino Code:
 
 void loop() {
    int serialIndex = 0;  
    int i2cIndex = 0; 
    while (0 < Serial.available()) { // loop through all the received bytes 
       char bufferByte = 0;     
       bufferByte = Serial.read();
       serialBuffer[serialIndex] = (byte) bufferByte;  // put current index byte in array      
       serialIndex ++;                    // add index. 
       if(serialIndex%12==0 && serialIndex != 0){
          sendBytes(0);      
       }
    } 
     //sendBytes(0);  
     delay(50);
 }
 
 void sendBytes(int slave){
   byte i2cBuffer[12];
   int bufferIndex = slave * 12;
   for(int i = 0; i < 12; i++){
     i2cBuffer[i] = serialBuffer[i + bufferIndex];
   }
   Wire.beginTransmission(slave+1);
   Wire.write(i2cBuffer, 12);
   Wire.endTransmission();  
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gameplay4all · Mar 22, 2017 at 01:53 PM

Try using Int.ToString("D5") or something similar. This forces the number to be displayed with 5 digits. So 345.ToString("D7") returns 0000345. Now just split the received string into segments of 7 or how many digits you need (3 in the case of 0-255) on the arduino side and cast them to Ints!

Good luck!

-Gameplay4all

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TeHuster · Mar 22, 2017 at 03:47 PM 0
Share

This was the answer i didn't knew i was looking for! Thanks a bunch, the communication works like a charm now! :).

avatar image
1

Answer by _dns_ · Mar 22, 2017 at 02:51 PM

Hi, one way to convert binary information to a string is use Base64 encoding, it was designed exactly for that (and to minimize size too).

Check "System.Convert.ToBase64String()" and "System.Convert.FromBase64String()". I don't know what kind of environment you have on Arduino side, but Base64 encoding/decoding is quite standard in many languages/libraries.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TeHuster · Mar 22, 2017 at 03:48 PM 0
Share

Thanks dns! I should add this when i got more time!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

parseInt givin' me issues! 2 Answers

Convert Text to float 3 Answers

How to convert a string to int array in Unity C# 1 Answer

How to turn a String to an Int? 5 Answers

Convert int to string and back again 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges