my eventual aim is to receive data and display it as both a graph and in individual text boxes (or something better!) the data is 2 x temperature readings and a humidity reading, for example the data sent would be "222160" etc.
However before I even tackle that I am having issues simply showing any data in a textbox. This is the code I am using; (Consists of a textbox for the UI)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace WFARxSimple
{
public partial class Form1 : Form
{
string rxString;
public Form1()
{
InitializeComponent();
myport = new SerialPort();
myport.PortName = "COM5";
myport.BaudRate = 9600;
myport.Parity = Parity.None;
myport.DataBits = 8;
myport.StopBits = StopBits.One;
myport.Open();
}
private SerialPort myport;
private void DisplayText(object sender, EventArgs e)
{
showRx.AppendText(rxString);
}
private void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
rxString = myport.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
}
}
However I cannot get any data to show in my textbox.
You need, at the very least: myPort.DataReceived += myPort_DataReceived;. Do this in Form1().
Related
My Form contains a button and a chart added as shown below.
My code is built such that a separate thread continuously gets data from the sender (which is being sent using the UDP protocol of communication), processes it and adds it to the global GLineSeries object 'gls'. GLineSeries is basically a class of the library which is basically just a list of the datapoints of the graph. My aim is that when the button is clicked this series is added to the chart in the form (cartesianChart1) and the plot shows. This is done using the line cartesianChart1.Series.Add(gls); The code for this is shown below (Form1.cs file)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.WinForms;
using LiveCharts.Wpf;
using LiveCharts.Defaults;
using LiveCharts.Geared;
using System.Windows.Shell;
namespace livecharts_example
{
public partial class Form1 : Form
{
LiveCharts.WinForms.CartesianChart cartesianChart1 = new LiveCharts.WinForms.CartesianChart();
GLineSeries gls;
Thread t;
public Form1()
{
InitializeComponent();
cartesianChart1.Dock = DockStyle.Fill;
this.Controls.Add(cartesianChart1);
t = new Thread(() => {
UdpClient dataUdpClient = new UdpClient(90);
string carIP = "127.0.0.1";
IPEndPoint carIpEndPoint = new IPEndPoint(IPAddress.Parse(carIP), 0);
Byte[] receiveBytes;
gls = new GLineSeries();
gls.Values = new GearedValues<ObservablePoint>();
while (true)
{
receiveBytes = dataUdpClient.Receive(ref carIpEndPoint);
ObservablePoint op = new ObservablePoint(BitConverter.ToInt32(receiveBytes, 0), BitConverter.ToSingle(receiveBytes, 8));
gls.Values.Add(op);
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void button1_Click(object sender, EventArgs e)
{
cartesianChart1.Series.Add(gls);
}
}
}
The problem is that when the button is pressed the program jumps to the program.cs file and throws the error as shown below. I also tried aborting the thread 't' and then adding the lineseries to the chart but the error still arises. Can someone please help?
The following code worked. Thanks for all the suggestions
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.WinForms;
using LiveCharts.Wpf;
using LiveCharts.Defaults;
using LiveCharts.Geared;
using System.Windows.Shell;
using System.Windows;
using System.Collections.ObjectModel;
namespace livecharts_example
{
public partial class Form1 : Form
{
LiveCharts.WinForms.CartesianChart cartesianChart1 = new LiveCharts.WinForms.CartesianChart();
Thread t;
static GLineSeries gls;
public Form1()
{
InitializeComponent();
cartesianChart1.Dock = DockStyle.Fill;
this.Controls.Add(cartesianChart1);
t = new Thread(() => {
UdpClient dataUdpClient = new UdpClient(90);
string carIP = "127.0.0.1";
IPEndPoint carIpEndPoint = new IPEndPoint(IPAddress.Parse(carIP), 0);
Byte[] receiveBytes;
cartesianChart1.Invoke(new Action(() =>
{
Form1.gls = new GLineSeries(); Form1.gls.Values = new GearedValues<ObservablePoint>();
}));
while (true)
{
receiveBytes = dataUdpClient.Receive(ref carIpEndPoint);
ObservablePoint op = new ObservablePoint(BitConverter.ToInt32(receiveBytes, 0), BitConverter.ToSingle(receiveBytes, 8));
cartesianChart1.Invoke(new Action(() => {
gls.Values.Add(op);
}));
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void button1_Click(object sender, EventArgs e)
{
cartesianChart1.Invoke(new Action(()=> {
cartesianChart1.Series.Add(gls);
}));
}
}
}
THIS QUESTION MAY SEAM A DUPLICATE, BUT NOT. after a long period of search on internet and no, result, then had to seek for assistance.
All solutions apply to loading available ports in a combo box and the user checks one at a go. But the automation feature then dies.
thus, im looking for assistance on how the modem can connect automatically from the available ports without user interaction (USER FRIENDLINESS)
FOR THE COMBO BOX, IT IS WORKING FINE AS BELOW,
using GsmComm.GsmCommunication;
using GsmComm.PduConverter;
using GsmComm.Server;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace COMM_All
{
public partial class Comm_F : Form
{
public Comm_F()
{
InitializeComponent();
}
private void COM_PORTS()
{
string[] ports = SerialPort.GetPortNames();
txtGPort1.Items.AddRange(ports);
txtGPort2.Items.AddRange(ports);
}
private void Form1_Load(object sender, EventArgs e)
{
COM_PORTS();
}
private void Modem1_Click(object sender, EventArgs e)
{
if (txtGPort1.Text == "") { MessageBox.Show("Invalid Port Name"); return; }
comm = new GsmCommMain(txtGPort1.Text, 9600, 8);
Cursor.Current = Cursors.Default;
bool retry;
do
{
retry = false;
try
{
Cursor.Current = Cursors.WaitCursor; comm.Open(); Cursor.Current = Cursors.Default;
//MessageBox.Show("Modem Connected Sucessfully");
txtGStatus1.Text = "Connected Sucessfully";
comm.EnableMessageNotifications();
MessageBox.Show("Message notifications activated.");
}
catch (Exception)
{
Cursor.Current = Cursors.Default;
if (MessageBox.Show(this, "GSM Modem is not available", "Check",
MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)
retry = true;
else { return; }
}
} while (retry);
}
}
}
Note: Computer has multiple usb devices;
I am hoping someone can help me in getting an issue of mine to work, I feel as if it is an easy one, however not having any luck in fixing what I am trying to do. I want to be able to pause a video which I am playing using vlc.dotnet below is a brief summary of the structure of my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
using Vlc.DotNet.Forms;
using System.Threading;
using Vlc.DotNet.Core;
using System.Diagnostics;
namespace TS1_C
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button8.Click += new EventHandler(this.button8_Click);
}
void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string chosen = listBox1.SelectedItem.ToString();
string final = selectedpath2 + "\\" + chosen; //Path
playfile(final);
}
void playfile(string final)
{
var control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
// Default installation path of VideoLAN.LibVLC.Windows
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
control.Play();
}
private void button8_Click(object sender, EventArgs e)
{
}
}
}
As you can see I have one method which takes a double click from an item in a list box and plays it using the method playfile. However I want to be able to pause the video using my button known as button8. I have tried many things even this
control.Paused += new System.EventHandler<VlcMediaPlayerPausedEventArgs>(button8_Click);
Which I put into the playfile method, however nothing seems to work. I am wondering if my whole method in which I play a file using playfile(); is completely wrong. I am hoping someone can help me in trying to achieve what I need
Thank you
Your control should be initialized only once:
private VlcControl control;
public Form1()
{
InitializeComponent();
control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
// Default installation path of VideoLAN.LibVLC.Windows
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
}
then, your play method could be simplified:
void playfile(string url)
{
control.Play(url);
}
And for your pause method:
private void button8_Click(object sender, EventArgs e)
{
control.Pause();
}
I have a GUI designed on Visual Studio using C#. I am a beginner in C# but good at C++ programming but due to requirements of task, I am designing it in C#. In this GUI, i have a button that connects to remote ssh server and as a trial, i have following commands to run when user presses button1.
client.Connect();
var output = client.RunCommand("echo happy test");
var dltOutput = client.RunCommand("rm /home/helloWorld.txt");
var launchFirst = client.RunCommand("bash /root/first.sh");
client.Disconnect();
Console.WriteLine(output.Result);
The command to delete "helloWorld.txt" in my home folder runs fine but could not running the command to run the shell script "first.sh". I would like to attach complete code below for your reference.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Renci.SshNet;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
//Connection information
string user = "root";
string pass = "hello123ado";
string host = "192.168.38.50";
int port = 22;
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Console.WriteLine("Say Button1_Clicked");
using (var client = new SshClient(host,user,pass))
{
client.Connect();
var output = client.RunCommand("echo happy test");
var dltOutput = client.RunCommand("rm /home/helloWorld.txt");
var launchFirst = client.RunCommand("bash /root/first.sh");
client.Disconnect();
Console.WriteLine(output.Result);
}
}
private void Button2_Click(object sender, EventArgs e)
{
Console.WriteLine("Say Button2_Clicked");
}
private void Button3_Click(object sender, EventArgs e)
{
Console.WriteLine("Say Button3_Clicked");
}
}
}
I m trying to Dial mobile phone via C# program. Below Show my Program. In this, When i Click my Dial Button it dial the number(Destination number) which i given in my program. But after one or two seconds it is disappeared & its not connect to the that destination number. Below Shows my C# code. Pls help me to solve this problem. Thank you.......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SerialPort sp = new SerialPort();
sp.PortName = "COM10";
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.Handshake = Handshake.XOnXOff;
sp.DtrEnable = true;
sp.RtsEnable = true;
sp.Open();
if (!sp.IsOpen)
{
MessageBox.Show("Serial port is not opened");
return;
}
sp.WriteLine("AT" + Environment.NewLine);
sp.WriteLine("ATD=\"" + "Destination Number" + "\"" + Environment.NewLine);
}
}
}
Finally i found the solution. We should add the semi-colon to end of the destination number. then its worked.
sp.WriteLine("ATD=\"" + "Destination Number;" + "\"" + Environment.NewLine);
Increase your BaudRate to max and use this AT Command:
ATD = DestinationNumber;
This will not work with out ; as the system will think you are taking a data call and not a voice call.
Try moving the decleration of the 'sp' outside the method, like so:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private SerialPort sp;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
sp = new SerialPort();
}
private void button1_Click(object sender, EventArgs e)
{
if (sp.IsOpen)
{
sp.Close();
}
sp.PortName = "COM10";
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.Handshake = Handshake.XOnXOff;
sp.DtrEnable = true;
sp.RtsEnable = true;
sp.Open();
if (!sp.IsOpen)
{
MessageBox.Show("Serial port is not opened");
return;
}
sp.WriteLine("AT" + Environment.NewLine);
sp.WriteLine("ATD=\"" + "Destination Number" + "\"" + Environment.NewLine);
}
}
}
Here is my working dialing cord it rings the phone
Don't knows that how to get the voice input and out put from port I'm using huwavi E173 dongle.Here is my working cord C#
SerialPort port = new SerialPort();
port.Open();
string t = port.ReadExisting();
Thread.Sleep(100);
string cmd = "ATD";
Thread.Sleep(100);
string phoneNumber = "071********";
Thread.Sleep(100);
port.WriteLine(cmd + phoneNumber + ";\r");
port.Close();