c# Serialport problem: data are send in "clumps" - c#

I am trying to remote control a lego mindstorms NXT robot using a bluetooth serial connection. The program connects without any problem but when i send single commands they do not appear before several other commands has been send. Then they all appear at once on the nxt.
I have tried everything (i can think of or google has told me to) but i cannot seam to get the buffer to flush after a command is send.
Anyone having a idea about what i can do?
Here is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace NXTBtRemote
{
public class BTHandler
{
SerialPort comm;
private string comPort;
public BTHandler()
{
comm = new SerialPort();
}
public Boolean Connect(string _comPort)
{
this.comPort = _comPort;
comm.PortName = comPort;
comm.Open();
if (comm.IsOpen) return true;
else return false;
}
public void sendCommand(Command command)
{
string msg = command.cmdType + "#" + command.arguments;
if (!comm.IsOpen) comm.Open();
comm.WriteLine(msg);
comm.DiscardOutBuffer();
}
}
}
Hope somone can help. Thanks in advance
kind regards - kenneth

comm.DiscardOutBuffer();
That's very bad. You are throwing away the bytes you have just written. The WriteLine() method writes the command to the output buffer from which they are slowly written to the serial port. Only if you debug the code, single stepping through the code, would the serial port driver have enough of a chance to actually send something. It would be hit or miss if the chip itself has a FIFO buffer. Just delete the DiscardOutBuffer() call, it does nothing but harm.
Beyond that, you are really complaining about a problem with receiving a response. But didn't show any code that makes any Read call.

Related

sniff serial port - .net

I have this Arduino code just for testing purpose:
int num=0;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(num);
num+=1;
delay(800);
}
Now it prints an integer and increments its value by one. When I open up Serial Monitor it prints as expected every 800ms.
My Arduino is connected on PORT-6
Now if I try to access the port, it says it is in use, I'm trying to access that from a .NET application. How can I do so?
c# code, collected from the internet, modified a little:
using System;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static SerialPort _serialPort;
public static void Main()
{
_serialPort = new SerialPort();
_serialPort.PortName = "COM6";//Set your board COM
_serialPort.BaudRate = 9600;
_serialPort.Open();
while (true)
{
string a = _serialPort.ReadExisting();
Console.WriteLine(a);
}
}
}
}
How can I sniff data from that serial port ? [Educational Purpose]
You can't open a serial port twice.
If what you want is to get to see what is going through the bus (sniffing), you can use virtual ports and port forwarding, see here for a complete example.
Nothing will stop you from replacing any of the tools discussed in the link with your own code (.NET or other), in case they don't suit your needs or if you have enough determination to reinvent the wheel.

What happens if I use VCP on a FTDI (FT2232H) setted in UART mode? [COM not well talking]

I'm working on a C# program that is basically a char sequence's listener in a SerialPortwhich is connected to a FT2232H.
The sequence should start with an arbitrary char and has an arbitrary lenght.
The problem is not that I don't have the correct pattern I expected, the problem is that I don't have the chars I passed into the chip at all, and sometimes they're mixed with other bytes I'm sure to not have passed to the chip!
Even coding this char by char reader didn't help me to understand what happens inside.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Windows.Forms;
namespace FTDI_VCP
{
class SerialPortProgram
{
int _test_counter = 0;
private string _buffer;
private SerialPort port = new SerialPort("COM11",
9600, Parity.Even, 8, StopBits.Two);
[STAThread]
static void Main(string[] args)
{
new SerialPortProgram();
}
private SerialPortProgram()
{
Console.WriteLine("incoming chars:");
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
Application.Run();
}
private void port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
if(port.BytesToRead != 0)
{
do {
int byte2read = port.ReadChar();
if(byte2read == 0xFF) //this is my arbitrary char value
{
Console.WriteLine();
}
else
{
Console.Write((char)port.ReadChar());
}
} while (true);
}
}
}
This is the output:
Suppositions:
I think that using a VCP through a FTDI in UART-RS232 mode I am listening directly on the UART buffer however I fear that there are remote possibilities that I'm reading a something else instead of simple chars. Maybe RS232 packets? Maybe another pattern? I don't know. Can anyone help me to understand what happens inside a UART buffer when I'm using a COM port?
Things I've already checked that seems to be okay
Baud rate mismatching
Other devices interferring
FTDI in right mode, I used FT_Prog like this
Thanks for the attention payed. I'm waiting for a FTDI GURU to help me (:

Reading Info sent by arduino to Serial Port with C#

I am trying to read simple sensor reading from a arduino. The arduino is connected to COM3 (used for sending data and programming the arduino). The C# Programm is very simple and tries to read what the arduino sends.
Problem: I can not open COM3 Port with C# or the arduino when the other side (C# or arduino respectively) already opened it. Just sending without opening doesnt produce any results aswell. How are you supposed to "connect" them? My understanding was that both devices open the port with the same baudrate and then you can send and read data. When I am trying to open, I will get a UnauthorizedAccess on the C# side or a "can't open serial" on the arduino side.
Arduino C-Code:
#include <DHT.h>
#define DHTPIN A4
#define DHTTYPE DHT11
#define THERPIN A0
DHT dht(DHTPIN,DHTTYPE);
String hum="Humidity:";
String temptext="Temp:";
String semi=";";
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(A0,INPUT);
}
void loop() {
float humidity = dht.readHumidity();
delay(300);
float temp = dht.readTemperature();
delay(300);
if (isnan(humidity)||isnan(temp))
{
Serial.println("Fehler beim Lesen(NAN)");
delay (5000);
}else
{
Serial.print(temp + semi);
Serial.print(humidity);
Serial.flush();
delay(1000);
}
}
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Ports;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
SerialPort serialPort1;
serialPort1 = new SerialPort();
serialPort1.PortName = "COM3";
serialPort1.BaudRate = 9600;
serialPort1.DtrEnable = true;
REPEAT:
if (serialPort1.IsOpen)
{
string reading = serialPort1.ReadLine();
Console.WriteLine(reading);
serialPort1.Close();
}
else
{
Console.WriteLine("closed,opening");
serialPort1.Open();
goto REPEAT;
}
}
}
}
While searching the solution was always that another programm was already using the COM Port, but isn't that exactly what I need to communicate? Obviously, the arduino has to use the same COM-Port as my C# app, as far as I understand.
Thanks
Your code is opening and closing the serial port perpetually. This does not work, because when .NET code closes the connection Windows internally will close the port asynchronously. It can take a few seconds before the port is actually closed. This is why the program almost immediately blocks.
Open the connection only once at the start of your program.
Besides: avoid GOTO statements at any cost. Edgar Dijkstra wrote a paper against its use many years ago: Go To Statement Considered Harmful.

TCP Send multiple message without closing connection

Hi guys i have build an app for android to get the GPS coordination and i want to send the data to my C# UWP server through TCP. As concept i have opened a socket and i want to send multiple messages without closing the socket.
socket = new java.net.Socket("192.168.2.10", 9999);
printwriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
printwriter.println("message1");
printwriter.println("message2");
printwriter.println("message3");
printwriter.println("message4");
printwriter.flush();
The problem is i only receive message1 or sometimes also messages2 on the server. The other message doesn't show on the server. I don't want to make new connection because i'm planning sending a lot of messages. If any of you know a solution would be appreciated.
I'm currently using the server code for UWP in C# from https://msdn.microsoft.com/en-us/windows/uwp/networking/sockets.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Maps
{
class Connection
{
public async void Connectie()
{
try
{
System.Diagnostics.Debug.WriteLine("Waiting for connection................");
//Create a StreamSocketListener to start listening for TCP connections.
Windows.Networking.Sockets.StreamSocketListener socketListener = new Windows.Networking.Sockets.StreamSocketListener();
//Hook up an event handler to call when connections are received.
socketListener.ConnectionReceived += SocketListener_ConnectionReceived;
//Start listening for incoming TCP connections on the specified port. You can specify any port that' s not currently in use.
await socketListener.BindServiceNameAsync("9999");
System.Diagnostics.Debug.WriteLine("Waiting for connection................");
}
catch (Exception e)
{
//Handle exception.
}
}
private async void SocketListener_ConnectionReceived(Windows.Networking.Sockets.StreamSocketListener sender,
Windows.Networking.Sockets.StreamSocketListenerConnectionReceivedEventArgs args)
{
Stream inStream = args.Socket.InputStream.AsStreamForRead();
StreamReader reader = new StreamReader(inStream);
reader = new StreamReader(args.Socket.InputStream.AsStreamForRead());
System.Diagnostics.Debug.WriteLine("connection................");
//Read line from the remote client.
string request = await reader.ReadLineAsync();
System.Diagnostics.Debug.WriteLine(request);
}
}
}
In addition to what #Hasson said about reading all the lines, you should make sure that you're properly disposing the stream (preferably with a using on the StreamReader, and you could do the same on Stream, although the StreamReader dispose would close it also). It looks like your code is hoping to get the whole message in one call though, so if you don't want to use a while loop that checks !reader.EndOfStream before each ReadLineAsync, you could do something like this:
using (Stream inStream = args.Socket.InputStream.AsStreamForRead())
using (StreamReader reader = new StreamReader(inStream))
{
System.Diagnostics.Debug.WriteLine("connection................");
//Read line from the remote client.
string request = await reader.ReadToEndAsync();
System.Diagnostics.Debug.WriteLine(request);
}
I'm also a little worried about the usage of this class. If the code that instantiates a new Connection() doesn't keep a reference to it available, the garbage collector will gobble it all up and your server won't be listening anymore.
For example, if you're having a button click or a page load doing something like this:
Connection conn = new Connection();
conn.Connectie();
Where there isn't some reference to conn maintained, then after that function is done being called, the garbage collector will automatically dispose it and your StreamSocketListener.
Your server code is incorrect, you have to keep reading in the SocketListener_ConnectionReceived until there is nothing to read, but here you are reading just one line, think about adding a while loop. there are lots of examples for this kind of loop, for example here.

WPF C# application will freeze my whole computer ever 2-3 times I run it

I put a lot of information in this issue because I have no idea what will be relavent
Issue:
I am having an issue with a program I am working on where when running it, it will freeze my whole computer and return no error (I am completely incapable of doing anything CTRL+ALT+DEL doesn't even work). This program accepts a connection from a android client and atm the android client is not configured correctly so the connection is being rejected.
Question:
How can I stop my program from freezing my entire machine?
Conjecture:
I have a few theories as to what is going on but no idea how to fix them. I have read that this may have something to do with me running a single threaded process inside my async worker but I am not sure that the socket is a single threaded process. Also I am not entirely sure how I am supposed to deal with exceptions in a backgroundworker so I just let it fall back to the RunWorkerCompletedEventArgs then retrieve the error message from there.
What I have tried:
- I tried putting try catches every where then removing try catches nothing seems to be able to capture this error
- I checked my systems event log and nothing is showing up except my restarts after my computer freezes
- I have attempted to isolate the issue but it can literally happen at any point from the program starting till when I attempt to connect
Setup:
I am running the program out of visual studio 2012 professional on a windows 8 pro machine. The computer I am on has a i7-3770K 3.50GHz and 32GB of ram. The application that is attempting to make a connection to mine is a Android application and the credentials are incorrect when it is attempting to connect. Visual Studio is running off my main hard drive and building the project on another drive.
Closing:
With all that said would some one please be willing to help me? If you need any more information I will be happy to provide it, please ask.
Main Method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace Server
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class SourceServer : Window
{
private BackgroundWorker worker = new BackgroundWorker();
public SourceServer()
{
InitializeComponent();
StartListeningForConnections();
}
private void StartListeningForConnections()
{
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.WorkerReportsProgress = true;
if (worker.IsBusy != true)
{
worker.RunWorkerAsync();
}
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
worker.ReportProgress(0, "Source server version 0.0.0.1ib started");
LoginServer oLoginServer = new LoginServer();
oLoginServer.StartListening(worker);
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
try
{
lvOutput.Items.Add(e.UserState.ToString());
}
catch (Exception exception)
{
lvOutput.Items.Add(exception.StackTrace);
}
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
System.IO.File.WriteAllText(Environment.CurrentDirectory + #"\log.txt", e.Error.StackTrace + " /n " + e.Error.Message);
}
else
{
MessageBox.Show("Error was null");
}
worker.Dispose();
}
}
}
SSL Socket Connection:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using MySql.Data.MySqlClient;
using System.IO;
namespace Server
{
public class LoginServer
{
// Incoming data from the client.
public static string data = null;
public static X509Certificate serverCertificate = null;
public delegate void UpdateListView(ListView oOutput);
public void StartListening(BackgroundWorker worker)
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[1];
serverCertificate = X509Certificate.CreateFromCertFile(#"server.crt");
TcpListener oServer = new TcpListener(ipAddress, 12345);
// Bind the socket to the local endpoint and
// listen for incoming connections.
// Start listening for connections.
while (true)
{
Thread.Sleep(100);
worker.ReportProgress(0, "Waiting for connection....");
// Program is suspended while waiting for an incoming connection.
//Socket handler = listener.Accept();
oServer.Start();
TcpClient oClient = oServer.AcceptTcpClient();
Stream oStream = oClient.GetStream();
SslStream oSSLStream = new SslStream(oStream);
data = null;
// An incoming connection needs to be processed.
string sUsername = "place holder";
string sPassword = "place holder";
while (true)
{
bytes = new byte[1024];
int bytesRec = oSSLStream.Read(bytes, 0, bytes.Length);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
string[] sCredentials = data.Split("|".ToCharArray()[0]);
sUsername = sCredentials[0];
sPassword = sCredentials[1];
if (data.IndexOf("<EOF>") > -1)
{
break;
}
}
// Show the data on the console.
worker.ReportProgress(0, "Connection Recieved : ");
worker.ReportProgress(0, "Username: " + sUsername);
worker.ReportProgress(0, "Password: " + sPassword);
worker.ReportProgress(0, "");
// Echo the data back to the client.
byte[] msg;
if (sUsername.Equals("test") && sPassword.Equals("test"))
{
msg = Encoding.ASCII.GetBytes("approved<EOF>\n");
worker.ReportProgress(0, "approved");
oSSLStream.Write(msg, 0, msg.Length);
}
else
{
msg = Encoding.ASCII.GetBytes("rejected<EOF>\n");
worker.ReportProgress(0, "rejected");
oSSLStream.Write(msg, 0, msg.Length);
}
}
}
public void VerifyUser()
{
}
}
}
While I don't see any reason for this to lock up your entire computer, I do see a couple of reasons for the application to potentially hang...
Your while loop inside of your SSL server will never break unless your client writes '<EOF>' to the stream; which you would have to force it to do. I would likely do something similar to this:
while(( bytesRec = oSSLStream.Read(bytes,0,bytes.Length)) > 0 )
{
// Compare input & break
}
-- The while loop you have now ( without a thread sleep ) will consume all of your systems resources waiting for ... something that may never occur.
In a related issue - I note that your 'DoWork' method launches the listener - but does not start a new thread for this listener. This means that the listener is running inside of your interface thread - which will cause the interface ( and potentially more... ) to hang until the process is completed - which as stated, may never happen.
... Ahem... This last paragraph may be incorrect - you are running an async worker, so I may be incorrect in my second assessment.
Cheers, hope this is helpful.
I've had some hanging problems on Windows 8 that I never saw on Windows 7 (with VS2012). As you experienced it worked fine the first time but only locked up Visual Studio (and not my whole machine) and I had to force quit.
The Visual Studio 2012 Update 4 (which focuses on bug fixes and compatibility) seemed to fix it, although I didn't scientifically test this.
Note: As of 9/1/13 this is only the RC2 version so please check for newer versions, and edit this answer when RTM happens.

Categories