{"id":649,"date":"2011-12-19T17:04:47","date_gmt":"2011-12-19T17:04:47","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=649"},"modified":"2011-12-20T00:24:03","modified_gmt":"2011-12-20T00:24:03","slug":"serial-io-in-visualstudio-2011-c","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/12\/serial-io-in-visualstudio-2011-c\/","title":{"rendered":"Serial IO in VisualStudio 2011 \/ C++"},"content":{"rendered":"<p>I needed to hack together a quick windows application to read data over a serial port and write it to a file. Unfortunately I think VisualStudio is probably still the best way of writing Win32 GUI apps. However I&#8217;ve not really used VisualStudio since the VisualStudio.net beta, and most of the stuff I did before that was on VisualStudio 6.<\/p>\n<p>Man Microsoft have really murdered C++&#8230; are these proprietary garbage collection extensions all over the place? I couldn&#8217;t see a way of writing a pure C++, unmanaged GUI app in VS 2011&#8230;<\/p>\n<p>Anyway, to get Serial IO working in a GUI application I needed to add the following methods, these were all added to the Form itself, like I say this was a quick hack:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\npublic:\r\n\t\/\/ members\r\n\tString^ comport;\r\n\tString^ dump_filepath;\r\n\tString^ current_filename;\r\n\tSerialPort^ serialn;\r\n\r\n\t\/\/ initalise serial port\r\n\tvoid init_acquire() {\r\n\t\tint baudRate=9600;\r\n\r\n\t\tserialn = gcnew SerialPort(comport, baudRate);\r\n\r\n\t\tserialn-&gt;ReadTimeout = 50;\r\n\r\n\t\tserialn-&gt;Open();\r\n\t}\r\n\r\n\t\/\/ Read from serial port\r\n\tString^ acquire() {;\r\n\t\tString^ s;\r\n\t\ttry {\r\n\t\t\ts = serialn-&gt;ReadLine();\r\n\t\t}\r\n\t\tcatch (TimeoutException ^) { }\r\n\t\t\r\n\t\treturn s;\r\n\t}\r\n\r\n\t\/\/ Button handler, starts thread\r\n\tprivate: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {\r\n\r\n\t\tdumped_lines      = 0;\r\n\t\tdumped_file_count = 0;\r\n\t\tdump_filepath = this-&gt;drop_location_textbox-&gt;Text;\r\n\t\tcurrent_filename = dump_filepath + dumped_file_count + &quot;.txt&quot;;\r\n\r\n\t\tcomport = comport_textbox-&gt;Text;\r\n\r\n\t    ThreadStart ^myThreadDelegate = gcnew ThreadStart(this, &amp;Form1::repeat);\r\n\t\ttrd = gcnew Thread(myThreadDelegate);\r\n\t\ttrd-&gt;IsBackground = true;\r\n\t\ttrd-&gt;Start();\r\n\t\tdumped_lines = 0;\r\n\t}\r\n\r\n\t\/\/ Thread code\r\n\tdelegate void DelegateThreadTask();\r\n\tprivate: void ThreadTask() {\r\n\r\n\t\t\/\/ This weirdness is required because setting the TextBox text is not thread safe.\r\n\t\tif (data_textbox-&gt;InvokeRequired == false) {\r\n\t\t\tString ^data = acquire();\r\n\t\t\tthis-&gt;data_textbox-&gt;Text = this-&gt;data_textbox-&gt;Text + data;\r\n\t\t\t\/\/current_filename\r\n\r\n\t\t\tStreamWriter^ sw = gcnew StreamWriter(current_filename,true);\r\n\r\n\t\t\tsw-&gt;WriteLine(data);\r\n\r\n\t\t\tsw-&gt;Close();\r\n\r\n\t\t} else {\r\n\t\t\tDelegateThreadTask ^myThreadDelegate = gcnew DelegateThreadTask(this,&amp;Form1::ThreadTask);\r\n\t\t\tthis-&gt;Invoke(myThreadDelegate);\t\t\t\r\n\t\t}\r\n\t}\r\n\r\n\t\/\/ Thread loop (reads from serial, dumps to file\/textbox.\r\n\tprivate: void repeat() {\r\n\t\tinit_acquire();\r\n\t\twhile(true) {\r\n\t\t\tThreadTask();\r\n\t\t\tThread::Sleep(100);\r\n\t\t}\r\n\t}\r\n<\/pre>\n<p>You&#8217;ll also need to add the following headers:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tusing namespace System::IO::Ports;\r\n\tusing namespace System::Threading;\r\n\tusing namespace System::IO;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I needed to hack together a quick windows application to read data over a serial port and write it to a file. Unfortunately I think VisualStudio is probably still the best way of writing Win32 GUI apps. However I&#8217;ve not really used VisualStudio since the VisualStudio.net beta, and most of the stuff I did before [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-649","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-at","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=649"}],"version-history":[{"count":28,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/649\/revisions"}],"predecessor-version":[{"id":678,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/649\/revisions\/678"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}