Announcement

Collapse
No announcement yet.

DCDC-USB on Linux

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • DCDC-USB on Linux

    I'm trying to write some code to read voltage and ignition from the dcdc-usb powersupply from minibox. I"m using libusb 1.0 and I'm a little lost in this protocol. I seem to be able to write to it but I'm unable to read from it. I wonder if I'm sending the write message. For my test, I'm just writing a simple test for now, setting up the device, writing a "Get all values" command and then trying to read a response. Here's my code:

    Code:
    #define DCDCUSB_GET_ALL_VALUES			0x81
    #define DCDCUSB_RECV_ALL_VALUES			0x82
    #define READENDPOINT 				0x81
    #define WRITEENDPOINT				0x01
    
    unsigned char* message = new unsigned char[1];
    	message[0] = DCDCUSB_GET_ALL_VALUES;
    	int transfered=0;
    
    	int result = libusb_interrupt_transfer(psudev, WRITEENDPOINT, message, 1, &transfered, TIMEOUT);
    
    	qDebug()<<"result:"<<result<<" transfered:"<<transfered;
    
    	if(result == 0) //success
    	{
    		unsigned char *buffer = new unsigned char[500];
    		result = libusb_interrupt_transfer(psudev, READENDPOINT, buffer, 500, &transfered, TIMEOUT);
    
    		qDebug()<<"result:"<<result<<" transfered:"<<transfered;
    
    		QByteArray reply = QByteArray::fromRawData((const char*)buffer, transfered);
    
    		if(reply.size() && reply.at(0) == DCDCUSB_RECV_ALL_VALUES)
    		{
    			double val = reply.at(3)*(float)0.1558;
    			m_voltageIn = QString("%.2f").arg(val).toDouble();
    
    			val = reply.at(4)*(float)0.1558;
    			m_ignition = QString("%.2f").arg(val).toDouble();
    
    			val = reply.at(5)*(float)0.1170;
    			m_voltageOut = QString("%.2f").arg(val).toDouble();
    		}
    
    		delete buffer;
    	}
    Here's some links to the window's source code and a "text test": http://resources.mini-box.com/online...SB-Pro-src.zip

    Am i missing anything obvious?
    Former author of LinuxICE, nghost, nobdy.
    Current author of Automotive Message Broker (AMB).
    Works on Tizen IVI. Does not represent anyone or anything but himself.

  • #2
    Nagging suspicion: reply.at(0) is signed, and you're comparing it to a byte with the MSB set. I do not see your sample output in your post, but that's what strikes me.

    EDIT: Don't you compile with -Wall set?

    Comment


    • #3
      Output is:

      result: 0 transfered 1

      result: -7 transfered 0
      "operation timed out"



      I tried switching to bulk transfers but with the same result. I don't have -Wall turned on and yes there are a bunch of warnings. I'll try to clean it up a bit, but i doubt it'll solve my issue with not being able to read frmo the device at all.
      Former author of LinuxICE, nghost, nobdy.
      Current author of Automotive Message Broker (AMB).
      Works on Tizen IVI. Does not represent anyone or anything but himself.

      Comment


      • #4
        Ok... one other thing comes to mind

        How do you know that data is immediately available after sending the request? What if it takes some time? Can you run a select() on libusb to let you know that data is available? Can you do send a "wait forever" command to libusb by using '0' for the timeout?

        Comment


        • #5
          Originally posted by kshots View Post
          Ok... one other thing comes to mind

          How do you know that data is immediately available after sending the request? What if it takes some time? Can you run a select() on libusb to let you know that data is available? Can you do send a "wait forever" command to libusb by using '0' for the timeout?
          yes, passing 0 as the timeout will force it to wait forever. and no, that doesn't work. My only guess is that my command is wrong, or missing something. If i set up the usb device wrong, i shouldn't be able to even write to it should i?
          Former author of LinuxICE, nghost, nobdy.
          Current author of Automotive Message Broker (AMB).
          Works on Tizen IVI. Does not represent anyone or anything but himself.

          Comment


          • #6
            Well, you apparently _did_ write to something. You may have the receive endpoint wrong, though... How do you know what you're sending is correct? Did you use a USB-sniffer of some sort with the windows drivers for the thing?

            Comment


            • #7
              Code:
              void HIDDcDcUsb::GetAllParams()
              
              {
              
              	sendMessage(DCDCUSB_GET_ALL_VALUES, 0);
              
              }
              
              bool HIDDcDcUsb::sendMessage(unsigned char cType, unsigned int len, ...)
              
              {
              
              	HArray hArray(len + 1);
              
              	hArray.getBuf()[0] = cType;
              
              
              
              	va_list args;
              
              	va_start(args, len);
              
              	unsigned int cnt = 0;
              
              #ifdef _WIN32	
              
              	unsigned char i = va_arg(args, unsigned char);
              
              #else
              
              	unsigned char i = va_arg(args, unsigned int);
              
              #endif
              
              	while (cnt<len)
              
              	{
              
              		hArray.getBuf()[1+cnt] = i;
              
              		cnt++;
              
              		if (cnt < len)
              
              #ifdef _WIN32			
              
              		 	i = va_arg(args, unsigned char);
              
              #else
              
              			i = va_arg(args, unsigned int);
              
              #endif
              
              	}
              
              	
              
              	va_end(args);
              
              
              
              	m_nError = -1;
              
              	if (Write(&hArray))
              
              	{
              
              		if ((cType == DCDCUSB_MEM_READ_OUT)||(cType == DCDCUSB_MEM_ERASE)||(cType == DCDCUSB_MEM_WRITE_OUT))
              
              		{
              
              			int tmout = 0;
              
              			while (m_nError == -1)
              
              			{
              
              				Sleep(A_SLEEP);
              
              				tmout++;
              
              				if (tmout > A_TIMEOUT) return false;
              
              			}
              
              		}
              
              		return true;
              
              	}
              
              	else return false;
              
              }
              
              bool HIDDcDcUsb::Write(HArray* mesg)
              
              {
              
              	m_lockWrite.lock();
              
              	bool b = m_pParent->Write(mesg);
              
              	m_lockWrite.unlock();
              
              
              
              	return b;
              
              }
              
              ///m_pParent is an HIDDevice:
              
              bool HIDDevice::Write(HArray* mesg)
              
              {
              
              	
              
              #ifdef _WIN32
              
              	DWORD	BytesWritten = 0;
              
              	ULONG	Result;
              
              #else
              
              	long	BytesWritten = 0;
              
              	long	Result;
              
              #endif
              
              
              
              	unsigned short send = m_hidDescriptor->GetOUTLength(mesg->getBuf()[0]);
              
              	//if (mesg->length() == 1) send = 1;
              
              
              
              	unsigned char* buf = new unsigned char[send];
              
              	memset(buf, 0, send);
              
              	memcpy(buf, mesg->getBuf(), mesg->length());
              
              
              
              #ifdef _DEBUG
              
              	if (buf[0]!=0x81)
              
              	{
              
              		TRACE("TX[%d]:",send); 
              
              		for (unsigned int i=0;i<send;i++)
              
              			TRACE("%02X ",buf[i]);
              
              		TRACE("\n");
              
              	}
              
              #endif
              
              
              
              #ifdef _WIN32
              
              	Result = WriteFile 
              
              		(m_WriteHandle, 
              
              		buf, 
              
              		send, 
              
              		&BytesWritten, 
              
              		NULL);
              
              #else
              
              	Result = write(m_WriteHandle, buf , send);
              
              	BytesWritten = Result;
              
              #endif
              
              
              
              	if (Result <= 0)
              
              	{
              
              		DWORD err = GetLastError();
              
              #ifdef _DEBUG
              
              		TRACE(">>>TX[ERR=%d(%d)]\n",err,send); 
              
              #endif
              
              		if (err == ERROR_DEVICE_NOT_CONNECTED)
              
              		{
              
              			if (m_bOpened)
              
              			{
              
              				m_bOpened = false;
              
              				if (m_pParser) 
              
              					m_pParser->Disconnected();
              
              			}
              
              		}
              
              
              
              		delete buf;
              
              		return false;
              
              	}
              
              	else
              
              	{
              
              		delete buf;
              
              		return true;
              
              	}
              
              	return true;
              
              }
              Former author of LinuxICE, nghost, nobdy.
              Current author of Automotive Message Broker (AMB).
              Works on Tizen IVI. Does not represent anyone or anything but himself.

              Comment


              • #8
                I can't read much of that... this board's code tags seem to be broken, the horizontal scroll bar is all the way at the bottom of the box

                Comment


                • #9
                  Originally posted by kshots View Post
                  Well, you apparently _did_ write to something. You may have the receive endpoint wrong, though... How do you know what you're sending is correct? Did you use a USB-sniffer of some sort with the windows drivers for the thing?
                  I got the send and receive endpoints from lsusb -v.
                  Former author of LinuxICE, nghost, nobdy.
                  Current author of Automotive Message Broker (AMB).
                  Works on Tizen IVI. Does not represent anyone or anything but himself.

                  Comment


                  • #10
                    G'Day tripzero,
                    Did you get this working?
                    Rgds,
                    Ben

                    Comment


                    • #11
                      yes, myself and Chunky_Ks have written a simple driver. I want to make a dcdc-usb daemon but the right thing to do may be to write a ups driver using kernel interfaces. that way thinks like UPower/devicekit can use it....

                      Anyway, if you can use the code, i can post it somewhere.

                      cheers,
                      Former author of LinuxICE, nghost, nobdy.
                      Current author of Automotive Message Broker (AMB).
                      Works on Tizen IVI. Does not represent anyone or anything but himself.

                      Comment


                      • #12
                        That'd be great. I'd like to have a look at the code, I'm looking to write a simple utility that monitors Vin, and ultimately signals the PSU to power off when it drops below a certain level (for "safe" SLA discharge).

                        Problem is I know zip about USB, so would love a working example!

                        Comment


                        • #13
                          Originally posted by af3556 View Post
                          That'd be great. I'd like to have a look at the code, I'm looking to write a simple utility that monitors Vin, and ultimately signals the PSU to power off when it drops below a certain level (for "safe" SLA discharge).

                          Problem is I know zip about USB, so would love a working example!
                          that actually makes two of us... but i learned a little bit by doing this. I'll I do currently is read voltages. If you figure out how to do anything else useful, patches accepted .

                          I'll have it in a git repo as soon as I see what license Chunky_Ks wants for his contributed part...
                          Former author of LinuxICE, nghost, nobdy.
                          Current author of Automotive Message Broker (AMB).
                          Works on Tizen IVI. Does not represent anyone or anything but himself.

                          Comment


                          • #14
                            Hello,
                            do you released your code tripzero? I would like to retrieve data from my dcdcusb and your code could be very helpfull.
                            Please let me know

                            Comment


                            • #15
                              I did post the code: http://archive.openice.org/repos/dcdcusb.git. I'm afraid it's not very useful if you don't know how to code. I'm thinking of making a qt-based dbus daemon. would that be useful?
                              Former author of LinuxICE, nghost, nobdy.
                              Current author of Automotive Message Broker (AMB).
                              Works on Tizen IVI. Does not represent anyone or anything but himself.

                              Comment

                              Working...
                              X