c# - "System.UnauthorizedAccessException" error when opening second serial port -
i need open second serialport in visual c# program read data arduino. worked fine, in case see below not work..
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.io.ports; using commandspd4i; namespace csharpexample { public partial class csharpexample : form { public commotorcommands motor1; public csharpexample() { initializecomponent(); motor1 = new commotorcommands(); motor1.setsteps(convert.toint32(numericschritte.value)); } serialport arduino; delegate void invokelb(string data); invokelb lbrecieveddelegate; int xpos = 0; private void startbtn_click(object sender, eventargs e) { // set comm settings motor 1 motor1.selectedport = comportbox1.text; motor1.baudrate = convert.toint32(baudratebox1.text); // set motor address motor1.motoraddresse = convert.toint32(motor1id.value); // set relative positioning mode motor1.setpositiontype(1); // start travel profile if (motor1.errorflag) { statuslabel1.text = "status 1: " + motor1.errormessagestring; } else { statuslabel1.text = "status 1: ok"; } } private void stopbtn_click(object sender, eventargs e) { // stop travel profile motor1.stoptravelprofile(); } private void timer1_tick(object sender, eventargs e) { lblposition.text = convert.tostring(motor1.getposition()); lblstatus.text = motor1.errormessagestring; // this.chart1.series["kraft"].points.addxy(xpos, convert.todouble(lblkraft.text)); // xpos++;**strong text** } private void btnhoch_click(object sender, eventargs e) { motor1.setdirection(0); motor1.setpositiontype(1); motor1.starttravelprofile(); } private void btnrunter_click(object sender, eventargs e) { motor1.setdirection(1); motor1.setpositiontype(1); motor1.starttravelprofile(); } private void numericschritte_valuechanged(object sender, eventargs e) { motor1.setsteps(convert.toint32(numericschritte.value)); } private void numericgeschwindigkeit_valuechanged(object sender, eventargs e) { motor1.setmaxfrequency(convert.toint32(numericgeschwindigkeit.value)); } private void btndiagramm_click(object sender, eventargs e) { if (timer1.enabled) { timer1.stop(); } else { timer1.start(); } } private void btnresetdiagramm_click(object sender, eventargs e) { this.chart1.series["kraft"].points.clear(); xpos = 0; } private void arduino_datareceived(object sender, serialdatareceivedeventargs e) { string recievedline = " "; while (recievedline != "") { recievedline = arduino.readline(); lblkraft.invoke(lbrecieveddelegate, new object[] { recievedline }); } } void invokelabel1(string data) { label1.text = data; this.chart1.series["kraft"].points.addxy(xpos, convert.todouble(lblkraft.text)); xpos++; } private void btnarduino_click(object sender, eventargs e) { //hier erstellen wir unseren serialport und legen die einstellungen fest arduino = new serialport("com7", 9600); if (!arduino.isopen) { arduino.open(); if (arduino.isopen) { lblarduino.forecolor = system.drawing.color.fromargb(((int)(((byte)(0)))), ((int)(((byte)(200)))), ((int)(((byte)(0))))); lblarduino.text = "verbunden mit " + arduino.portname; } } lbrecieveddelegate = new invokelb(invokelabel1); arduino.datareceived += new serialdatareceivedeventhandler(arduino_datareceived); //datarecieved event abonnieren } } }
when leave out this:
motor1.selectedport = comportbox1.text; motor1.baudrate = convert.toint32(baudratebox1.text);
then works..
i hope can :)
Comments
Post a Comment