I am writing a C# program that will read data from multiple serial port simultaneously and log that data to file. Each port will have it's own dedicated file to write to.
I was able to successfully create a class to do this with one port, however when I attempt to do it with two ports only the port that is opened second has any data written to the file.
Below is the class I have written to handle the data received events from the serial port and write to the files:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.IO.Ports;
namespace Shell
{
public class Shell
{
private string port_name;
static SerialPort port;
static FileStream fs;
static StreamWriter stream;
/*------------------------------------------------------
Constructors can be added as needed to take
additional inputs for more flexibility.
------------------------------------------------------*/
public Shell(string name)
{
port = new SerialPort();
port_name = name;
port.PortName = name;
port.BaudRate = 115200;
port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Handshake = Handshake.None;
port.Encoding = Encoding.UTF8;
}
/*------------------------------------------------------
Close the serial port and file.
------------------------------------------------------*/
public void close()
{
if (port.IsOpen)
{
port.DataReceived -= port_DataReceived;
port.Close();
stream.Dispose();
fs.Close();
}
}
/*------------------------------------------------------
Add the serial data received handler, open the file to
log to, and open the serial port.
------------------------------------------------------*/
public void open(string path)
{
if (!port.IsOpen)
{
fs = File.Open(path, FileMode.Create, FileAccess.ReadWrite);
stream = new StreamWriter(fs);
stream.AutoFlush = true;
stream.WriteLine("Start");
port.DataReceived += port_DataReceived;
port.Open();
}
}
/*------------------------------------------------------
Handler for the serial data events.
------------------------------------------------------*/
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = port.ReadExisting();
if (!string.IsNullOrEmpty(data))
{
stream.Write(data);
}
}
}
}
And here is the code from the simple form I am using to open and close the connections:
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;
namespace shell_log_test
{
public partial class Form1 : Form
{
private Shell.Shell port1;
private Shell.Shell port2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
port2 = new Shell.Shell("COM5");
port2.open("port2.log");
port1 = new Shell.Shell("COM12");
port1.open("port1.log");
}
private void button2_Click(object sender, EventArgs e)
{
port1.close();
port2.close();
}
}
}
Thank you in advance for the assistance.
Change the following:
static SerialPort port;
static FileStream fs;
static StreamWriter stream;
to:
private SerialPort port;
private FileStream fs;
private StreamWriter stream;
Related
I am try to make a VOice chat over IP using C#. First I have connected two laptops. Then I have sent message between two laptops. Then I try to send vice message it is difficult. I counld get microphone voice input using Naudio. Then I should do sampling and put wave file into byte array and make data package and send it. In client side I should catch the data packet and conver it to sound. But the problem is I cant find any material to learn regarding code for to convert and send data package. I search Internet but I couldnt find anything that can I understand. SO please anyone help me to do this. I see many projects that people do but those are difficult to understand. I am beginer to about this sending data.
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.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Tutorial7
{
public partial class Form1 : Form
{
Socket sock;
Socket acc;
public Form1()
{
InitializeComponent();
}
Socket socket()
{
return new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
}
private void button1_Click(object sender, EventArgs e)
{
List<NAudio.Wave.WaveInCapabilities> sources = new
List<NAudio.Wave.WaveInCapabilities>();
for (int i = 0; i < NAudio.Wave.WaveIn.DeviceCount; i++)
{
sources.Add(NAudio.Wave.WaveIn.GetCapabilities(i));
}
sourceList.Items.Clear();
foreach (var source in sources)
{
ListViewItem item = new ListViewItem(source.ProductName);
item.SubItems.Add(new ListViewItem.ListViewSubItem(item,
source.Channels.ToString()));
sourceList.Items.Add(item);
}
}
NAudio.Wave.WaveIn sourceStream = null;
NAudio.Wave.DirectSoundOut waveOut = null;
NAudio.Wave.WaveFileWriter waveWriter = null;
private void button2_Click(object sender, EventArgs e)
{
if (sourceList.SelectedItems.Count == 0) return;
int deviceNumber = sourceList.SelectedItems[0].Index;
sourceStream = new NAudio.Wave.WaveIn();
sourceStream.DeviceNumber = deviceNumber;
sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100,
NAudio.Wave.WaveIn.GetCapabilities(deviceNumber).Channels);
NAudio.Wave.WaveInProvider waveIn = new
NAudio.Wave.WaveInProvider(sourceStream);
}
}
}
I think you can use System.Net.Sockets.NetworkStream Class:
Have a look at:
https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.networkstream?redirectedfrom=MSDN&view=netframework-4.7.2
And this is a similar question: get stream of a socket object in c#
This is my form
https://i.stack.imgur.com/M7fuy.png
This is 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.Net.Sockets;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new
System.Net.Sockets.TcpClient();
//NetworkStream serverStream = default(NetworkStream);
//string readData = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
msg("Client Started");
clientSocket.Connect("192.168.0.167", 5000);
label1.Text = "Client Socket Program - Server Connected ...";
}
private void button1_Click(object sender, EventArgs e)
{
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
msg(returndata);
textBox2.Text = "";
textBox2.Focus();
}
public void msg(string mesg)
{
textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
}
}
}
when I click the start button in the code i am getting this error to connect to the server. please guide me how to get rid of this error and connect to the server instead.
https://i.stack.imgur.com/wDSCI.png
I am trying to set up two programs in C#. Basically, a simple server side set up where I want the client to listen for an image from the Server. Then, upon receiving the image, will display it in a PictureBox.
**I keep running into the following error:
A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll or Parameter is not Valid**
SERVER SIDE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ServerComputer
{
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
}
Socket sendsocket;
private void goLive_Click(object sender, EventArgs e)
{
try
{
sendsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//The instantiation of socket, IP for 192.168.1.106, 10001 for Port
IPEndPoint ipendpiont = new IPEndPoint(IPAddress.Parse(ipAddress.Text.Trim()), 10001);
sendsocket.Connect(ipendpiont);
//Establishment of end point
Thread th = new Thread(new ThreadStart(threadimage));
th.IsBackground = true;
th.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
this.Hide(); //Hidden form
}
private Bitmap GetScreen()
{
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
return bitmap;
}
private void threadimage()
{
try
{
while (true)
{
MemoryStream ms = new MemoryStream();
GetScreen().Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); //Here I use the BMP format
byte[] b = ms.ToArray();
sendsocket.Send(b);
Thread.Sleep(67); //I'm here to set to send a second
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
return;
}
}
}
}
CLIENT SIDE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ClientComputer
{
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
}
Socket hostSocket;
Thread thread;
string localIP = string.Empty;
string computrHostName = string.Empty;
private void mainForm_Load(object sender, EventArgs e)
{
computrHostName = Dns.GetHostName();
IPHostEntry hostname = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in hostname.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
}
}
}
private void liveScreen_Click(object sender, EventArgs e)
{
Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint hostIpEndPoint = new IPEndPoint(IPAddress.Parse(localIP), 10001);
//Connection node
receiveSocket.Bind(hostIpEndPoint);
receiveSocket.Listen(10);
MessageBox.Show("start");
hostSocket = receiveSocket.Accept();
thread = new Thread(trreadimage);
thread.Start();
thread.IsBackground = true;
}
private void trreadimage()
{
int dataSize, i = 0;
try
{
while (true)
{
i++;
byte[] b = new byte[1024 * 1024 * 20]; //Picture of great
dataSize = hostSocket.Receive(b,0,b.Length,SocketFlags.None);
MemoryStream ms = new MemoryStream(b,0,dataSize,true);
//bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Image img = Image.FromStream(ms);
img.Save("Image"+i+".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
videoBox.Image = img;
Console.WriteLine("Image Size: " + dataSize);
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
thread.Abort();
}
}
}
}
I can't stop repeating: never ever send raw data to a socket and expect to receive them the same way on another end. Use a protocol instead, to describe your network activity. In simplest case, send DWORD first that designated total length of your image. Same time, it's worth to reuse existing protocols like HTTP or what can fit your needs.
Why to bother? Well, if your "network" is just a piece of wire with two jacks on ends that placed directly into network cards of your PCs, that maybe happen to work OK (and still no 100% warranty). In real case, your "network connection" is a pack of inter-connections between routers, firewalls, switches and all that sort of hidden machinery in-between. They can (and will!) re-shape your stream the way they find useful for them. So, on the other end, how can your counterparty understand: is there everything received? how many packets to expect? etc. etc. etc.
So, sending some business data directly to socket is the same efficient as yelling to the window if you plan to say something to your friend living nearby. And, usually we use a phone with that kind of communication protocol "hey, Peter?.. yes, it's me.. listen, take care about your car as hail shower is coming... Bye!". See the difference?
Following code is working now, and I have few suggestions,
Try Async methods/events to receive and send data through socket instead of using loop or recursive methods.
Server Code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ServerComputer
{
publicpartialclassmainForm : Form
{
public mainForm()
{
InitializeComponent();
}
Socket sendsocket;
privatevoid goLive_Click(object sender, EventArgs e)
{
try
{
sendsocket = newSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//The instantiation of socket, IP for 192.168.1.106, 10001 for PortIPEndPoint ipendpiont = newIPEndPoint(IPAddress.Parse(ipAddress.Text.Trim()), 10001);
sendsocket.Connect(ipendpiont);
//Establishment of end pointThread th = newThread(newThreadStart(threadimage));
th.IsBackground = true;
th.Start();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
return;
}
this.Hide(); //Hidden form
}
privateBitmap GetScreen()
{
Bitmap bitmap = newBitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
return bitmap;
}
privatevoid threadimage()
{
try
{
MemoryStream ms = newMemoryStream();
GetScreen().Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); //Here I use the BMP formatbyte[] b = ms.ToArray();
sendsocket.Send(b);
ms.Close();
}
catch (Exception ee)
{
// MessageBox.Show(ee.Message);//return;
}
Thread.Sleep(1000);
threadimage();
}
}
}
Clint Code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ClientComputer
{
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
}
Socket hostSocket;
Thread thread;
string localIP = string.Empty;
string computrHostName = string.Empty;
private void mainForm_Load(object sender, EventArgs e)
{
computrHostName = Dns.GetHostName();
IPHostEntry hostname = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in hostname.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
}
}
this.Text = this.Text + " | " + localIP;
}
private void liveScreen_Click(object sender, EventArgs e)
{
connectSocket();
}
private void connectSocket()
{
Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint hostIpEndPoint = new IPEndPoint(IPAddress.Parse(localIP), 10001);
//Connection node
receiveSocket.Bind(hostIpEndPoint);
receiveSocket.Listen(10);
MessageBox.Show("start");
hostSocket = receiveSocket.Accept();
thread = new Thread(new ThreadStart(trreadimage));
thread.IsBackground = true;
thread.Start();
}
private void trreadimage()
{
int dataSize;
string imageName = "Image-" + System.DateTime.Now.Ticks + ".JPG";
try
{
dataSize = 0;
byte[] b = new byte[1024 * 10000]; //Picture of great
dataSize = hostSocket.Receive(b);
if (dataSize > 0)
{
MemoryStream ms = new MemoryStream(b);
Image img = Image.FromStream(ms);
img.Save(imageName, System.Drawing.Imaging.ImageFormat.Jpeg);
videoBox.Image = img;
ms.Close();
}
}
catch (Exception ee)
{
//MessageBox.Show(ee.Message);
//thread.Abort();
}
System.Threading.Thread.Sleep(1500);
trreadimage();
}
}
}
MemoryStream ms = new MemoryStream(b);
Image img = Image.FromStream(ms);
img.Save(imageName, System.Drawing.Imaging.ImageFormat.Jpeg);
videoBox.Image = img;
The error may due to corrupted or incomplete bmp image received in the MemoryStream it worked fine for me after increasing the socket send/receive buffers values
so adjust the sender "Socket.SendBufferSize" and the receiver "Socket.ReceiveBufferSize" to large values for example SendBufferSize = 1024 * 1024 * 20
this will help sending the entire image at once.
or another solution is to check whether the received data size (b.length) is the same as the sent image data size, before the code line of forming the received image stream
Sorry if this is hard to understand, trying out C# for the first time.
I am trying to make a simple public 'chat' between clients that are connected to the server. I've tried passing integers to the server and printing them out and everything was fine, however,when I switched to strings, it seems that it can only pass 1 character (because of ns.Write(converted, 0, 1);). If I increase the ns.Write to ns.Write(converted,0,10) everything crashes (both the client and the server) when I enter a message that is less than 10 characters.
Server code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
namespace MultiServeris
{
class Multiserveris
{
static void Main(string[] args)
{
TcpListener ServerSocket = new TcpListener(1000);
ServerSocket.Start();
Console.WriteLine("Server started");
while (true)
{
TcpClient clientSocket = ServerSocket.AcceptTcpClient();
handleClient client = new handleClient();
client.startClient(clientSocket);
}
}
}
public class handleClient
{
TcpClient clientSocket;
public void startClient(TcpClient inClientSocket)
{
this.clientSocket = inClientSocket;
Thread ctThread = new Thread(Chat);
ctThread.Start();
}
private void Chat()
{
byte[] buffer = new byte[10];
while (true)
{
NetworkStream ns = clientSocket.GetStream();
ns.Read(buffer,0,1);
string line = Encoding.UTF8.GetString(buffer);
Console.WriteLine(line);
}
}
}
}
Client code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace Klientas
{
class Klientas
{
static void Main(string[] args)
{
while (true)
{
TcpClient clientSocket = new TcpClient("localhost", 1000);
NetworkStream ns = clientSocket.GetStream();
byte[] buffer = new byte[10];
string str = Console.ReadLine();
byte[] converted = System.Text.Encoding.UTF8.GetBytes(str);
ns.Write(converted, 0, 1);
}
}
}
}
You're best using the BinaryReader/BinaryWriter classes to correctly format and read out data. This removes the need to process it yourself. For example in the client do:
BinaryWriter writer = new BinaryWriter(clientSocket.GetStream());
writer.Write(str);
And in the server:
BinaryReader reader = new BinaryReader(clientSocket.GetStream());
Console.WriteLine(reader.ReadString());
When using BinaryReader or BinaryWriter on the same stream, and you have .NET framework version 4.5 or above, be sure to leave the underlying stream open by using the overload:
using (var w = new BinaryWriter(stream, Encoding.UTF8, true)) {}
I'm implementing really simple UDP server in C# language.
When I implemented the server on Console based application, it worked well.
But when I implemented it by using the same code on Windows based application,
it doesn't works as before.
I guessed that it is the problem of thread thing.
So I putted thread on the code, but still doesn't works.
The function of "ReceiveFrom()" doesn't works as before.
numReceived = udpSocket.ReceiveFrom(buffer, ref remoteEP);
what is a problem that I'm missing?
Thank you in advance.
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.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace UDP_Server
{
public partial class Form1 : Form
{
private const int portNum = 5432;
Socket udpSocket;
byte[] buffer = new Byte[100];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ThreadStart threadStart = new ThreadStart(onServer);
Thread thread = new Thread(threadStart);
thread.Start();
}
private void onServer()
{
EndPoint localEP = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xxx"), portNum);
//EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSocket.Bind(localEP);
ThreadStart threadStart1 = new ThreadStart(OnReceive);
Thread thread1 = new Thread(threadStart1);
thread1.Start();
//udpSocket.BeginReceiveFrom(buffer, 0, buffer.Length,
//SocketFlags.None, ref remoteEP, new AsyncCallback(OnReceive), (object)this);
}
private void OnReceive()//IAsyncResult ar)
{
int numReceived = 0;
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
Console.WriteLine("UDP Starting Server");
Console.WriteLine("-----------------------------");
while (true)
{
numReceived = udpSocket.ReceiveFrom(buffer, ref remoteEP);
string s = Encoding.UTF8.GetString(buffer, 0, numReceived);
Console.WriteLine("Echo : {0}", s);
}
}
}
}
Looks as if your expecting it to print some output (which it is via the console window).You need to add all of the output you want to display in a windows forms control such as a rich text box and do:
RichTextbox1.Text += yourInput;