IOException while opening Bluetooth Serial Port of HC-06 module in Visual Studio (C#) -
i'm trying send input data arduino pc bluetooth serial connection using hc-06 module. while opening corresponding serial port, returns ioexception
, means
the port in invalid state
but there aren't problems while connecting using serial monitor applications of windows , android (bt serial terminal)
after pairing bluetooth module, shows 2 com ports connected it:
- com6 outgoing hc-06
- com7 incoming hc-06 'dev b'
i suspected i'm using wrong port, tried both of them: first 1 opens, doesn't acquire data. second 1 gives ioexception while opening mentioned above , port said 1 needed used forums.
which com port should use actually? can cause problem it?
here list of bluetooth com ports:
visual studio code in c#
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; using system.io.ports; using system.threading; namespace windowsformsapplication1 { public partial class form1 : form { private static string buffer; public form1() { initializecomponent(); try { serialport1.open(); } catch (unauthorizedaccessexception) { console.writeline("error opening serial port"); } catch (system.io.ioexception) { console.writeline("ioexception"); } serialport1.datareceived += new serialdatareceivedeventhandler(readbuffer); } private void readbuffer(object sender, serialdatareceivedeventargs e) { this.invoke(new action(() => { buffer = string.empty; buffer = serialport1.readline(); console.writeline(buffer); /* if (buffer[0] == '@') { string c = string.empty; c += buffer[1]; if (c == "%") sendkeys.send("{backspace}"); else sendkeys.send(c); }*/ })); } private void initializecomponent() { this.components = new system.componentmodel.container(); this.textbox3 = new system.windows.forms.textbox(); this.serialport1 = new system.io.ports.serialport(this.components); this.button1 = new system.windows.forms.button(); this.suspendlayout(); // // textbox3 // this.textbox3.location = new system.drawing.point(279, 296); this.textbox3.multiline = true; this.textbox3.name = "textbox3"; this.textbox3.size = new system.drawing.size(378, 147); this.textbox3.tabindex = 2; // // serialport1 // this.serialport1.portname = "com6"; // // button1 // this.button1.location = new system.drawing.point(329, 110); this.button1.name = "button1"; this.button1.size = new system.drawing.size(283, 69); this.button1.tabindex = 3; this.button1.text = "led"; this.button1.usevisualstylebackcolor = true; this.button1.click += new system.eventhandler(this.button1_click); // // form1 // this.clientsize = new system.drawing.size(948, 549); this.controls.add(this.button1); this.controls.add(this.textbox3); this.name = "form1"; this.resumelayout(false); this.performlayout(); } private void button1_click(object sender, eventargs e) { serialport1.write("1"); } } }
part of arduino code:
#define analog a0 #define rx 0 #define tx 1 void setup() { serial.begin (9600); pinmode (rx,input); pinmode (tx,output); } ... int r = analogread(analog); serial.println(r);
Comments
Post a Comment