I'm trying to open each port and send <mccon> serially, for which my microcontroller will respond <connected>\n after which the C# code must exit the for each loop.
I'm having a problem at the serialPort.PortName = str; line. After two iterations, it does not continue further.
I tried doing this manually too. I made a drop down and selected ports one by one. After the second port, it does not allow to change the serial Port. But in case I select within two tries, it works fine.
I know OOP in C++. But I'm new to C#. I'm not sure why the loop fails.
public Form1()
{
InitializeComponent();
send_button.Enabled = false;
//Availabe COM ports
SerialPort tmp;
foreach(string str in SerialPort.GetPortNames())
{
tmp = new SerialPort(str);
if (tmp.IsOpen == false)
{
serialPort.PortName = str;
try
{
//Open serial port
serialPort.Open();
serialPort.BaudRate = 9600;
serialPort.WriteTimeout = 10;
serialPort.ReadTimeout = 10;
serialPort.Write("<mccon>");
readtxt.Text = serialPort.ReadTo("\n");
if (readtxt.Text == "<connected>")
{
send_button.Enabled = true;
port_combobox.Enabled = false;
break;
}
else
{
serialPort.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
I don't have multiple serial ports, but when I compiled and executed your code, I noticed that you are not closing the serial port if it errors during the read. I suggest you modify your code as follows:
SerialPort tmp;
foreach (string str in SerialPort.GetPortNames())
{
tmp = new SerialPort(str);
if (tmp.IsOpen == false)
{
serialPort.PortName = str;
try
{
//open serial port
serialPort.Open();
serialPort.BaudRate = 9600;
serialPort.WriteTimeout = 10;
serialPort.ReadTimeout = 10;
serialPort.Write("<mccon>");
String s = serialPort.ReadTo("\n");
if (s == "<connected>")
{
break;
}
else
{
serialPort.Close();
}
}
catch (TimeoutException)
{
serialPort.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
I'm not sure the effect on changing the port name while it's open, but it could well cause the issues you are seeing.
Could you execute this code and return what it shows? It might show some information about the Arduino port which you can then use for the serialport.
Add a reference to System.Management and also add the using, and then try the code:
using System.Management;
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PnPEntity");
foreach (ManagementObject queryObj in searcher.Get())
{
if (queryObj["Caption"].ToString().ToUpper().Contains("ARDUINO"))
{
Console.WriteLine(queryObj["Caption"]);
foreach (PropertyData pd in queryObj.Properties) { Console.WriteLine(pd.Name + " : " + pd.Value); }
}
}
}
catch (ManagementException e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
Related
My app uses USB based serial ports to connect to physical hardware devices. I can open any valid USB port and communicate with the external devices. However, when I close the connection, the USB port is left in some sort of indeterminate state for some time, and during that time further attempts to reconnect result in the "Access to port "COM--" is denied" error. However, after some few seconds, attempting to reconnect is successful. How can I determine WHEN the USB port will again support a new connection?
The code looks like this:
private void Setup(string Port)
{
bool ValidPort = false;
int CloseSleep = 10;
_PortName = Port;
_PortType = this;
string[] AvailablePorts = SerialPort.GetPortNames();
foreach(string aPort in AvailablePorts)
{
if (aPort == _PortName)
{
// The required port is listed in the list of available ports)
ValidPort = true;
break;
}
}
if (ValidPort)
{
try
{
if (_ThePort != null)
{
_ThePort.Close();
_ThePort.DataReceived -= ReceivedDataEventHandler;
while(CloseSleep-- > 0)
System.Threading.Thread.Sleep(100);
_ThePort.Dispose();
_ThePort = null;
}
}
catch (Exception ex)
{
EMS_Config_Tool.ModalDialog md = new EMS_Config_Tool.ModalDialog("Closing Port: " + ex.Message, "System Exception");
md.ShowDialog();
}
System.IO.Ports.SerialPort TheNewPort = new System.IO.Ports.SerialPort(Port, 38400);
// Setup the event handlers from Tx and Rx
Handler.DataOutEvent += CommsSender;
TheNewPort.DataReceived += ReceivedDataEventHandler;
TheNewPort.DataBits = 8;
TheNewPort.Parity = Parity.None;
TheNewPort.Handshake = System.IO.Ports.Handshake.None;
TheNewPort.StopBits = System.IO.Ports.StopBits.One;
// We will try 3 times to open the port, and report an error if we fail to open the port
try
{
TheNewPort.Open();
}
catch (Exception)
{
System.Threading.Thread.Sleep(1000);
try
{
TheNewPort.Open();
}
catch (Exception)
{
System.Threading.Thread.Sleep(1000);
try
{
TheNewPort.Open();
}
catch (Exception ex)
{
EMS_Config_Tool.ModalDialog md = new EMS_Config_Tool.ModalDialog("Opening Port: " + ex.Message, "System Exception");
return;
}
}
}
The final catch statement is where the error about Access being denied is issued. Note my attempt to retry opening the port 3 times doesn't really help. If I leave the port alone for about 5 to 10 seconds and retry calling the Setup method it succeeds immediately.
As #Neil said, there are many issues. The best thing to do, in my point of view, is to put the search in a loop, and as soon as the port can be opened, it will be.
I used to do like this :
public Task WaitingPort()
{
while (port is null)
{
port = CheckPort();
}
}
private SerialPort CheckPort()
{
string[] listPort = SerialPort.GetPortNames();
foreach(string namePort in listPort)
{
SerialPort port = new SerialPort(namePort, 9600);
if (!port.IsOpen)
{
try
{
port.Open();
port.ReadTimeout = 1500;
string data = port.Readline();
// I programmed my device to send an "A" until it receives
// "777" to be able to recognize it once opened
if (data.Substring(0, 1) == "A")
{
port.ReadTimeout = 200;
port.Write("777"); // to make it stop sending "A"
return port;
}
else
{
port.Close();
}
}
catch (Exception e1)
{
port.Close();
}
}
}
return null;
}
Of course, this is just some kind of a template which you have to reshape to your use
I have amended my code to use a constrained loop to give it a better chance to work, which it usually does. I was hoping that there was a better way to do it, as I tend to have pretty impatient users who will be posting defect reports if they have to wait 5 or 10 seconds to make a connection....
// We will try several times to open the port, upto 10 times over 5 seconds, and report an error if we finally fail to open the port
try
{
TheNewPort.Open();
}
catch (Exception ex)
{
RetryOpenTimer.Interval = 500;
RetryCount = 10;
RetryOpenTimer.Elapsed += new System.Timers.ElapsedEventHandler(RetryOpenTimer_Elapsed);
WaitForOpen = true;
RetryOpenTimer.Start();
while (WaitForOpen && RetryCount > 0)
{
System.Threading.Thread.Sleep(500);
}
if (WaitForOpen)
{
EMS_Config_Tool.ModalDialog md = new EMS_Config_Tool.ModalDialog("Opening Port: " + ex.Message, "System Exception");
return;
}
}
...
void RetryOpenTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
RetryOpenTimer.Stop();
RetryOpenTimer.Elapsed -= RetryOpenTimer_Elapsed;
try
{
if (RetryCount-- > 0)
{
TheNewPort.Open();
WaitForOpen = false;
}
else
return;
}
catch (Exception)
{
RetryOpenTimer.Start();
RetryOpenTimer.Elapsed += RetryOpenTimer_Elapsed;
}
}
I have to use Bulgarian language to send SMS and I use thic project that works fine if you need to send English SMS (https://www.codeproject.com/Articles/38705/Send-and-Read-SMS-through-a-GSM-Modem-using-AT-Com).
So I open Srrial port like
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; //COM1
port.BaudRate = p_uBaudRate; //9600
port.DataBits = p_uDataBits; //8
port.StopBits = StopBits.One; //1
port.Parity = Parity.None; //None
port.ReadTimeout = p_uReadTimeout; //300
port.WriteTimeout = p_uWriteTimeout; //300
port.Encoding = Encoding.GetEncoding("windows-1251");
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
port.DtrEnable = true;
port.RtsEnable = true;
}
catch (Exception ex)
{
throw ex;
}
return port;
}
I send SMS text that comes from MS SQL Server (nvarchar) like
public bool sendMsg(SerialPort port, string PhoneNo, string Message)
{
bool isSend = false;
try
{
string recievedData = ExecCommand(port,"AT", 300, "No phone connected");
recievedData = ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format.");
String command = "AT+CMGS=\"" + PhoneNo + "\"";
recievedData = ExecCommand(port,command, 300, "Failed to accept phoneNo");
command = Message + char.ConvertFromUtf32(26) + "\r";
recievedData = ExecCommand(port,command, 3000, "Failed to send message"); //3 seconds
if (recievedData.EndsWith("\r\nOK\r\n"))
{
isSend = true;
}
else if (recievedData.Contains("ERROR"))
{
isSend = false;
}
return isSend;
}
catch (Exception ex)
{
throw ex;
}
}
//Execute AT Command
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("No success message was received.");
return input;
}
catch (Exception ex)
{
throw ex;
}
}
But I cannot see normal text it looks like abrakadabra with ?????? and so on.
Please help me encoding text properly so I get redable SMS.
Thank you!
------- Extra Code to clarify the issue -----------------------------------------
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("Response received is incomplete.");
else
throw new ApplicationException("No data received from phone.");
}
}
while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
}
catch (Exception ex)
{
throw ex;
}
return buffer;
}
public AutoResetEvent receiveNow;
//Receive data from port
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (e.EventType == SerialData.Chars)
{
receiveNow.Set();
}
}
catch (Exception ex)
{
throw ex;
}
}
I found best answer posible to implement ASAP.
https://github.com/welly87/GSMComm
GsmCommMain comm=new GsmCommMain(/*Set your option here*/);
string txtMessage="your long message...";
string txtDestinationNumbers="your destination number";
//select unicode option by a checkBox or any other control
bool unicode = chkUnicode.Checked;
SmsSubmitPdu[] pdu = SmartMessageFactory.CreateConcatTextMessage(txtMessage, unicode, txtDestinationNumbers);
сomm.SendMessages(pdu);
How to concat long SMS in GSMComm Library?
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!!!
I am developing a desktop application which interacts with Arduino via SerialPort.
I want my application to respond every time arduino writes something on the serial port.
But DataReceivedHandler does not get triggered for all the events.
Here is the connection code -
public static bool connectToArduino()
{
foreach (string name in SerialPort.GetPortNames())
{
SerialPort serialPort = new SerialPort(name);
Console.WriteLine(name);
if (serialPort.IsOpen == false)
{
serialPort.PortName = name;
try
{
Console.WriteLine("Openning serial port.");
serialPort.WriteTimeout = 5000;
serialPort.ReadTimeout = 5000;
serialPort.BaudRate = 115200;
serialPort.Open();
serialPort.Write(Constants.CONNECT_APP_STRING);
Console.WriteLine("Written to serial port.");
string reply = serialPort.ReadLine();
//string reply = serialPort.ReadTo("\n");
Console.WriteLine("Reply is: " + reply);
Console.WriteLine("Read from serial port.");
if (reply == Constants.CONNECT_ACK)
{
Console.WriteLine(name);
Console.WriteLine("Connected with arduino controller");
//serialPort.DataReceived += DataReceivedHandler;
serialController = serialPort;
return true;
}
else if (reply+"\n" == Constants.CONNECT_ARDUINO_STRING) {
serialPort.WriteLine(Constants.CONNECT_ACK);
serialController = serialPort;
MessageBox.Show("Connected with arduino controller");
return true;
}
else
{
serialPort.Close();
}
}
catch (TimeoutException)
{
Console.WriteLine("Timeout occured.");
serialPort.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR");
Console.WriteLine(ex);
}
}
}
MessageBox.Show("Connection with Arduino failed!");
return false;
}
After this I set the data received handler -
SerialComm.serialController.DataReceived += DataReceivedHandler;
Now, the problem is that sometimes DataReceivedHandler is not being triggered.
I am not able to find a pattern in this. It randomly just skips some events.
Any idea what is going wrong?
Looking at the documentation you can find in the remarks:
The DataReceived event is not guaranteed to be raised for every byte received. Use the BytesToRead property to determine how much data is left to be read in the buffer.
If the event is in your case too unreliable to be trusted catch every incoming information from your device, then you could use an extra thread which would run in the background constantly checking the BytesToRead property and/or constantly reading with ReadExisting which is non-blocking.
I wrote a message last week but would like to add another more simple question again to clarify.
I have a serial port console application. There are 20 virtual ports. My application successfully working but doesnt change received data value until I restart the application. I am not sure why it happens.
This issue appeared after FIFO enabled property was set to true.
How can i refresh serial port?
Thanks
--- here is the codes :
static void serialportinstances()
{
for (int ix =1; ix < 20; ix++)
{
if (sportlar.ContainsValue("COM"+ix.ToString()) ==false)
{
try
{
SerialPort ekle = new SerialPort("COM" + ix.ToString(), 1200, Parity.None, 8, StopBits.One);
//ekle.DtrEnable = true;
//ekle.RtsEnable = true;
try
{
ekle.NewLine = "\r";
ekle.Open();
ekle.DataReceived += new SerialDataReceivedEventHandler(datareceived);
Console.WriteLine(String.Format("Dinamik SP {0} başarıyla açıldı", ekle.PortName));
}
catch (Exception eu)
{
Console.WriteLine(String.Format("Dynamic SP {0} açılamadı!", ekle.PortName));
}
sportlar.Add(ekle, "COM" + ix.ToString());
}
catch (Exception ee)
{
Console.WriteLine("port could not be created");
}
}
}
}
and datareceiving handler ;
static void datareceived(object sender, SerialDataReceivedEventArgs e)
{
lock (mylockobject)
{
SerialPort spn = (SerialPort)sender;
try
{
string s = spn.ReadLine();
GetWeightPWI(spn.PortName, s);
Thread.Sleep(1);
}
catch (Exception ee)
{
Console.WriteLine("error : " + ee.ToString());
}
}
}
and parsing the received data here :
private static void GetWeightPWI(string portadi, string alinan)
{
Console.WriteLine("PORT NAME : "+portadi+" and received data : "+alinan.toString());
}