Click here to Skip to main content
15,915,080 members
Home / Discussions / C#
   

C#

 
AnswerRe: UDP send /recieve problem... well mainly recieve Pin
Luc Pattyn14-Nov-11 5:35
sitebuilderLuc Pattyn14-Nov-11 5:35 
GeneralRe: UDP send /recieve problem... well mainly recieve Pin
Alberto Bar-Noy14-Nov-11 5:36
Alberto Bar-Noy14-Nov-11 5:36 
AnswerRe: UDP send /recieve problem... well mainly recieve Pin
Alberto Bar-Noy14-Nov-11 5:40
Alberto Bar-Noy14-Nov-11 5:40 
Questiondistribute stored procedure files Pin
Marcel Vreuls (www.agentbase.nl)14-Nov-11 2:30
Marcel Vreuls (www.agentbase.nl)14-Nov-11 2:30 
AnswerRe: distribute stored procedure files Pin
SilimSayo14-Nov-11 10:42
SilimSayo14-Nov-11 10:42 
QuestionMouse move (hover) over an Image with Graphics drawings Pin
eyalbi00714-Nov-11 2:12
eyalbi00714-Nov-11 2:12 
AnswerRe: Mouse move (hover) over an Image with Graphics drawings Pin
Luc Pattyn14-Nov-11 2:43
sitebuilderLuc Pattyn14-Nov-11 2:43 
QuestionClass for Serail Port Comms Pin
KeithF14-Nov-11 1:24
KeithF14-Nov-11 1:24 
Hi Guys,

Just a quick question I have c# code working for communicating with a Serial device using the SerialPort Class (see code below):

private void SendEnq(bool blnIncStep)
     {
         if (blnIncStep)
         {
             iStep++;
         }

         byte[] c = new byte[]
         {
             0x05,
         };

         spBPDione.Write(c, 0, c.Length);
         Console.WriteLine("OUT:  " + ByteToHex(c));

     }

     private void DataRecvEvent()
     {
         int bytes = spBPDione.BytesToRead;

         byte[] comBuffer = new byte[bytes];

         spBPDione.Read(comBuffer, 0, bytes);

         Console.WriteLine("IN:  " + ByteToHex(comBuffer));
         Console.WriteLine("STEP:" + iStep);

         if (ByteToHex(comBuffer).Trim() == "06")
         {
             if (iStep >= 2)
             {
                 //All good to here
             }
             else
             {
                 byte[] c = new byte[] {
                    0x02, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31,
                    0x30, 0x30, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
                    0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x31, 0x20, 0x20, 0x20,
                    0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x03,
                    0x02 /*LRC*/
                 };

                 spBPDione.Write(c, 0, c.Length);
                 Console.WriteLine("OUT:  " + ByteToHex(c));
                 iStep++;
             }
         }
         else if (ByteToHex(comBuffer).Trim() == "07")
         {
             byte[] c = new byte[] {
                0x04,
             };

             spBPDione.Write(c, 0, c.Length);
             Console.WriteLine("OUT:  " + ByteToHex(c));
             iStep++;
         }
         else if (ByteToHex(comBuffer).Trim() == "05")
         {
             if (iStep >= 2)
             {
                 byte[] c = new byte[] {
                     0x06,
                 };

                 //spBPDione.ReceivedBytesThreshold = 29;

                 spBPDione.Write(c, 0, c.Length);
                 Console.WriteLine("OUT:  " + ByteToHex(c));
             }
         }
         else if (ByteToHex(comBuffer).Trim().StartsWith("02"))
         {
             if (iStep >= 2)
             {
                 byte[] c = new byte[] {
                     0x07,
                 };

                 spBPDione.Write(c, 0, c.Length);
                 Console.WriteLine("OUT:  " + ByteToHex(c));
             }
         }
         else if (ByteToHex(comBuffer).Trim() == "04")
         {
             if (iStep >= 2)
             {
                 byte[] c = new byte[] {
                     0x06,
                 };

                 spBPDione.Write(c, 0, c.Length);
                 Console.WriteLine("OUT:  " + ByteToHex(c));
             }
         }
     }


Now this works and will do what its supposed to on the device it is not very nice clean maintainable code, could i refactor this into a class that follows the following protocol to make the code more readable and OO.

My System      			DIRECTION    	 Physical Device
ENQ (05)             	        ->            
	           		<-		 ACK0 (06)
STX (message) ETX LRC		->
				<-		 ACK1 (07)
EOT (04)			->
				<-		 ACK0 (06)
				<-		 ENQ (05)
ACK0 (06)			->
				<-		 STX (message) ETX LRC
ACK1 (07)			->
				<-		EOT (04)
ACK0 (06) 			->

AnswerRe: Class for Serail Port Comms Pin
Luc Pattyn14-Nov-11 1:48
sitebuilderLuc Pattyn14-Nov-11 1:48 
GeneralRe: Class for Serail Port Comms Pin
KeithF14-Nov-11 4:24
KeithF14-Nov-11 4:24 
AnswerRe: Class for Serail Port Comms Pin
Luc Pattyn14-Nov-11 5:06
sitebuilderLuc Pattyn14-Nov-11 5:06 
GeneralRe: Class for Serail Port Comms Pin
KeithF16-Nov-11 4:06
KeithF16-Nov-11 4:06 
AnswerRe: Class for Serail Port Comms Pin
Luc Pattyn16-Nov-11 16:02
sitebuilderLuc Pattyn16-Nov-11 16:02 
QuestionIterate all child forms and saving all reports to one excel. Pin
pinifg14-Nov-11 1:18
pinifg14-Nov-11 1:18 
AnswerRe: Iterate all child forms and saving all reports to one excel. Pin
Blue_Boy14-Nov-11 1:26
Blue_Boy14-Nov-11 1:26 
GeneralRe: Iterate all child forms and saving all reports to one excel. Pin
pinifg14-Nov-11 2:45
pinifg14-Nov-11 2:45 
AnswerRe: Iterate all child forms and saving all reports to one excel. Pin
DaveyM6914-Nov-11 1:45
professionalDaveyM6914-Nov-11 1:45 
QuestionConvert To English To Gujarati Pin
ramesh dabhi13-Nov-11 22:38
ramesh dabhi13-Nov-11 22:38 
AnswerRe: Convert To English To Gujarati Pin
Keith Barrow13-Nov-11 23:20
professionalKeith Barrow13-Nov-11 23:20 
AnswerRe: Convert To English To Gujarati Pin
Abhinav S14-Nov-11 1:42
Abhinav S14-Nov-11 1:42 
AnswerRe: Convert To English To Gujarati Pin
thatraja14-Nov-11 1:57
professionalthatraja14-Nov-11 1:57 
QuestionMultiple lines : datagridview Pin
abbd13-Nov-11 11:24
abbd13-Nov-11 11:24 
AnswerRe: Multiple lines : datagridview Pin
Richard MacCutchan13-Nov-11 22:42
mveRichard MacCutchan13-Nov-11 22:42 
AnswerRe: Multiple lines : datagridview Pin
thatraja14-Nov-11 2:05
professionalthatraja14-Nov-11 2:05 
QuestionCheckers Board game error Pin
xnaLearner13-Nov-11 11:03
xnaLearner13-Nov-11 11:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.