Click here to Skip to main content
15,885,216 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to Send & Receive data using USB port in C# Pin
Richard MacCutchan21-Oct-12 22:00
mveRichard MacCutchan21-Oct-12 22:00 
GeneralRe: How to Send & Receive data using USB port in C# Pin
willington.d21-Oct-12 23:02
willington.d21-Oct-12 23:02 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Richard MacCutchan21-Oct-12 23:24
mveRichard MacCutchan21-Oct-12 23:24 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Dave Kreskowiak22-Oct-12 2:08
mveDave Kreskowiak22-Oct-12 2:08 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 5:07
protectorMarco Bertschi22-Oct-12 5:07 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Dave Kreskowiak22-Oct-12 10:25
mveDave Kreskowiak22-Oct-12 10:25 
GeneralRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 21:35
protectorMarco Bertschi22-Oct-12 21:35 
AnswerRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 5:02
protectorMarco Bertschi22-Oct-12 5:02 
willington.d wrote:
How to do this in c#? Is there any Namespase or thrd party dll available?


You can use the SerialPort class which is available in the namespace System.IO.Ports.

You need the following variables to initialize a serial port (you can treat the USB port as a serial port).
C#
private SerialPort _port;       //Serial port (USB)
const int BAUDRATE = 9600;      //Baud-Rate of the serial port (must be the same value on the other device)
const string COMPORT = "COM8";  //Name of the port
delegate void DisplayArduinoSerialOutput(string arduinoSerialOutput); //Delegate to display the serial input buffers' content to the User


In the constructor of your App, you need to call the following method to initialize your serial port:
C#
/// <summary>
/// This method initializes the COM-Port
/// </summary>
private void InitCOMPort()
{
   _port = new SerialPort(COMPORT) { BaudRate = BAUDRATE };
   //Event occurs when any data arrives at the port
   _port.DataReceived += new   SerialDataReceivedEventHandler(_port_DataReceived); 
   //Open the port 
   //An exception can rise if another program blocked
   //the port
   _port.Open();
}


Afterwards, your port is ready to use:
C#
/// <summary>
/// Occurs when new data is available at the ports input buffer
/// </summary>
/// <param name="sender">the sender of the event (usually the port)</param>
/// <param name="e">Some event args, not used at the moment</param>
private void _port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
   DisplayArduinoSerialOutput displayArduinoSerialOutput = new DisplayArduinoSerialOutput(arduinoSerialOutputTextBox.AppendText);
   SerialPort spL = (SerialPort)sender;
   this.Dispatcher.Invoke(displayArduinoSerialOutput, spL.ReadExisting());
}

In my case, I invoke the UI to add the data to a text box using the Delegate located near my other variables.

Please note: Maybe you have to change the piece of code in the _port_DataReceived-Eventhandler (because I used WPF for this snippet).

However, hope I was able to answer your question.
GeneralRe: How to Send & Receive data using USB port in C# Pin
Richard Andrew x6422-Oct-12 7:48
professionalRichard Andrew x6422-Oct-12 7:48 
SuggestionRe: How to Send & Receive data using USB port in C# Pin
Marco Bertschi22-Oct-12 21:43
protectorMarco Bertschi22-Oct-12 21:43 
QuestionError: Calling C++ dll function in C# Pin
taibc21-Oct-12 17:33
taibc21-Oct-12 17:33 
SuggestionRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan21-Oct-12 21:59
mveRichard MacCutchan21-Oct-12 21:59 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc21-Oct-12 22:01
taibc21-Oct-12 22:01 
GeneralRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan21-Oct-12 22:55
mveRichard MacCutchan21-Oct-12 22:55 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc21-Oct-12 23:28
taibc21-Oct-12 23:28 
GeneralRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan21-Oct-12 23:34
mveRichard MacCutchan21-Oct-12 23:34 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc21-Oct-12 23:40
taibc21-Oct-12 23:40 
GeneralRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan21-Oct-12 23:41
mveRichard MacCutchan21-Oct-12 23:41 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc21-Oct-12 23:45
taibc21-Oct-12 23:45 
GeneralRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan22-Oct-12 0:08
mveRichard MacCutchan22-Oct-12 0:08 
GeneralRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan22-Oct-12 0:10
mveRichard MacCutchan22-Oct-12 0:10 
GeneralRe: Error: Calling C++ dll function in C# Pin
J4amieC22-Oct-12 0:25
J4amieC22-Oct-12 0:25 
GeneralRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan22-Oct-12 1:05
mveRichard MacCutchan22-Oct-12 1:05 
GeneralRe: Error: Calling C++ dll function in C# Pin
taibc22-Oct-12 0:37
taibc22-Oct-12 0:37 
GeneralRe: Error: Calling C++ dll function in C# Pin
Richard MacCutchan22-Oct-12 1:03
mveRichard MacCutchan22-Oct-12 1:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.