{"id":451,"date":"2010-12-21T20:49:20","date_gmt":"2010-12-21T20:49:20","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=451"},"modified":"2022-02-17T06:24:04","modified_gmt":"2022-02-17T06:24:04","slug":"serial-port","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/serial-port\/serial-port","title":{"rendered":"Serial Port"},"content":{"rendered":"<pre>\r\n<code>\r\nusing namespace System::IO::Ports;\r\n<\/code><\/pre>\n<h4>\nTo Discover Available Comm Ports On The PC<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t array&lt;String^&gt; ^AvailableSerialPorts;\r\n\t array&lt;unsigned char&gt; ^aTemp;\r\n\t String ^sTemp;\r\n\r\n\t try\r\n\t {\r\n\t\tAvailableSerialPorts = SerialPort::GetPortNames();\r\n\r\n\t\tcmbCommPort-&gt;Items-&gt;Add(&quot;None&quot;);\r\n\t\tfor(int Count = 0; Count &lt; AvailableSerialPorts-&gt;Length; Count++)\r\n\t\t{\r\n\t\t\tsTemp = AvailableSerialPorts[Count];\r\n\r\n\t\t\t\/\/Remove any non numeric last letter which can be added by microsoft bluetooth drivers\r\n\t\t\taTemp = System::Text::Encoding::UTF8-&gt;GetBytes(sTemp);\r\n\t\t\twhile (\r\n\t\t\t\t(aTemp-&gt;Length &gt; 1) &amp;&amp; \r\n\t\t\t\t((aTemp[(aTemp->Length - 1)] < '0') || (aTemp[(aTemp->Length - 1)] &gt; &#39;9&#39;))\r\n\t\t\t\t)\r\n\t\t\t{\r\n\t\t\t\tArray::Resize(aTemp, (aTemp-&gt;Length - 1));\r\n\t\t\t}\r\n\t\t\tsTemp = System::Text::Encoding::UTF8-&gt;GetString(aTemp);\r\n\r\n\t\t\tcmbCommPort-&gt;Items-&gt;Add(sTemp);\r\n\t\t}\r\n\t\tcmbCommPort-&gt;SelectedIndex = 0;\r\n\t }\r\n\t catch(System::Exception^ e)\r\n\t {\r\n\t }\r\n<\/code><\/pre>\n<h4>\nTo use a simple form based SerialPort<br \/>\n<\/h4>\n<p>\n(This is no use if you want to use in a seperate class!)<br \/>\nPlace the serialport from the tool box onto the form (not the old Comm control that used to be used in VB).<br \/>\nYou can set its properties by selecting it and using the properties box<br \/>\nTo create a new event, such as datareceived press the lightning bolt &#39;events&#39; icon in the properties box and double click the event you want to create.\n<\/p>\n<h4>\nTo create a serial port in a class<br \/>\n<\/h4>\n<h5>\nIn the class private declarations area<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tSystem::IO::Ports::SerialPort ^serialPort1;\r\n<\/code><\/pre>\n<h5>\nIn the class constructor function<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tserialPort1 = gcnew System::IO::Ports::SerialPort();\r\n\tserialPort1-&gt;BaudRate = 19200;\r\n\tserialPort1-&gt;DataBits = 8;\r\n\tserialPort1-&gt;Parity = Parity::None;\r\n\tserialPort1-&gt;StopBits = StopBits::One;\r\n\tserialPort1-&gt;Handshake = Handshake::None;\r\n\r\n\tserialPort1-&gt;DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &amp;MyClass::serialPort1_DataReceived);\r\n<\/code><\/pre>\n<h5>\nIn a function that wants to set the port number and open it<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\ttry\r\n\t{\r\n\t\tserialPort1-&gt;PortName = &quot;COM&quot; + CommPort;\r\n\t\tif (serialPort1-&gt;IsOpen)\t\t\t\/\/Port should not already be open\r\n\t\t\treturn(0);\r\n\t\tserialPort1-&gt;Open();\t\t\t\/\/Try and open it\r\n\t\treturn(1);\t\t\t\t\t\/\/Sucess\r\n\t}\r\n\tcatch (Exception^ e)\r\n\t{\r\n\t\treturn(0);\r\n\t}\r\n<\/code><\/pre>\n<h5>\nIn a function that wants to send data<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tserialPort1-&gt;Write(&quot;SCIP2.0\\n&quot;);\r\n\t\/\/or\r\n\tString ^buffer;\r\n\tbuffer = &quot;BM\\n&quot;;\r\n\tserialPort1-&gt;Write(buffer);\r\n<\/code><\/pre>\n<h5>\nFunction to receive<br \/>\n<\/h5>\n<pre>\r\n<code>\r\n\tvoid MyClass::serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e)\r\n\t{\r\n\t\tint RxBytes;\r\n\t\tint rxByte;\r\n\r\n\t\tRxBytes = serialPort1-&gt;BytesToRead;\r\n\r\n\t\t\/\/----- GET WAITING BYTES -----\r\n\t\twhile (RxBytes--)\r\n\t\t{\r\n\t\t\trxByte = serialPort1-&gt;ReadByte();\r\n\r\n\t\t}\r\n<\/code><\/pre>\n<h4>\n<span style=\"color: rgb(51, 51, 51); font-size: 10px;\">Threading On DataReceived<\/span><br \/>\n<\/h4>\n<p>\n<span style=\"color: rgb(51, 51, 51); font-size: 10px;\">The DataReceived event will not necessarily occur on the same thread as your form UI so you need to use a thread safe call to a new function if you want to update form components in response to data being received.<\/span>\n<\/p>\n<h4>\nIssue where the _DataReceived event might occasionally not get triggered<br \/>\n<\/h4>\n<p>\nSee http:\/\/www.codeproject.com\/KB\/dotnet\/NET_20_SerialPort.aspx?msg=2729827#xx2729827xx\n<\/p>\n<p>\nOne fix is to add this into the waiting for response loop\n<\/p>\n<pre>\r\n<code>\r\n\tSystem::Windows::Forms::Application::DoEvents();\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using namespace System::IO::Ports; To Discover Available Comm Ports On The PC array&lt;String^&gt; ^AvailableSerialPorts; array&lt;unsigned char&gt; ^aTemp; String ^sTemp; try { AvailableSerialPorts = SerialPort::GetPortNames(); cmbCommPort-&gt;Items-&gt;Add(&quot;None&quot;); for(int Count = 0; Count &lt; AvailableSerialPorts-&gt;Length; Count++) { sTemp = AvailableSerialPorts[Count]; \/\/Remove any non numeric last letter which can be added by microsoft bluetooth drivers aTemp = System::Text::Encoding::UTF8-&gt;GetBytes(sTemp); while ( [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[],"class_list":["post-451","post","type-post","status-publish","format-standard","hentry","category-serial-port"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=451"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/451\/revisions"}],"predecessor-version":[{"id":1078,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/451\/revisions\/1078"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}