Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to asked, how to automatically create a serial port in unity? there any sample source code.Thanks
Posted
Comments
Richard MacCutchan 25-Mar-14 13:11pm    
What do you mean by "create a serial port"?
Sergey Alexandrovich Kryukov 25-Mar-14 13:30pm    
Simple. It means that OP's unit has self-assembling hardware, controlled by software applications. :-)
—SA
CHill60 25-Mar-14 16:27pm    
:laugh:
Richard MacCutchan 25-Mar-14 17:17pm    
Ah, so unity is an assembly robot.
Sergey Alexandrovich Kryukov 25-Mar-14 18:15pm    
:-)
It must have the API for self-assembly... :-)
—SA

1 solution

Man the dropkick trolls why do they even bother to post, oh wait they are trying to be funny .. legends in their own lunchboxes.

Anyhow the answer to your question in unity this will get you started you will need to change the com3 and baud rate to whatever port and speed you need

C#
//unity code

using UnityEngine;
using System.Collections;
using System.IO.Ports;
     
public class serialControlBasic : MonoBehaviour {
     
            SerialPort stream;
           
           
            //valid bauds 1200,2400,4800,9600,14400,19200,38400,57600 and 115200 
           
            void Start () {
                    stream = new SerialPort("COM3", 9600); //change port to your needs
                    stream.Open(); //Open the Serial Stream.
            }
           
     
           
            // Update is called once per frame
            void Update () {

            }
           
           
 }


A small Arduino test code will something like
C#
void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println(random(1,1000)); //Send Random number to computer
  delay(10);                      //Delay between sends.
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 26-Mar-14 4:44am    
Why do we bother to post? In the rather forlorn hope that people will actually start to give proper thought to their problems before posting such vague questions.
leon de boer 27-Mar-14 0:19am    
The post is very specific about what he wants there is nothing vague about it the word automatically is probably the only vague part.

You and the other idiot posts seem to troll around the word "create" for opening a port well if you think hard you need to take that issue up with Microsoft you have not opened a serial port in a very long time.

You use CreateFile and CreateFileEx to open an already existing file for reading and in many cases the file is actually read only. For a com port under win32/64 framework with exactly the same routines the specific open com port routines long gone. We could also add you can now also have virtual com ports which really are created and the like but it's just fluff to the answer.

So perhaps take your troll jokes to Microsoft because of the above "creating" a com port has become standard terminology and if you don't realize that and why perhaps the joke is on you.
Richard MacCutchan 27-Mar-14 4:54am    
Thank you for clarifying that; and for your childish abuse.
[no name] 26-Mar-14 5:22am    
This is very likely what OP wanted. For completeness I would add the stream.Readline to read the data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900