Exemple #1
0
        // this writes a frame into wireshark. it calculates the timestamp and length and uses that
        // for the frame header. it then writes captured bytes into wireshark

        static void write_frame(uint frame_len, CanCommand CAN_Stuct)
        {
            uint incl_len, orig_len;
            long sec, usec;

            // generating timestamserialPort. its kind of cheesy but there isn't a unix timestamp mechanism in win.
            // just counting ticks from when program was started. each tick is 100 nsec.
            long diff_in_ticks = DateTime.Now.Ticks - start_time_in_ticks;  // get difference in ticks

            sec            = diff_in_ticks / TimeSpan.TicksPerSecond;       // get seconds
            diff_in_ticks -= (sec * TimeSpan.TicksPerSecond);               // subtract off seconds from total
            usec           = diff_in_ticks / 10;                            // get usec

            // calculate frame length. we won't be feeding frame checksum (FCS) into wireshark.
            incl_len = (uint)frame_len;
            orig_len = frame_len;

            // write frame header first
            write_frm_hdr(sec, usec, incl_len, orig_len);



            // send CAN frame for wireshark
            ws.Write((uint)littleToBigEndianInt32(CAN_Stuct.ID));
            ws.Write((byte)CAN_Stuct.lengthData);
            ws.Write((byte)0); //pad
            ws.Write((byte)0); //res0
            ws.Write((byte)0); //res1
            for (int i = 0; i < CAN_Stuct.lengthData; i++)
            {
                ws.Write((byte)CAN_Stuct.data[i]);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            ///////////// Commane line options  /////////////////////////
            if (args.Length == 0)
            {
                // generate list of active serial ports
                string[] names = SerialPort.GetPortNames();
                Console.WriteLine("Serial ports:");
                foreach (string name in names)
                {
                    Console.WriteLine(name);
                }
                Console.Write("Choose one:"); // read name port (String)
                port = Console.ReadLine();
            }
            else if (args.Length == 1)
            {
                port = args[0];
            }
            else
            {
                Console.WriteLine("Usage: wsbridge <portname>");
                Console.WriteLine("or leave portname blank for a list of ports.");
                Environment.Exit(0);
            }



            ///////////////// Open serial port /////////////////////////
            try
            {
                serialPort = new SerialPort(port, 115200, Parity.None, 8, StopBits.One);
                serialPort.Open();
            }
            catch (Exception e)
            {
                // ooops, serial port can't be opened. throw exception, print message, and exit
                Console.WriteLine("Error opening serial port. Msg = " + e.Message);
                Environment.Exit(0);
            }
            Console.WriteLine("Serial port opened successfully.");



            ///////////////////// create pipe   /////////////////////////
            try
            {
                wspipe = new NamedPipeServerStream("wireshark", PipeDirection.Out);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error opening pipe. Msg = " + e.Message);
                serialPort.Close();
                Environment.Exit(0);
            }


            ///////////// wait for wireshark to connect to pipe /////////////
            Console.WriteLine("Waiting for connection to wireshark.");
            Console.WriteLine("Open wireshark and connect to interface: Local:\\\\.\\pipe\\wireshark");
            wspipe.WaitForConnection();   // block process
            Console.WriteLine("Client connected.");


            //////////// connect binary writer to pipe to write binary data into it //////////
            ws = new BinaryWriter(wspipe);


            ///////////// add serial data capture event  ///////////////////////
            // serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
            state = FSM.START_CAPTURE; // set start state


            ///////////// keep track of time started. this will be used for timestamps ////////////
            start_time_in_ticks = DateTime.Now.Ticks;


            //////////// generate header to identify the packet capture     ////////////////
            write_global_hdr();

            // run forever


            ///////////////////  initialize module  ///////////////////////////
            Console.WriteLine("Initialize Status:");

            closeCAN_Channel();

            setupCAN_BitRate('6');
            openCAN_Channel();

            //Thread.Sleep(milliseconds);

            //closeCAN_Channel();
            //closeCAN_Channel();
            //closeCAN_Channel();
            //closeCAN_Channel();
            //closeCAN_Channel();

            //closeCAN_Channel();
            //  setTimeStamp(true);



            while (true)
            {
                serialPort_DataReceived();
                switch (state)
                {
                //  Read Data
                case FSM.SUCCESS:
                    CanCommand CAN_Stuct = createStuctFromCommand();
                    write_frame((uint)CAN_Stuct.lengthData + 8, CAN_Stuct);

                    state = FSM.START_CAPTURE;

                    break;
                }
            }
        }
Exemple #3
0
        static CanCommand createStuctFromCommand()
        {
            CanCommand tempCanCommand = new CanCommand();
            String     commandStr     = System.Text.Encoding.Default.GetString(dataReceive);
            String     ID_str         = "";

            String[] data_str = new String[8];

            int EndID      = 0;
            int lengthPosi = 0;
            int startData  = 0;


            if ((char)dataReceive[0] == 't')
            {
                EndID      = 3;
                lengthPosi = 4;
                startData  = lengthPosi + 1;
            }
            else if ((char)dataReceive[0] == 'T')
            {
                EndID      = 8;
                lengthPosi = 9;
            }

            startData = lengthPosi + 1;

            ID_str = commandStr.Substring(1, EndID);                                              // cut string for ID

            tempCanCommand.ID = Int32.Parse(ID_str, System.Globalization.NumberStyles.HexNumber); // ID
            // Console.WriteLine("ID_str " + ID_str + " ID_num " + tempCanCommand.ID);

            tempCanCommand.lengthData = (byte)(dataReceive[lengthPosi] - '0');// length

            byte[] dataTemp = new byte[tempCanCommand.lengthData];

            int j = startData;

            for (int i = 0; i < tempCanCommand.lengthData; i++)
            {
                data_str[i] += (char)dataReceive[j];
                data_str[i] += (char)dataReceive[j + 1];

                dataTemp[i] = (byte)Int32.Parse(data_str[i], System.Globalization.NumberStyles.HexNumber);
                // Console.WriteLine("data[" + i + "] " + data_str[i] + " dataTemp[i] " + dataTemp[i]);

                j += 2;
            }

            tempCanCommand.data = dataTemp;

            // Console.WriteLine("j : " + j +" length " + length);

            if (j < length - 1)  // length include CR ( need -1 )
            {
                String timeStr = commandStr.Substring(j, 4);

                tempCanCommand.haveTimeStamp = true;
                tempCanCommand.timeStamp     = Int32.Parse(timeStr, System.Globalization.NumberStyles.HexNumber);
                //  Console.WriteLine("TimeStamp Str: " + timeStr + "TimeStamp int: " + tempCanCommand.timeStamp);
            }
            else
            {
                tempCanCommand.haveTimeStamp = false;
            }
            //     Console.WriteLine("");

            return(tempCanCommand);
        }