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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.