InDashPC.org Forum Index InDashPC.org
In Dash Car PC forum
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Car2PC interface wrapper for .NET

 
Post new topic   Reply to topic    InDashPC.org Forum Index -> Software
View previous topic :: View next topic  
Author Message
fireboy
Newbie


Joined: 08 Dec 2007
Posts: 8

PostPosted: Sun Dec 09, 2007 2:28 pm    Post subject: Car2PC interface wrapper for .NET Reply with quote

I have recently bought a Car2PC adapter for Mazda and I have an idea to make a managed wrapper of the Car2PC interface in C# (.NET 2.0). I am planning to make it a reusable component that anyone could use in their .NET based projects.
Where could I find documentation of the interface as well as some demos or source code of current winamp/wmp plugins?
Back to top
View user's profile Send private message
jbors
Site Admin


Joined: 12 Nov 2004
Posts: 1675

PostPosted: Sun Dec 09, 2007 11:06 pm    Post subject: Reply with quote

Here is the info:
http://indashpc.org/vbullettin/viewtopic.php?t=103
I would suggest using COM(RS232) interface directly instead of using HU_* functions (even though they provide port autodetection)
It will keep things simpler...
grom_console should be very useful also Wink
Back to top
View user's profile Send private message
fireboy
Newbie


Joined: 08 Dec 2007
Posts: 8

PostPosted: Mon Dec 10, 2007 5:59 am    Post subject: Reply with quote

Great. Thanks for the info. I'll probably use the USB COM port to send and receive messages.
What would be the best way to automatically recognize the COM port that has the interface hooked on it? Trying to send some commands and see if it responds?
Another thing - if I use USB COM port, will I be able to use HU emulator?
Back to top
View user's profile Send private message
JozefS



Joined: 29 Jan 2009
Posts: 2

PostPosted: Thu Jan 29, 2009 7:45 am    Post subject: Reply with quote

fireboy, how is the progress with your c# wrapper? I am currently developing with the FTD2XX_NET.dll from the FTDI website. Receiving signals from my pioneer unit is no problem, but I am stuck on the sending part...
Back to top
View user's profile Send private message
fireboy
Newbie


Joined: 08 Dec 2007
Posts: 8

PostPosted: Thu Jan 29, 2009 7:56 am    Post subject: Reply with quote

Everything's fine, the wrapper is more or less finished and working. I had some problems because the specification of commands given on this forum does not exactly match the commands that are actually sent through my Car2PC adapter, but I used the grom console to see the actual command set.
The wrapper was written using the standard .NET SerialPort class and works nicely. I can share the source code if you (or anyone) are interested.
Back to top
View user's profile Send private message
JozefS



Joined: 29 Jan 2009
Posts: 2

PostPosted: Fri Jan 30, 2009 12:13 am    Post subject: Reply with quote

I am very interested in the source code. I will give you my email address in a pm.
Back to top
View user's profile Send private message
Oitz



Joined: 29 Mar 2005
Posts: 3

PostPosted: Thu Oct 01, 2009 9:00 pm    Post subject: Did you finish the .NET wrapper? Reply with quote

Hi,

Did you have a chance to finish the .NET wraper? I am very interested in it and would like to ask for the code, if possible.
Back to top
View user's profile Send private message
fireboy
Newbie


Joined: 08 Dec 2007
Posts: 8

PostPosted: Fri Oct 02, 2009 4:13 am    Post subject: Reply with quote

Yes, it's finished (works perfectly on my Mazdaspeed 6). It is, however, integrated into my software project (not available separately), so I'll try to separate it and add some quick documentation and will get back to you after the weekend.
Back to top
View user's profile Send private message
Howler



Joined: 02 Apr 2007
Posts: 1

PostPosted: Thu Nov 05, 2009 9:08 am    Post subject: Reply with quote

Any updates on your wrapper? I just installed a pc in my speed6, and was getting ready to create a c# wrapper for my car2pc =)
Back to top
View user's profile Send private message
fireboy
Newbie


Joined: 08 Dec 2007
Posts: 8

PostPosted: Thu Nov 05, 2009 12:24 pm    Post subject: Reply with quote

OK here comes the wrapper. This code is released in the public domain, feel free to use it for any free or commercial projects and do whatever you want. Basically what this class does is provide a structured way to detect input from the interface in the serial port buffer and send custom commands to the interface. I haven't used it much, but at least the part that detects input from the interface works well and is resilient if you connect to the unit in the middle of transmission, etc.

I have noticed that interfaces do not follow the official specification very closely, so it's best if you use the Grom console to see the exact commands that occur when you press headunit buttons and handle them in the "ProcessCommand" method.
Also use the "SendCommand" to send a custom command. You only need to supply the text part, such as SendCommand("TR003");

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace CarPC.Integration.HeadUnit
{
    public class Car2PCIntegration : IDisposable
    {
        /// <summary>
        /// Each command is identified by this character
        /// </summary>
        private const char CommandStartIdentifier = '?';

        /// <summary>
        /// Media player engine reference
        /// </summary>
        private MediaPlayer mediaPlayer;

        private SerialPort adapterPort;
        /// <summary>
        /// Linked list that stores data received from the serial port
        /// </summary>
        private LinkedList<char> inputBuffer = new LinkedList<char>();

        public Car2PCIntegration(string portName)
        {
            //initialize serial port connection
            adapterPort = new SerialPort(portName, 9600);
            adapterPort.DataReceived += new SerialDataReceivedEventHandler(adapterPort_DataReceived);
            adapterPort.Open();
        }

        /// <summary>
        /// Copies the data received on the serial port to an input buffer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void adapterPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (e.EventType == SerialData.Chars)
            {
                lock (inputBuffer)
                {
                    while (adapterPort.BytesToRead > 0)
                    {
                        char value = (char)adapterPort.ReadChar();
                        inputBuffer.AddLast(value);
                    }
                }
            }
            CheckForCommands();
        }

        /// <summary>
        /// Checks input buffer for commands and processes them accordingly
        /// </summary>
        private void CheckForCommands()
        {
            lock (inputBuffer)
            {
                //remove trash from the beginning, command has to start with an identifier char
                while (inputBuffer.Count > 0 &&
                    inputBuffer.First.Value != CommandStartIdentifier)
                {
                    inputBuffer.RemoveFirst();
                }

                //find all commands in the buffer
                while (inputBuffer.Count > 2 &&
                    inputBuffer.First.Value == CommandStartIdentifier)
                {
                    LinkedListNode<char> lengthNode = inputBuffer.First.Next;
                    int length = (int)lengthNode.Value;
                    //command not yet read into the buffer
                    if (inputBuffer.Count < length + 2)
                    {
                        return;
                    }

                    inputBuffer.RemoveFirst(); //command identifier
                    inputBuffer.RemoveFirst(); //length byte

                    StringBuilder commandText = new StringBuilder(length);
                    for (int i = 0; i < length; i++)
                    {
                        commandText.Append(inputBuffer.First.Value);
                        inputBuffer.RemoveFirst();
                    }

                    ProcessCommand(commandText.ToString());
                }
            }
        }

        /// <summary>
        /// Handles a given command
        /// </summary>
        /// <param name="command">Command text</param>
        private void ProcessCommand(string command)
        {
            switch (command)
            {
                //play started (1) or stopped (0) (in)
                case "PL":
                    //mediaPlayer.Play();
                    break;
                //play started (1) or stopped (0) (in)
                case "ST":
                    //mediaPlayer.Pause();
                    break;
                //next track button pressed (in)
                case "NT":
                    //mediaPlayer.Next();
                    break;
                //previous track button was pressed (in)
                case "PT":
                   //mediaPlayer.Previous();
                    break;
                //MIX button was pressed (in)
                case "MX0":
                    //mediaPlayer.Shuffle = false;
                    break;
                case "MX1":
                    //mediaPlayer.Shuffle = true;
                    break;
                //REPEAT button was pressed (in)
                case "RP0":
                    //mediaPlayer.Repeat = false;
                    break;
                case "RP1":
                    //mediaPlayer.Repeat = true;
                    break;

                    //TODO: add support for other commands here
            }
        }

        /// <summary>
        /// Sends a command to the adapter
        /// </summary>
        /// <param name="command">Command text</param>
        private void SendCommand(string command)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(CommandStartIdentifier);
            sb.Append((char)command.Length);
            sb.Append(command);

            adapterPort.Write(sb.ToString());
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (adapterPort != null)
            {
                adapterPort.Close();
                adapterPort.Dispose();
            }
        }

        #endregion
    }
}


Ask me if anything is unclear. Btw congratulations on the PC in the Speed 6! The car is great by itself, but the PC makes driving it even greater. Are you a member of any mazda forums? I've got a pc in my Speed 6 too, we could share some ideas.
Back to top
View user's profile Send private message
Louk
Newbie


Joined: 13 Feb 2010
Posts: 17
Location: Netherlands

PostPosted: Sat Feb 13, 2010 10:24 am    Post subject: Thanks: Nice work! Reply with quote

Hi,

First of all, let me introduce myself as a newbie on this forum: I'm a beta tester for Centrafuse and developing a Car2PC plugin for Centrafuse V3.0 / V3.1.

My basic (communication) code is quite similar to your wrapper; this gives me hope again Wink

I'm on the edge of releasing the first public beta release for Centrafuse V3.0, but I'm experiencing a nasty problem: Every time windows resumes from suspend mode, Centrafuse crashes when my plugin is active. What I see in the log files, is that just before a power mode change event is fired, the serial port is closed (not by my code) and after Windows is resumed, my plug-in code continues to run as if nothing happened. This only happens when the Car2PC module is connected to the PC's USB port.

When windows signals a power mode change to suspend mode, I stop all communication, dispose all event handlers and timers, close the port and dispose it. When Windows signals a resume, I start all over again.

First I thought, it was caused by double disposing of the serial port object (Close is an implicit dispose), but leaving out the dispose when the port was already closed did not help much.

So, now I'm thinking about a driver problem. At the moment I'm running the vcp_win32_car2pc drivers (Windows XP and Windows7).

To make a long story short: What drivers you are using and does this sound familiar?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    InDashPC.org Forum Index -> Software All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group