Thursday, January 15, 2015

Connecting to USB dongle in VB.net

This can be used if you need to communicate with a USB dongle

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Text.RegularExpressions;

namespace SMSapplication
{
public class clsSMS
{


public SerialPort OpenPort(string p_strPortName, int p_uBaudRate, int p_uDataBits, int p_uReadTimeout, int p_uWriteTimeout)
{
receiveNow = new AutoResetEvent(false);
SerialPort port = new SerialPort();

try
{        
port.PortName = p_strPortName;              
port.BaudRate = p_uBaudRate;            
port.DataBits = p_uDataBits;            
port.StopBits = StopBits.One;          
port.Parity = Parity.None;            
port.ReadTimeout = p_uReadTimeout;    
port.WriteTimeout = p_uWriteTimeout;
port.Encoding = Encoding.GetEncoding("iso-8859-1");
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
port.DtrEnable = true;
port.RtsEnable = true;
}
catch (Exception ex)
{
throw ex;
}
return port;
}


public void ClosePort(SerialPort port)
{
try
{
port.Close();
port.DataReceived -= new SerialDataReceivedEventHandler(port_DataReceived);
port = null;
}
catch (Exception ex)
{
throw ex;
}
}


public string ExecCommand(SerialPort port,string command, int responseTimeout, string errorMessage)
{
try
{

port.DiscardOutBuffer();
port.DiscardInBuffer();
receiveNow.Reset();
port.Write(command + "\r");

string input = ReadResponse(port, responseTimeout);
if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n"))))
throw new ApplicationException("Success message not found.");
return input;
}
catch (Exception ex)
{
throw ex;
}
}


public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (e.EventType == SerialData.Chars)
{
receiveNow.Set();
}
}
catch (Exception ex)
{
throw ex;
}
}
public string ReadResponse(SerialPort port,int timeout)
{
string buffer = string.Empty;
try
{  
do
{
if (receiveNow.WaitOne(timeout, false))
{
string t = port.ReadExisting();
buffer += t;
}
else
{
if (buffer.Length > 0)
throw new ApplicationException("Incomplete response");
else
throw new ApplicationException("zero data");
}
}
while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
}
catch (Exception ex)
{
throw ex;
}
return buffer;
}
}
}

No comments:

Post a Comment