{"id":261,"date":"2011-09-19T08:52:44","date_gmt":"2011-09-18T22:52:44","guid":{"rendered":"http:\/\/www.reenadu.com\/?p=261"},"modified":"2011-09-19T08:52:44","modified_gmt":"2011-09-18T22:52:44","slug":"dynamically-update-parity-bit-during-serial-communication-in-c","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=261","title":{"rendered":"Dynamically update parity bit during serial communication in C#"},"content":{"rendered":"<p>Today boss asked me to implement a serial com port simulator. I used <a href=\"http:\/\/support.microsoft.com\/kb\/823179\" target=\"_blank\">MSCommLib COM library<\/a> in C# to develop this simulator.<\/p>\n<p>The serial protocol I am working on is a bit strange:<\/p>\n<p><em>The message synchronization is achieved via a technique utilizing the parity bit of serially of transmitted data. The parity bit sent with each byte no longer denotes the byte\u2019s parity, but the parity bit is used instead to indicate the start of new messages.\u00a0 In this mode, the parity bit is often referred to as the \u201cwake-up bit\u201d.\u00a0 The parity bit is now used in the following manner:\u00a0 when the parity bit is set, this denotes the start (i.e. the first byte) of a new message. , the parity bit (or wake-up bit), is only ever set on the first byte (i.e. the address byte) of a message.\u00a0 The remainder of the message has parity forced to zero.<\/em><\/p>\n<p>Therefore, this protocol needs dynamically update the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.ports.parity.aspx\" target=\"_blank\">parity bit<\/a> settings during the message sending. There are many articles about generic serial com programming. However,\u00a0 it&#8217;s hard to find the parity bit toggling. After few hours exploring, I found a way to achieve this.<\/p>\n<p>Like other serial protocol, we have to initialize com port<\/p>\n<pre lang=\"csharp\">public void InitComPort(int portNumber)\n{\n\t\/\/ Set the com port to be 1\n\tm_ComPort.CommPort = (short)portNumber;\n\n\t\/\/ This port is already open, close it to reset it.\n\tif (m_ComPort.PortOpen)\n\t\tm_ComPort.PortOpen = false;\n\n\t\/\/ Trigger the OnComm event whenever data is received\n\tm_ComPort.RThreshold = 1;  \n\n\t\/\/e, Even. m, Mark. m, (Default) None. o, Odd. s, Space\n\t\/\/ Set the port to 19200 baud, even parity bit, 8 data bits, 1 stop bit (all standard)\n\tm_ComPort.Settings = \"19200,e,8,1\";\n\n\t\/\/ Force the DTR line high, used sometimes to hang up modems\n\tm_ComPort.DTREnable = true;\n\n\t\/\/ No handshaking is used\n\tm_ComPort.Handshaking = MSCommLib.HandshakeConstants.comNone;\n\n\t\/\/ Use this line instead for byte array input, best for most communications\n\tm_ComPort.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;\n\n\t\/\/ Read the entire waiting data when com.Input is used\n\tm_ComPort.InputLen = 0;\n\n\t\/\/ Don't discard nulls, 0x00 is a useful byte\n\tm_ComPort.NullDiscard = false;\n\n\t\/\/ Attach the event handler\n\tm_ComPort.OnComm += new MSCommLib.DMSCommEvents_OnCommEventHandler(OnComm);\n\n\tm_ComPort.ParityReplace = \"0\";\n\n\t\/\/ Open the com port\n\tm_ComPort.PortOpen = true;\n}<\/pre>\n<p>Here is the way to toggle the parity bit during sending message<\/p>\n<pre lang=\"csharp\">private void SendThread()\n{\n\twhile(m_ComPort.PortOpen)\n\t{\n\t\tm_ComPort.PortOpen = false;\n\t\t\/\/ Change port setting, toggle the parity bit to 1\n\t\tm_ComPort.Settings = \"19200,m,8,1\";\n\t\tm_ComPort.PortOpen = true;\n\t\tm_ComPort.Output = addr;\n\n\t\t\/\/ wait 10 ms to toggle parity bit\n\t\tThread.Sleep(10);\n\t\tm_ComPort.PortOpen = false;\n\t\t\/\/ Change port setting, toggle the parity bit to 0\n\t\tm_ComPort.Settings = \"19200,s,8,1\";\n\t\tm_ComPort.PortOpen = true;\n\t\tm_ComPort.Output = packet;\n\t}\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today boss asked me to implement a serial com port simulator. I used MSCommLib COM library in C# to develop this simulator. The serial protocol I am working on is a bit strange: The message synchronization is achieved via a technique utilizing the parity bit of serially of transmitted data. The parity bit sent with &hellip; <a href=\"https:\/\/nickdu.com\/?p=261\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Dynamically update parity bit during serial communication in C#&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-261","post","type-post","status-publish","format-standard","hentry","category-net","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=261"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}