I'm trying to plot the data read from a serial port using zedgraph. I'm still learing to code so I couldn't deduce why the plot does not work. Please have a look at the code and advice;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
string t;
SerialPort sp;
Thread m_thread;
bool m_running = false;
ManualResetEvent m_event = new ManualResetEvent(true);
bool m_pause = false;
private GraphPane myPane;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
// User can already search for ports when the constructor of the FORM1 is calling
// And let the user search ports again with a click
// Searching for ports function
SearchPorts();
CreateZedGraph(); //error : Severity Code Description Project File Line Suppression State
//Error CS7036 There is no argument given that corresponds to the required formal parameter
//'w' of 'Form1.DrawPoint(ZedGraphControl, int, PointPair)'
}
// start button
private void btnStart_Click(object sender, EventArgs e)
{
if (m_thread == null || m_thread.IsAlive == false)
{
ClearGraph();
m_thread = new Thread(Process);
m_thread.Start();
}
}
void Process()
{
PointPair point = new PointPair();
btnStart.Enabled = false;
btnStop.Enabled = true;
m_running = true;
while (m_running == true)
{
m_event.WaitOne();
point.Y = Convert.ToDouble(serialPort1);
point.X++; //time instance of measurement??
DrawPoint(zed1, point);
ssData.Value = point.Y.ToString();
RefresheZedGraphs(zed1);
Thread.Sleep(700);
}
btnStart.Enabled = true;
}
private void CreateZedGraph(object sender, SerialDataReceivedEventArgs e, ZedGraphControl zgc)
{
myPane = zgc.GraphPane;
// axes stuff
myPane.Title.Text = "FRDM-KW40z serial Test";
myPane.XAxis.Title.Text = "Time";
myPane.YAxis.Title.Text = "Voltage";
myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.XAxis.MinorGrid.IsVisible = true;
myPane.YAxis.MinorGrid.IsVisible = true;
// data from serial port
PointPairList list = new PointPairList();
zed1.GraphPane.AddCurve("Test", list, Color.Red);
}
To open and read from a serial port, I use a combox and a couple of buttons (and then later I try to save it to a text file);
private void button2_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
SearchPorts();
}
void SearchPorts()
{
string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
comboBox1.Items.Add(port);
}
}
private void button3_Click(object sender, EventArgs e)
{
// Catch exception if it will be thrown so the user will see it in a message box
OpenCloseSerial();
}
void OpenCloseSerial()
{
try
{
if (sp == null || sp.IsOpen == false)
{
t = comboBox1.Text.ToString();
sErial(t);
button3.Text = "Close Serial port"; // button text
}
else
{
sp.Close();
button3.Text = "Connect and wait for inputs"; // button text
}
}
catch (Exception err) // catching error message
{
MessageBox.Show(err.Message); // displaying error message
}
}
void sErial(string Port_name)
{
try
{
sp = new SerialPort(Port_name, 115200, Parity.None, 8, StopBits.One); // serial port parameters
sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
sp.Open();
}
catch (Exception err)
{
throw (new SystemException(err.Message));
}
}
private void DataReceivedHandler(object sender,SerialDataReceivedEventArgs e)
{
// This below line is not need , sp is global (belongs to the class!!)
//SerialPort sp = (SerialPort)sender;
if (e.EventType == SerialData.Chars)
{
if (sp.IsOpen)
{
string w = sp.ReadExisting();
if (w != String.Empty)
{
Invoke(new Action(() => Control.Update(w)));
}
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (sp == null || sp.IsOpen == false)
{
OpenCloseSerial();
}
}
The plot does not update! I can't quite guess why. Please tell me if there's mistakes in my approach or the code.
I get an error at : Invoke(new Action(() => Control.Update(w))); when trying to update the graph so that I can save after that.
I again have an error at: DrawPoint(zed1, point);
Thank you all for your time. Good day.
Cheers,
Ram.
To update the chart, you need to call the Invalidate method.
When you receive data from serial port, you need to convert it to double[] and add those points to your PointPairList.
PointPairList list = new PointPairList();
zed1.GraphPane.AddCurve("Test", list, Color.Red);
//Convert the received data to double array
double[] serialPortData = ....
//You may want to clear list first, by list.Clear();
list.Add(serialPortData);
//Invalidate the ZedGraphControl to update
zed1.Invalidate();
Related
I am creating a Windows Form application, where it is connecting to a device through bluetooth. I am able to send commands to the device and I am receiving the data continuously. The problem I am facing is that I am not able to show the continuous data in the text box. The text box only shows the first line of characters the application is receiving. Here is my code:
CONNECT BUTTON ACTION:
private void btnConnect_Click(object sender, EventArgs e)
{
if (listBox.SelectedItem != null)
{
lblProgress.Text = "";
btnStart.Enabled = true;
cBoxAvailablePorts.Enabled = cBoxAvailableBaudRates.Enabled = true;
try
{
int pos = listBox.SelectedIndex;
deviceInfo = array.ElementAt(pos);
if (pairDevice())
{
Thread thread = new Thread(() => connectThread());
thread.Start();
}
else
{
MessageBox.Show("Pair failed!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("Please connect to a device!");
}
}
THREAD ACTION
private void connectThread()
{
//BluetoothClient client = new BluetoothClient();
bc.BeginConnect(deviceInfo.DeviceAddress, serviceClass, this.connectCallBack, bc);
}
CALLBACK ACTION:
private void connectCallBack(IAsyncResult result)
{
//BluetoothClient client = (BluetoothClient)result.AsyncState;
try
{
if (bc.Connected)
{
MessageBox.Show("Connected!");
}
else
{
MessageBox.Show("Connection Failed!");
}
}
catch (Exception)
{
MessageBox.Show("Not able to identify Bluetooth devices! Please try again.!");
}
}
START BUTTON ACTION:
Here I send a command "S".
In button action I call sendMessage("S").
The function that is called is shown below:
public void sendMessage(string msg)
{
try
{
if (bc.Connected)
{
Stream stream = bc.GetStream();
stream.ReadTimeout = 1000;
StreamWriter streamWriter = new StreamWriter(stream);
streamWriter.WriteLine(msg);
streamWriter.Flush();
// Read operation
StreamReader streamReader = new StreamReader(stream);
string result = streamReader.ReadLine();
txtResult.Text = result;
}
else
{
MessageBox.Show("Sending failed!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
I wrote the StreamReader part in a loop, and it gave me Socket Exception.
I also tried to get the data from Serial Port and used DataReceived event just in case, but still it didn't help.
Any help would be appreciated.
Thank you!
OKAY! I solved the problem. Without getting in trouble with 32feet library (though it is fun to code with 32feet), I thought to make communication through serial port. I connected the device with my laptop and got to know the outgoing COMPORT in bluetooth setting of my laptop. The two-way communication can only be done through outgoing COMPORT, not the incoming COMPORT.
Suppose the outgoing COMPORT is COM12 and the baud rate that I have set is 9600.
So here is my code:
public delegate void updateDelegate(string text);
private updateDelegate objDelegate;
private SerialPort serialPort;
public View() // constructor
{
InitializeComponent();
this.WindowState = FormWindowState.Normal;
this.StartPosition = FormStartPosition.CenterScreen;
this.objDelegate = new updateDelegate(getText);
serialPort = new SerialPort("COM12", 9600);
serialPort.Handshake = Handshake.None;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.DtrEnable = true;
serialPort.RtsEnable = true;
}
START BUTTON ACTION
private void btnStart_Click(object sender, EventArgs e)
{
sendData("S");
}
// SEND COMMAND
public void sendData(string msg)
{
try
{
if (!serialPort.IsOpen)
{
serialPort.Open();
//serialPort.Close();
}
if (serialPort.IsOpen)
{
serialPort.Write(msg);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
// READ DATA
public void readData()
{
try
{
serialPort.DataReceived += SerialPort_DataReceived;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string res = serialPort.ReadExisting();
Thread.Sleep(500);
txtResult.Invoke(this.objDelegate, new object[] {res});
}
public void getText(string text)
{
txtResult.Text = text;
}
I hope this will help someone! Thank you!!!
Im sending audio from micro to PC using dma(LAN-TCP):
while (1) {
U32 max;
int r,i;
main_TcpNet ();
if(tcpSend & sendBuffer)
{
if(selectBuffer)
{
send_datalog(ADC_RegularConvertedValueTab2,sizeof(ADC_RegularConvertedValueTab2));
sendBuffer = 0;
}
else
{
send_datalog(ADC_RegularConvertedValueTab,sizeof(ADC_RegularConvertedValueTab));
sendBuffer = 0;
}
main_TcpNet ();
}
}
}
I need to play it in real time .this is what Ive done so far using NAudio:
byte[] recBuff = new byte[1400];
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
int iRx = theSockId.thisSocket.EndReceive(asyn);
recBuff [count]= theSockId.dataBuffer[0];
count++;
if (count >= 1400)
{
//--------------------------------------------------------------------
for (int i = 0; i < 1400; i += 2)
recieveSound[i / 2] = recBuff[i] + (recBuff[i + 1] * 256); //turn back to 16bit
//--------------------------------------------------------------------
foreach(int data in recieveSound)
sound.Add(data);
//----------------------------------
if (playStauts)
{
if (firstplay)
{
IWaveProvider provider = new RawSourceWaveStream(
new MemoryStream(recBuff), new WaveFormat());
_waveOut.Init(provider);
_waveOut.Play();
//playThread.Start();
//firstplay = false;
}
}
else
{
player.Stop();
}
count = 0; //RESET THE RecBuff
}
//---------------------------------------------------------------
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (m_clientSocket != null)
{
m_clientSocket.Close();
m_clientSocket = null;
}
Close();
}
private void frmMain_Load(object sender, EventArgs e)
{
playThread = new Thread(new ThreadStart(play));
player = new SoundPlayer(filePath);
toolStriplbIP.Text = "Your IP: " + GetIP();
btnDisconnect.Enabled = false;
}
#region Palying Sound
private void btnPlay_Click(object sender, EventArgs e)
{
try
{
//Array.Clear(sound, 0, sound.Count);
buffCount = 0;
offsetSound = 0;
sound.Clear();
Object objData = "7";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
if (m_clientSocket != null)
m_clientSocket.Send(byData);
playStauts = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPause_Click(object sender, EventArgs e)
{
playStauts = false;
}
#endregion
public void play()
{
while(true){
using (SoundPlayer player = new SoundPlayer(filePath))
{
//????????
}
}
}
I just hear like a bijilion Buzzes in sec. But When I save it and then play it I hear the song very clear and loud.
What is wrong? How can I play my byte array when its growing?
does this even work for me?
byte[] bytes = new byte[1400];
IWaveProvider provider = new RawSourceWaveStream(
new MemoryStream(bytes), new WaveFormat());
_waveOut.Init(provider);
_waveOut.Play();
For one thing, you're using the default WaveFormat, which may or may not be correct. Disagreement between source and destination formats will definitely cause you problems.
Once you're sure the WaveFormat is correct, I would suggest using a BufferedWaveProvider as the input to your wave player rather than the MemoryStream, something like this:
WaveFormat Format = new WaveFormat(/* fill in the right parameters here */);
BufferedWaveProvider Provider = new BufferedWaveProvider(Foramt);
Then, whenever you're happy with your recBuff, you just call Provider.AddSamples to drop the data into the BufferedWaveProvider, which will then be picked up by your WaveOut player.
There's some other strangeness going on. Are you receiving only one byte at a time? It looks like that's what your asynchronous handler is doing. This might not be the best thing, since that will result in lots and lots of context switching. If you're receiving more than one byte at a time, then you're only grabbing the first one and ignoring the rest. That will undoubtedly result in "unexpected" sounds during playback.
I have a program designed to read data in from the serial port, display it on a chart and allow saving. This data is simply uint64 numbers sent one by one. The data is sent very rapidly (many times per second).
Currently, the data is read into a datatable, this functionality is done in the datareceivedhandler event method which is triggered automatically when data is received.
I want to have the following:
A 'runonce' button that allows a single capture of data to be read
A selectable time and 'timed' button that will allow data to be read for some period of time only
A 'continuous' button that will allow data capture forever
Any data that is not within the selected times/captures should be discarded (or never captured in the first place). There should be a 'stop capture' button that will stop the continuous or timed modes.
What would the best method for implementing this? I've attached my code below.
using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Text.RegularExpressions;
namespace FieldProbe
{
public partial class MainForm : Form
{
SerialPort _port = new SerialPort(); //set up port
DataTable dt = new DataTable(); //set up datatable to hold output
int numSamples = 0;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
var ports = SerialPort.GetPortNames(); //get list of ports connected to PC
cmbSerialPorts.DataSource = ports; //use list of ports as combobox options
_port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); //create pointer to datareceived event handler
dt.Clear(); //Clear datatable on start
dt.Columns.Add("index", typeof(int)); //index
dt.Columns.Add("dutyCycle", typeof(float)); //0.5 means equal on/off, 1.0 means permanently on
dt.Columns.Add("frequency", typeof(float)); //probe can go from 40MHz to 10GHz
dt.Columns.Add("distance", typeof(float)); //antenna to radiator distance in metres (usually 1m)
dt.Columns.Add("pulseWidth", typeof(float)); //calculated from duty cycle and frequency
dt.Columns.Add("level", typeof(int)); //output in V/m
chChart.Series["series1"].YValueMembers = "level";
chChart.Series["series1"].XValueMember = "index";
chChart.DataSource = dt;
chChart.DataBind();
}
private void btnConnect_Click(object sender, EventArgs e)
{
if (btnConnect.Text == "Connect") //if user clicks connect button
{
if (cmbSerialPorts.SelectedIndex > -1) //if port valid
{
//MessageBox.Show(String.Format("You selected port '{0}'", cmbSerialPorts.SelectedItem));
Connect(cmbSerialPorts.SelectedItem.ToString()); //go to connect function
}
else
{
MessageBox.Show("Please select a port first");
}
}
else //if user clicks disconnect button
{
btnConnect.Text = "Connect";
btnContinuous.Enabled = false;
btnSingle.Enabled = false;
btnTimed.Enabled = false;
nudTime.Enabled = false;
try
{
_port.Close();
}
catch
{
MessageBox.Show("Connection close failed- device may already be unplugged. Program will exit.");
Application.Exit();
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "csv files (*.csv)|*.csv*";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.DefaultExt = "csv";
saveFileDialog1.AddExtension = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, dtToCSV());
}
}
private void btnSingle_Click(object sender, EventArgs e)
{
}
private void Connect(string portName)
{
if (!_port.IsOpen)
{
_port.BaudRate = 9600; //CHANGE ME AFTER ARDUINO TESTING!!!!!!!!!!!!!!
_port.DataBits = 8;
_port.StopBits = StopBits.One;
_port.Parity = Parity.None;
_port.Handshake = Handshake.None;
_port.RtsEnable = true; //CHANGE ME AFTER ARDUINO TESTING!!!!!!!!!!!!!!
_port.DtrEnable = true; //CHANGE ME AFTER ARDUINO TESTING!!!!!!!!!!!!!!
_port.PortName = portName;
try
{
_port.Open(); //connect to port
btnConnect.Text = "Disconnect";
btnContinuous.Enabled = true;
btnSingle.Enabled = true;
btnTimed.Enabled = true;
nudTime.Enabled = true;
}
catch
{
MessageBox.Show("ERROR: Port invalid or in use!");
btnConnect.Text = "Connect";
btnContinuous.Enabled = false;
btnSingle.Enabled = false;
btnTimed.Enabled = false;
nudTime.Enabled = false;
}
}
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender; //set up serial port
string indata = sp.ReadExisting(); //read data from port
string indataString = Regex.Match(indata, #"\d+").Value; //extract number (still a string)
numSamples++; //increment x axis
try
{
int indataInt = Int32.Parse(indataString); //convert string to int
var pulseWidth = nudDutyCycle.Value / (nudFrequency.Value * 100); //formula to find pulse width from duy cycle & frequency
object[] o = { numSamples, nudDutyCycle.Value, nudFrequency.Value, nudDistance.Value, pulseWidth, indataInt }; //collect data
dt.Rows.Add(o); //add data to datatable
teIncomingData.invokeIfRequired(() => //add text to textbox (different thread)
{
teIncomingData.AppendText(indataString + Environment.NewLine);
});
}
catch { }
chChart.invokeIfRequired(() => //update chart (different thread)
{
chChart.DataBind();
});
}
private string dtToCSV()
{
string file = "";
foreach (DataColumn col in dt.Columns)
file = string.Concat(file, col.ColumnName, ",");
file = file.Remove(file.LastIndexOf(','), 1);
file = string.Concat(file, "\r\n");
foreach (DataRow row in dt.Rows)
{
foreach (object item in row.ItemArray)
file = string.Concat(file, item.ToString(), ",");
file = file.Remove(file.LastIndexOf(','), 1);
file = string.Concat(file, "\r\n");
}
return file;
}
public void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{ //do not delete me, this needs to exist for some reason for saving to work
}
}
public static class Invoker
{
public static void invokeIfRequired(this ISynchronizeInvoke obj, MethodInvoker action)
{
if(obj.InvokeRequired)
{
var args = new object[0];
obj.Invoke(action, args);
}
else
{
action();
}
}
}
}
I am trying to build a simply app that returns the number calling via a modem, however I only seem to be getting the first line of the data received from the modem.
When I run HyperTerminal and pass through the AT#CID=1 command, ring the number, I get a full output of :
OK
DATE=0314
TIME=1111
NMBR=4936
NAME=Stuart E
RING
In my app i only seem to receive the first section containing the "OK" part. Any help on what i am doing wrong or am missing?
Code:
public partial class Form1 : Form
{
public SerialPort port = new SerialPort("COM3", 115200,Parity.None,8,StopBits.One);
public String sReadData = "";
public String sNumberRead = "";
public String sData = "AT#CID=1";
public Form1()
{
InitializeComponent();
}
private void btnRun_Click(object sender, EventArgs e)
{
SetModem();
ReadModem();
MessageBox.Show(sReadData);
}
public void SetModem()
{
if (port.IsOpen == false)
{
port.Open();
}
port.WriteLine(sData + System.Environment.NewLine);
port.BaudRate = iBaudRate;
port.DtrEnable = true;
port.RtsEnable = true;
}
public string ReadModem()
{
try
{
sReadData = port.ReadExisting().ToString();
return (sReadData);
}
catch (Exception ex)
{
String errorMessage;
errorMessage = "Error in Reading: ";
errorMessage = String.Concat(errorMessage, ex.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, ex.Source);
MessageBox.Show(errorMessage, "Error");
return "";
}
}
private void btnExit_Click(object sender, EventArgs e)
{
port.Close();
Close();
}
}
}
In ReadModem() try to use port.ReadLine() in a loop instead and loop until you get a line saying RING (if that is the final line you are expecting).
You are just reading the Modem once after setting it. You need to subscribe the DataReceivedEvent on serialPort to continuously get data from the port.
public void SetModem()
{
if (port.IsOpen == false)
{
port.Open();
}
port.WriteLine(sData + System.Environment.NewLine);
port.BaudRate = iBaudRate;
port.DtrEnable = true;
port.RtsEnable = true;
port.DataReceived += port_DataReceived;
}
void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//For e.g. display your incoming data in RichTextBox
richTextBox1.Text += this.serialPort1.ReadLine();
//OR
ReadModem();
}
I have made a simple windows form with a ComboBox, TextBox and two Buttons to setup a serial protocol with my hardware.
However, whenever I send something I do get reply from hardware but C# doesn't display it. Instead it gives an exception saying that the operation has timed out. I even used an oscilloscope to check if I received something and it was positive. But C# doesn't display the code as stated before.
I am attaching my code below. Anyhelp would be welcome. Thanks in advance.
public partial class Form3 : Form
{
string buffer;
public SerialPort myComPort = new SerialPort();
delegate void setTextCallback(string text);
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PnPEntity");
foreach (ManagementObject queryObj in searcher.Get())
{
if (queryObj["Caption"].ToString().Contains("(COM"))
{
comboBox1.Items.Add(queryObj["Caption"]);
}
}
comboBox1.Text = comboBox1.Items[0].ToString();
}
catch (ManagementException ex)
{
MessageBox.Show(ex.Message);
}
}
private void setText(string text)
{
if (textBox1.InvokeRequired)
{
setTextCallback tcb = new setTextCallback(setText);
this.Invoke(tcb, new object[] { text });
}
else
{
textBox1.Text = text;
}
}
void myComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string myString = myComPort.ReadLine();
setText(myString);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button1_Click(object sender, EventArgs e)
{
myComPort.Close();
// button1.Enabled = false;
string name = comboBox1.Text;
string[] words = name.Split('(', ')');
myComPort.PortName = words[1];
myComPort.ReadTimeout = 5000;
// myComPort.WriteTimeout = 500;
myComPort.BaudRate = 9600;
myComPort.DataBits = 8;
myComPort.StopBits = StopBits.One;
myComPort.Parity = Parity.None;
myComPort.DataReceived += new SerialDataReceivedEventHandler(myComPort_DataReceived);
myComPort.Open();
}
private void button2_Click(object sender, EventArgs e)
{
myComPort.WriteLine("?GV1\r");
}
}
It say
...The DataReceived event is not guaranteed to be raised for every byte received...
Try something like:
private static void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// prevent error with closed port to appears
if (!_port.IsOpen)
return;
// read data
if (_port.BytesToRead >= 1)
{
// ...
// read data into a buffer _port.ReadByte()
DataReceived(sender, e);
}
// ...
// if buffer contains data, process them
}
Have a look at this url:
http://csharp.simpleserial.com/
And this url for WMI:
http://www.codeproject.com/Articles/32330/A-Useful-WMI-Tool-How-To-Find-USB-to-Serial-Adapto