コード例 #1
0
        public void test()
        {
            serialFrame myFrame = new serialFrame(15, 11, 11, 10, 0, 0);

            sendFrame(myFrame);
        }
コード例 #2
0
        private bool sendFrame(serialFrame frame)
        {
            bool result = true;

            byte[] data       = new byte[8];
            byte[] sendData   = new byte[9];
            string sendString = "";

            // require a byte array without the crc to use the crc function
            data[0] = frame.RecAddy;
            data[1] = frame.SendAddy;
            data[2] = frame.ModuleType;
            data[3] = frame.Command;
            data[4] = Convert.ToByte(frame.Data);
            data[5] = Convert.ToByte(frame.Data >> 8);
            data[6] = Convert.ToByte(frame.Data >> 16);
            data[7] = Convert.ToByte(frame.Data >> 24);

            sendData[0] = frame.RecAddy;
            sendData[1] = frame.SendAddy;
            sendData[2] = frame.ModuleType;
            sendData[3] = frame.Command;
            sendData[4] = data[4];
            sendData[5] = data[5];
            sendData[6] = data[6];
            sendData[7] = data[7];
            sendData[8] = Crc8.ComputeChecksum(data);


            Console.WriteLine("----------\nComputed data frame\nSend Addy [{0:X}]\nRec Addy [{1:X}]\nModule [{2:X}]\nCommand [{3:X}]\nData [{4:X},{5:X},{6:X},{7:X}]\nCRC [{8:X}]"
                              , sendData[0], sendData[1], sendData[2], sendData[3], sendData[4], sendData[5], sendData[6], sendData[7], sendData[8]);

            if (Crc8.ComputeChecksum(sendData) == 0)
            {
                Console.WriteLine("Checksum PASS");
            }
            else
            {
                Console.WriteLine("Checksum FAIL");
            }

            sendString = Encoding.ASCII.GetString(sendData);

            if (mySerial.IsOpen)
            {
                try
                {
                    mySerial.WriteLine(sendString);
                    Console.WriteLine("sent Data Frame");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to send serial frame\nSend Addy [{0:X}]\nRec Addy [{1:X}]\nModule [{2:X}]\nCommand [{3:X}]\nData [{4:X},{5:X},{6:X},{7:X}]\nCRC [{8:X}]\nException: " + ex.Message
                                      , sendData[0], sendData[1], sendData[2], sendData[3], sendData[4], sendData[5], sendData[6], sendData[7], sendData[8]);
                }
            }
            else
            {
                result = false;
                Console.WriteLine("Cannot send data frame as Serial Port is closed");
            }

            return(result);
        }
コード例 #3
0
 public bool SendSerialFrame(serialFrame frame)
 {
     return(sendFrame(frame));
 }