Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
VB
Hello, I have a problem to receive data from serialport but threading (in spot) and at the same time I can send data for activated LED.

I do not know how to apply the thread to receive data on spot in C #.

thank you for helping me
Posted
Comments
Sergey Alexandrovich Kryukov 30-Mar-14 22:34pm    
Not clear what's the problem. You just read from the port...
This is not a question. "I do not know..." is not informative.
—SA
tuning1001 30-Mar-14 22:51pm    
Problem I can not do both at once write and read.

I want to read the data and other data sent on the same port at the same time.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace Ibrahim23
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//// chercher les ports ouvert ///
private void bsearch_Click(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
search1.Items.Add(port);
}
}

//// recevoir les données /////
string t;
private void bdonnee_Click(object sender, EventArgs e)
{
t = search1.Text.ToString();
sErial(t);
}
//
SerialPort sp;
void sErial(string Port_name)
{
sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
sp.Open();
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string w = sp.ReadLine();
string msg = sp.ReadExisting();
if (w != String.Empty)
{
Invoke(new Action(() => tbRX.AppendText(w)));
}
}
//// envoyé les données /////
private void bsend_Click(object sender, EventArgs e)
{
string t = search1.Text.ToString();
string s = tbTX.Text.ToString();
sErial(t, s);

}

void sErial(string Port_name, string data_send)
{
SerialPort sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);
sp.Open();

sp.Write(data_send);
sp.Close();
}
//// effacer tout /////
private void bclear_Click(object sender, EventArgs e)
{
tbTX.Clear();
tbRX.Clear();
}
}
}

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