Click here to Skip to main content
15,042,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Sir,
Can you help me,please? Now, I am writing a Truck Scale program using C# , I can accept data from weight data from serial port(eg.COM1) , I can work well but when I close my C# form , I close my serial port. But When I reopen that form, I found error exception "Access to the port 'COM1' is denied." How can I solve that problem,please.
C#
DataTable dt = new DataTable();

dt = DataAccess.Class.SerialPort.GetDataBasePortSetting();

myport = new SerialPort();

myport.DataReceived += MyPortDataReceived;
myport.PortName = dt.Rows[0]["PortName"].ToString();
myport.BaudRate = Convert.ToInt32(dt.Rows[0]["BaudRate"].ToString());
myport.DataBits = Convert.ToInt32(dt.Rows[0]["DataBit"].ToString());
myport.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), (dt.Rows[0]["Parity"].ToString()));
myport.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), (dt.Rows[0]["StopBit"].ToString()));
myport.NewLine = "\r\n";
myport.Handshake = Handshake.None;
myport.ReadTimeout = 100;
myport.RtsEnable = true;
myport.DtrEnable = true;
//myport.ReceivedBytesThreshold = 100000;

if (myport.IsOpen)
{
    myport.Close();
}

if (!myport.IsOpen) //Error is here "Access to the port 'COM1' when reopen that form as a second time.
{                
    myport.Open();
    myport.DiscardInBuffer();
    System.Threading.Thread.Sleep(500); //Always sleep before reading. Just a good measure to ensure the device has written to the buffer.                
    myport.DiscardInBuffer(); //Discard stale data. I have use this code to be stable Data in txtGross.
}


Thanks
Thiha Swe
Myanmar
Posted
Updated 5-Feb-15 0:55am
v2

1 solution

As SerialPort implements IDisposable try wrapping your code in using to ensure that the resource is disposed of when you've finished with it.

E.g.
C#
var myport = new SerialPort();
using ( myport )
{
    myport.DataReceived += MyPortDataReceived;
    myport.PortName = dt.Rows[0]["PortName"].ToString();
    myport.BaudRate = Convert.ToInt32(dt.Rows[0]["BaudRate"].ToString());
    myport.DataBits = Convert.ToInt32(dt.Rows[0]["DataBit"].ToString());
    myport.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), (dt.Rows[0]["Parity"].ToString()));
    myport.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), (dt.Rows[0]["StopBit"].ToString()));
    myport.NewLine = "\r\n";
    myport.Handshake = Handshake.None;
    myport.ReadTimeout = 100;
    myport.RtsEnable = true;
    myport.DtrEnable = true;
    //myport.ReceivedBytesThreshold = 100000;

    if (!(myport.IsOpen)) //Error is here "Access to the port 'COM1' when reopen that form as a second time.
    {
        myport.Open();
        myport.DiscardInBuffer();
        System.Threading.Thread.Sleep(500); //Always sleep before reading. Just a good measure to ensure the device has written to the buffer.
        myport.DiscardInBuffer(); //Discard stale data. I have use this code to be stable Data in txtGross.
    }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900