I have a small issue with a program I am writing in Visual Studio 2022.
I have numerous devices that attach to the USB virtual com port, and because of the way that Windows enumerates the USB to serial chip in the device, I can have com ports listed as well over 100 (576 at last count). These com ports are not connected simultaneously, but one at a time, each enumerating as one higher than the previous port.
My problem is that I can access up to COM99, but when it rolls over to 100, the port will not connect. There is no error message or exception thrown.
The ComboBox will find port 100 (or greater) but the serial port will not connect. I am wondering if the System.IO.Ports Serial function is truncating my port string to "10" for instance.
I should mention that this code works flawlessly up to port 99 (on many different computers). I can see port 100 or greater in the ComboBox, but no success. This should be a simple thing but I'm baffled.
Any help or suggestions gratefully received.
I am using this code for com port connections:
private bool ScanSerial()
{
bool success = false;
//Open the serial port and get the number of the active serial port
serialPortComboBox.Items.Clear();
string[] serialPortNumbers = SerialPort.GetPortNames();
// Iterate each port and add it to the serial port drop down box
foreach (string port in serialPortNumbers)
{
if (!String.IsNullOrEmpty(port))
{
serialPortComboBox.Items.Add(port);
success = true;
}
else
{
MessageBox.Show("No available serial ports", "Serial Ports", MessageBoxButtons.OK);
serialPortInUse = null;
return false;
}
}
// default the highest numbered port as the port to use - the program will try to connect to this port
// if it doesn't find a device, user will have to override the method by selecting from the dropdown and trying again
string lastPort = serialPortNumbers.Max();
serialPortComboBox.Text = lastPort;
label10.Text = lastPort;
serialPortInUse = lastPort;
return success;
}
public bool Connect(string comPort)
{
bool success;
// Baud rate 57600, 8, N, 1 is standard
{
// Allow the user to set the appropriate properties.
_serialPort.PortName = comPort;
_serialPort.BaudRate = 57600;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.Handshake = Handshake.None;
_serialPort.Encoding = Encoding.GetEncoding(0);
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
}
try
{
_serialPort.Open();
success = true;
_continue = true;
}
catch (Exception ex)
{
Console.WriteLine("An exception was thrown: \n {0}", ex.ToString());
Console.WriteLine("Press enter to continue.");
Console.ReadLine();
success = false;
}
return success;
}
You can find GetPortNames() source code on github, it iterates over a registry key
https://github.com/dotnet/runtime/blob/b056b7b6a1eca5171e42f697a0b3d3c60d1fc048/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs
There several methods to get the ports. Checkout this post.
How do I get a list of available serial ports in Win32?
Since I guess you are using some kind of vendor specific serialport virtual adapter it make sense to search the registry for a high port e.g COM255 to find a more reliable registry way. I would guess it is an USB PNP device.
Maybe somewhere in HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum
Related
I will not be able to access the hardware I'm programming with sometimes or it's just not good to debug.
So that's why I thought to make my life a bit easier and work with a virtual serial port.
I chose to use com0com since it's pretty straight forward to set up and it's free.
My problem is that my UWP app does find the port but can't connect to it.
The code I'm using is:
public async Task<string> Init()
{
try
{
if (_serialPort == null)
{
var aqs = SerialDevice.GetDeviceSelector("COM7");
var devices = await DeviceInformation.FindAllAsync(aqs, null);
if (devices.Any())
{
await OpenPort(devices[0].Id);
return "found port";
}
}
else
{
return "port already configured";
}
return "whatever";
}
catch (Exception ex)
{
return ex.Message;
}
}
private async Task OpenPort(string deviceId)
{
try
{
_serialPort = await SerialDevice.FromIdAsync(deviceId);
if (_serialPort != null)
{
_serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
_serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
_serialPort.BaudRate = 19200;
_serialPort.Parity = SerialParity.None;
_serialPort.StopBits = SerialStopBitCount.One;
_serialPort.DataBits = 8;
_serialPort.Handshake = SerialHandshake.None;
}
}
catch (Exception ex)
{
throw ex;
}
}
I know the code itself is working because I use it with the hardwarelike this. The only thing I changed is that I directly search for the COM7 port.
WHen I debug my code I can see that the port is found and loaded into "device[0]"
but it is not loaded into "devices" when i run the FromIdAsync method.
Did I do something wrong or does UWP not work with virtual ports?
My problem is that my UWP app does find the port but can't connect to it.
Currently, UWP does not support virtual serial port that with out VID and PID Parameter.
For UWP, it has very limited access to serial port the file. It defines specific DeviceId scheme within AppxManifestType.xsd. When you invoke SerialDevice.FromIdAsync() method, it will match with the following scheme, if your device id does not match. the method will not return SerialDevice. So, UWP does not support visual serial port currently.
<xs:simpleType name="ST_DeviceId">
<xs:restriction base="ST_NonEmptyString">
<xs:pattern value="any"/>
<xs:pattern value="vidpid:[0-9a-fA-F]{4} [0-9a-fA-F]{4}( (usb|bluetooth))?"/>
<xs:pattern value="model:[^;]{1,512};.{1,512}"/>
</xs:restriction>
</xs:simpleType>
There are a fewer limits to access serial port in Win32 Application , you could connect to visual serial port directly.
may be this is not directly related to the problem as you asked for the virtual ports, it give a little enlighten on the serial port problem in UWP. and as #Nico Zhu said that the UWP has limited access to Serial Port.
SerialCommunication
System-internal or on-chassis serial ports may be enumerated by DeviceInformation.FindAllAsync(), but cannot be opened by SerialDevice.FromIdAsync() because they currently are not supported. However, serial ports connected over USB, such as on USB-to-Serial cables are supported.
It is possible that the DeviceInformation collection returned by DeviceInformation.FindAllAsync() may have a serial device whose DeviceInformation.Name property is set to the machine name. This is by design and may occur when enumerating an on-board serial port
I am developing a C# Windows Forms Application to communicate via a Bluetooth Connection with a Raspberry Pi Model 3. This connection is mimicked through a virtual serial port on the client machine. I am able to start a bluetooth connection within the C# program but I cannot receive any data from the program. When I use the program Putty, I can see that the data is transmitting on COM Port the way that I would like it to.
I also discovered a weird sort of glitch with the C# program. If i have the COM Port open on Putty and then start the C# program, an error will occur with the port.open() command since the port is reserved. So then if I close Putty and continue the C# application the data will stream perfectly to the program. Has anyone encountered this issue before? I've been at a bit of a loss for a few days now. The Code in my program is shown below:
using System;
using System.IO.Ports;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using InTheHand.Net;
using InTheHand.Net.Sockets;
using InTheHand.Net.Bluetooth;
using System.Threading;
namespace GUIfromPI
{
static class Program
{
//PC BT USB adapter
private static BluetoothEndPoint EP = new BluetoothEndPoint(BluetoothAddress.Parse("##:##:##:##:##:##"), BluetoothService.BluetoothBase); //addressing the usb adapter used on the PC (endpoint)
private static BluetoothClient BC = new BluetoothClient(EP);
//Pi BT Adapter
private static BluetoothDeviceInfo BTDevice = new BluetoothDeviceInfo(BluetoothAddress.Parse("##:##:##:##:##:##")); //addressing the BT adapter on the Rasperry Pi
// private static NetworkStream stream = null;
public static SerialPort mySerialPort = new SerialPort(); //Bluetooth module mimics serial protocol by streaming data through the COM5 port in the host80 computer
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main(string[] args)
{
Console.WriteLine("Executing Program...");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (BluetoothSecurity.PairRequest(BTDevice.DeviceAddress, "1234"))//MY_PAIRING_CODE))
{
Console.WriteLine("PairRequest: OK");
if (BTDevice.Authenticated)
{
Console.WriteLine("Authenticated: OK");
BC.SetPin("1234");//pairing code
//BC.BeginConnect(BTDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), BTDevice);
}
else
{
Console.WriteLine("Authenticated:No");
}
}
else
{
Console.WriteLine("PairRequest: No");
}
//mySerialPort = new SerialPort("COM5");
SerialThreadFunction();
}
public static void SerialThreadFunction()
{
mySerialPort.PortName = "COM10";
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DtrEnable = true;
mySerialPort.RtsEnable = true;
mySerialPort.ReadTimeout = 100000;
mySerialPort.Open();
//mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
string mydata = "hello";
while (true)
{
Console.WriteLine(mySerialPort.ReadLine());
mydata = mySerialPort.ReadLine();
}
mySerialPort.Close();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received: ");
Console.Write(indata);
}
}
}
UPDATE: I just discovered that declaring my Bluetooth Endpoint, client, and device are interfering with reading off of my serial port. Since the bluetooth connection was already initialized previously, I was able to see the data on the port. Now for why it does this?
Okay, it seems you are not doing anything wrong. Inherently .NET cannot handle multiple ownership of the same port. When you declare your SerialPort instance and connect to say... COM11... You have given ownership of COM11 solely to your SerialPort instance. In order to have access to COM11 you will now need to provide a reference to that specific SerialPort object which has ownership of COM11.
In your case you are opening PuTTY and then running your program. Once PuTTY obtains access to the port, your program will not be able to do so. This is completely standard in the .NET framework. Now, there are other ways that you can get multiple accesses to a COM port, but I think that's outside the scope of this question. Here's a software that will allow you to run an application and sniff traffic over the port at the same time... Free Serial Port Monitor. You can get this for free, and there is a better version for purchase that does all kinds of magic.
Here is a little algorithm for ensuring your port is opened properly, you may want to take this... modify it a little bit... and use it as the Connect method on your BluetoothClient class.
SerialPort port = null;
string error = string.Empty;
bool success = false;
int tries = 5;
foreach(var name in System.IO.Ports.SerialPort.GetPortNames())
{
// try each port until you find an open one
port.Name = name;
// there is always a chance that the port is open
// if trying some operations back-to-back
// give it a few extra tries if necessary
for (int i = tries; i > 0; --i)
{
try
{
// avoid the exception by testing if open first
if (!port.IsOpen)
{
port.Open();
success = true;
return;
}
}
catch (UnauthorizedAccessException e)
{
// sometimes the exception happens anyway, especially
// if you have multiple threads/processes banging on the
// ports
error += e.Message;
}
}
}
In addition to all of this, you may want to watch that your Bluetooth classes are not claiming ownership of the port when you need to read it. That may be what's interfering with reading the port. You really should create one single class and call it say BluetoothClient or something, and have that single class be responsible for all the interactions with the SerialPort reference. This way you ensure that whether you want to send/receive on the port you will always have ownership.
I have fiddled around with the Kinect SDK and arduino, and I have looked around the web to find a way to pass information from the Kinect to the arduino. I'm only trying to pass some very simple information (i.e. if the kinect recognizes a hand gesture, the arduino do this) and I have found people talking about using the serial port to do this. I looked at the documentations and people's code, but I'm very new to C# and don't really understand how the serial port works. I have the Kinect for Windows v1 and am trying to pass information to a botboarduino. If someone can explain how to set up the serial ports that would be greatly appreciated!
PS. I have been working in Visual Studio 2015 community for the Kinect and in the Arduino IDE for the arduino.
A bit obvious, I know, but start by using SerialPort component from System.IO.Ports namespace.
The examples section of this: https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx MSDN page offers all the functions you require to get the serial port up, and parse in data. You'll of course need your Serial port open, set to the correct parity on the Arduino end to receive the desired data you want to send and process it - but there are more than enough examples of that.
The examples provided by Mike should give a general layout, but as for setting up the serial port in your program you are most likely going to abide by 9600 8N1.
Here is an example of the initial serial port settings for 9600 8N1 (These are for C++ using Boost libraries, but it should make it clear what you will need to set):
port.set_option(asio::serial_port_base::baud_rate(9600));
port.set_option(asio::serial_port_base::character_size(8));
port.set_option(asio::serial_port_base::flow_control(asio::serial_port_base::flow_control::none));
port.set_option(asio::serial_port_base::parity(asio::serial_port_base::parity::none));
port.set_option(asio::serial_port_base::stop_bits(asio::serial_port_base::stop_bits::one));
When I set this up in the past the only thing I had to ensure was the baud rate on the Arduino matched that of the above settings, otherwise the rest seemed to be default settings. I can't guarantee this in your case though.
You need to write to serial the Arduino is connected to. Windows regards a serial as a file, so to write to the serial, use this class (I use VS2013, but it should work for VS2015 as well):
Header (named SerialClass.h):
#ifndef SERIALCLASS_H_INCLUDED
#define SERIALCLASS_H_INCLUDED
#define ARDUINO_WAIT_TIME 2000
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
class Serial
{
private:
//Serial comm handler
HANDLE hSerial;
//Connection status
bool connected;
//Get various information about the connection
COMSTAT status;
//Keep track of last error
DWORD errors;
public:
//Initialize Serial communication with the given COM port
Serial(char *portName);
//Close the connection
~Serial();
//Read data in a buffer, if nbChar is greater than the
//maximum number of bytes available, it will return only the
//bytes available. The function return -1 when nothing could
//be read, the number of bytes actually read.
int ReadData(char *buffer, unsigned int nbChar);
//Writes data from a buffer through the Serial connection
//return true on success.
bool WriteData(char *buffer, unsigned int nbChar);
//Check if we are actually connected
bool IsConnected();
};
#endif // SERIALCLASS_H_INCLUDED
CPP file (named SerialClass.cpp):
#include "SerialClass.h"
#include<iostream>
Serial::Serial(char *portName)
{
//We're not yet connected
this->connected = false;
//Try to connect to the given port throuh CreateFile
this->hSerial = CreateFile(portName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
//Check if the connection was successfull
if (this->hSerial == INVALID_HANDLE_VALUE)
{
//If not success full display an Error
if (GetLastError() == ERROR_FILE_NOT_FOUND){
//Print Error if neccessary
printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
}
else if (GetLastError() == ERROR_ACCESS_DENIED)
{
printf("It is this!!!!");
}
else
{
printf("ERROR!!!");
DWORD error = GetLastError();
std::cout << error;
}
}
else
{
//If connected we try to set the comm parameters
DCB dcbSerialParams = { 0 };
//Try to get the current
if (!GetCommState(this->hSerial, &dcbSerialParams))
{
//If impossible, show an error
printf("failed to get current serial parameters!");
}
else
{
//Define serial connection parameters for the arduino board
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
//Setting the DTR to Control_Enable ensures that the Arduino is properly
//reset upon establishing a connection
dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
//Set the parameters and check for their proper application
if (!SetCommState(hSerial, &dcbSerialParams))
{
printf("ALERT: Could not set Serial Port parameters");
}
else
{
//If everything went fine we're connected
this->connected = true;
//Flush any remaining characters in the buffers
PurgeComm(this->hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
//We wait 2s as the arduino board will be reseting
Sleep(ARDUINO_WAIT_TIME);
}
}
}
}
Serial::~Serial()
{
//Check if we are connected before trying to disconnect
if (this->connected)
{
//We're no longer connected
this->connected = false;
//Close the serial handler
CloseHandle(this->hSerial);
}
}
int Serial::ReadData(char *buffer, unsigned int nbChar)
{
//Number of bytes we'll have read
DWORD bytesRead;
//Number of bytes we'll really ask to read
unsigned int toRead;
//Use the ClearCommError function to get status info on the Serial port
ClearCommError(this->hSerial, &this->errors, &this->status);
//Check if there is something to read
if (this->status.cbInQue>0)
{
//If there is we check if there is enough data to read the required number
//of characters, if not we'll read only the available characters to prevent
//locking of the application.
if (this->status.cbInQue>nbChar)
{
toRead = nbChar;
}
else
{
toRead = this->status.cbInQue;
}
//Try to read the require number of chars, and return the number of read bytes on success
if (ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0)
{
return bytesRead;
}
}
//If nothing has been read, or that an error was detected return -1
return -1;
}
bool Serial::WriteData(char *buffer, unsigned int nbChar)
{
DWORD bytesSend;
//Try to write the buffer on the Serial port
if (!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
{
//In case it don't work get comm error and return false
ClearCommError(this->hSerial, &this->errors, &this->status);
return false;
}
else
return true;
}
bool Serial::IsConnected()
{
//Simply return the connection status
return this->connected;
}
Example of usage (writing text to the serial):
Serial Arduino = Serial("COM3"); //My Arduino is at COM3 - change it for yours!
if (Arduino.IsConnected() == false) //Checks if it connected
{
std::cout << "Your Arduino isn't connected!" << std::endl;
std::cin.ignore();
return 1;
}
Arduino.WriteData("This is sent data"); //Sends "This is sent data" to the Serial which Arduino is connected to!
The Arduino should read data from the Serial (using the Serial class).
Note that your Arduino will have to be connected to the laptop via USB (not Wifi) for this to work . . .
Source: I did a project just like this!
If you want to know how to use 32feet.NET library to communicate with bluetooth devices, read the solution
I am currently trying to communicate via bluetooth between a computer and a self-built .NET Gadgeteer prototype.
The Gadgeteer prototype consists of the mainboard, a power supply and a bluetooth module. The module is in discoverable mode.
On the computer a custom bluetooth program based on 32feet .NET Bluetooth is running. The program detects all bluetooth devices in range and tries to pair with them. However, this is not done automatically at the moment, I have to enter a pairing code for the device.
How can I pair devices without entering the pairing code?
Devices are found, the problem is the pairing part. I experimented a lot, but didn't find a solution...
foreach (BluetoothDeviceInfo device in this.deviceList)
{
try
{
//BluetoothClient client = new BluetoothClient(this.CreateNewEndpoint(localAddress));
//BluetoothEndPoint ep = this.CreateNewEndpoint(device.DeviceAddress);
EventHandler<BluetoothWin32AuthenticationEventArgs> handler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(HandleRequests);
BluetoothWin32Authentication auth = new BluetoothWin32Authentication(handler);
BluetoothSecurity.PairRequest(device.DeviceAddress, null);
}
}
This code block initiates the pairing and it works, but Windows is asking me to enter the pairing code for the device. I read about the BluetoothWin32Authentication to prevent this case but I don't get it right.
private void HandleRequests(object that, BluetoothWin32AuthenticationEventArgs e)
{
e.Confirm = true;
}
This is the code of the event handler (http://32feet.codeplex.com/wikipage?title=BluetoothWin32Authentication)
If you simply want to allow the pairing to go ahead when to SSP devices are connecting then handling the callback and setting e.Confirm=True will be enough -- but that is a little insecure...
I am confused -.- The goal is that the application and the gadgeteer module can send data in both directions without any user interference.
Is it true that I can't pair devices automatically without user interaction?
Is it true that if two device were already paired they can exchange data without user interaction?
I figured out how to solve my problems and my knowledge about Bluetooth connections is a bit bigger now. If someone else has problems with that, I provide my solution. The code examples represent the C# implementation of a bluetooth controller with the 32feet Bluetooth library.
Scanning
This means that devices in range are detected. My code:
// mac is mac address of local bluetooth device
BluetoothEndPoint localEndpoint = new BluetoothEndPoint(mac, BluetoothService.SerialPort);
// client is used to manage connections
BluetoothClient localClient = new BluetoothClient(localEndpoint);
// component is used to manage device discovery
BluetoothComponent localComponent = new BluetoothComponent(localClient);
// async methods, can be done synchronously too
localComponent.DiscoverDevicesAsync(255, true, true, true, true, null);
localComponent.DiscoverDevicesProgress += new EventHandler<DiscoverDevicesEventArgs>(component_DiscoverDevicesProgress);
localComponent.DiscoverDevicesComplete += new EventHandler<DiscoverDevicesEventArgs>(component_DiscoverDevicesComplete);
private void component_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
{
// log and save all found devices
for (int i = 0; i < e.Devices.Length; i++)
{
if (e.Devices[i].Remembered)
{
Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is known");
}
else
{
Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is unknown");
}
this.deviceList.Add(e.Devices[i]);
}
}
private void component_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
{
// log some stuff
}
Pairing
This means that devices get coupled with the local bluetooth device. This needs to be done once by entering a code of both sides. Can be done via code so that the user doesn't even notice that a device was added. My code for this purpose:
// get a list of all paired devices
BluetoothDeviceInfo[] paired = localClient.DiscoverDevices(255, false, true, false, false);
// check every discovered device if it is already paired
foreach (BluetoothDeviceInfo device in this.deviceList)
{
bool isPaired = false;
for (int i = 0; i < paired.Length; i++)
{
if (device.Equals(paired[i]))
{
isPaired = true;
break;
}
}
// if the device is not paired, pair it!
if (!isPaired)
{
// replace DEVICE_PIN here, synchronous method, but fast
isPaired = BluetoothSecurity.PairRequest(device.DeviceAddress, DEVICE_PIN);
if (isPaired)
{
// now it is paired
}
else
{
// pairing failed
}
}
}
Connecting
This means establishing a connection and exchanging of data. Again some code:
// check if device is paired
if (device.Authenticated)
{
// set pin of device to connect with
localClient.SetPin(DEVICE_PIN);
// async connection method
localClient.BeginConnect(device.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), device);
}
// callback
private void Connect(IAsyncResult result)
{
if (result.IsCompleted)
{
// client is connected now :)
}
}
If you keep the order scan, pair, connect, everything should work fine. To send or receive data, use the GetStream() method of the BluetoothClient. It provides a network stream that can be manipulated.
Receiving a connection
If you want another device to connect with your device you need to listen to incoming connection requests. This only works if the device have already been paired before. My code:
BluetoothListener l = new BluetoothListener(LOCAL_MAC, BluetoothService.SerialPort);
l.Start(10);
l.BeginAcceptBluetoothClient(new AsyncCallback(AcceptConnection), l);
void AcceptConnection(IAsyncResult result){
if (result.IsCompleted){
BluetoothClient remoteDevice = ((BluetoothListener)result.AsyncState).EndAcceptBluetoothClient(result);
}
}
Replace LOCAL_MAC with a valid BluetoothAddress (e.g. by using BluetoothAddress.Parse();). After the devices are connected they can exchange messages via the underlying stream. If the connection does not work there might be authentication issues, so try setting the local device pin in the listener (l.SetPin(LOCAL_MAC, MY_PASSWORD);
I have an application that reads from a COM Port and then does something with the data that it receives. I am currently using a COM Port emulator (Since i don't have the device available for me to use) but I am feeding it a sample of the data. The program seems to work perfectly fine if I open the COMPort before I start transmitting data. However, if I start transmitting before I open the COMPort, and then I open the port, the dataReceived event is never fired and I am never able to get any data. I have even tried to flush the INBuffer as soon as I open the port but am unable to read from it.
My code to open the port is this:
public void setupComPort(string baudRate, string dataBits, string stopBits, string parity, string portName)
{
if (comPort.IsOpen)
comPort.Close();
comPort.BaudRate = int.Parse(baudRate);
comPort.DataBits = int.Parse(dataBits);
comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits);
comPort.Parity = (Parity)Enum.Parse(typeof(Parity), parity);
comPort.PortName = portName;
// When data is recieved through the port, call this method
comPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
try
{
// Open the port
comPort.Open();
//If there's data in buffer, discard so we can start receiving
//comPort.DiscardInBuffer();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error Opening Port", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
Any help would be appreciated.
Its probably an issue with the emulator. I'll guess the issue will go away when you have the actual hardware. The only other thing I can think to try is setting the ReceivedBytesThreshold to something other than the default (like 10 or something).