0

How can i receive the data from my Arduino Mega 2560 with Visual C# on my PC.

I send a short databyte (2 bytes) and a timestamp (4 bytes) over USB to PC

//initialization
const int RX = 0;
const int TX = 1; 
void setup(){
pinMode(RX, INPUT); //readpin for USB
pinMode(TX, OUTPUT); //writepin for USB
... 
Serial.begin(28800); //open interface
}
 //loop()
 Serial.print(incomingByte, BIN); //sending databyte
 sendBinary(currentMillis); //send timestamp
//method for timestamp
void sendBinary(long currnetMillis)
 {
 int temp = currentMillis&0xFFFF; //16 bit of the low-byte
 sendBinary(temp);
 temp = currentMillis >> 16; //16 bit of the high-byte
 sendBinary(temp);
 }

I have surched in the internet for an example but i didn ́t find a good. So i ask here what ist the best way with Visual Studio C# to receive the incoming data? An example code will be nice because i ́m a beginner and didn ́t understand the jargon.

With frindly wishes sniffi

asked Jun 13, 2016 at 6:21

1 Answer 1

0

I haven't tried the code below, but it should point you in the right direction.

All you have to do is write a C# program that opens the Serial port that your Arduino is connected to and read and write from there like you would from a file.

  1. Open Visual Studio, create a C# command line project.

  2. Paste this code inside main()

     System.IO.Ports.SerialPort port = new SerialPort("COM1", 9600);
     try
     {
     while (true)
     {
     int readData = port.ReadByte();
     System.Console.WriteLine("Read {0} from the port", readData);
     }
     }
     catch(Exception) // Press CTRL-C to exit.
     {
     }
     port.Close();
    
  3. Compile the code and run it.

answered Jun 13, 2016 at 12:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.