Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

UART PinOffset in Python

U6 Python UART PinOffset Pin Offset DB15 LabJack

  • Please log in to reply
4 replies to this topic

#1 Victor

Victor
  • Members
  • 3 posts

Posted 22 October 2014 - 02:28 PM

Hello,

 

I am trying to use the UART over the DB15 connector on the LabJack U6 via Python (rather than the screw connectors)

 

My initial Python code is as follows:

 

import u6

d = u6.U6()
d.configU6()
d.getCalibrationData()
d.configTimerClock()

d.asynchConfig(DesiredBaud=9600)

txBytes = [116, 101, 115, 116, 13, 10] #"test\r\n"
d.asynchTX(txBytes)

This code works great for UART over FIO0 and FIO1.

 

Unfortunately, I can't figure out what the command would be to offset the RX and TX lines to use EIO0 and EIO1 (on the DB15 connector).

 

I tried initializing timers and using the timer offsets. ie: variations of:

 

d.configIO(NumberTimersEnabled = 0, TimerCounterPinOffset = 8)

 

But when I run the following:

 

print d.readDefaultsConfig()

 

PinOffset is still zero.

 

How can I go about shifting the Tx and Rx pins in Python so that they are on the DB15 connector rather than the screw terminals?

 

Thanks in advance!

 

 

 

 

 



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 22 October 2014 - 04:21 PM

I'll look into this further tomorrow, but asynchConfig looks like it is calling configIO and resetting the pin offset to 0.



#3 Victor

Victor
  • Members
  • 3 posts

Posted 23 October 2014 - 06:44 AM

As soon as you mentioned that it looked like asynchConfig was resetting configIO I had an idea...

 

asynchConfig looks like it operates stand-alone to the configIO which is what actually enables the UART.

 

My code now resembles the following:

import u6

d = u6.U6()
d.configU6()
d.getCalibrationData()
d.configTimerClock()

d.asynchConfig(DesiredBaud=9600)

d.configIO(NumberTimersEnabled = 0, TimerCounterPinOffset = 8, EnableUART = True)

txBytes = [116, 101, 115, 116, 13, 10] #"test\r\n"

d.asynchTX(txBytes)

This sets the desired Baud rate with asynchConfig (which also enables UART on FIO0 and FIO1).

 

Calling configIO afterwards seems to reset which UART pin is active.

 

This code snippet successfully shifted the UART pins to EIO0 and EIO1 (pins 4 and 12 on the DB15 connector).

 

For those who want to test this themselves, here's the line to add at the bottom if you want to read the communications with the LabJack:

 

print d.asynchRX()['AsynchBytes']

 

Note that you will need to connect EIO0 and EIO1 together for this to work (or whichever pins you desire based upon the TimerCounterPinOffset (aka PinOffset in the documentation)). I also find that adding a short 0.25 second delay after sending the bytes with asynchTX improves reliability of being able to read the UART drastically ( time.sleep(0.25) )

 

I'm not sure why the delay is necessary.

 

I have found that if you connect the LabJack UART to an Arduino UART, and have the Arduino echo the bytes sent to it, the LabJack will miss the returning bytes if no delay is added. And for me this is an issue.

 

Maybe someone has some thoughts on this?



#4 Victor

Victor
  • Members
  • 3 posts

Posted 24 October 2014 - 09:51 AM

Hello All,

 

Doing some digging it was an oversight on my part about the delay.

 

Of course if you try to read what an Arduino (or other microcontroller) returns right away after sending the message, there hasn't been enough time for the message to be sent and returned... (woups!)

 

Here is a complete minimal working example of the LabJack UART communication in Python that everyone should be able to use.

import u6

d = u6.U6()
d.configU6()
d.getCalibrationData()
d.configTimerClock()

d.asynchConfig(DesiredBaud=9600)

d.configIO(NumberTimersEnabled = 0, TimerCounterPinOffset = 8, EnableUART = True)

#What do I want to send?
To_Send = 'This is my sample text&' #23 char long

#Convert this string into bytes for the LabJack
txBytes = []
for x in range(0, len(To_Send)):
    txBytes.append(ord(To_Send[x]))

#Send the message
d.asynchTX(txBytes)

#Read the response; Every time you call asynchRX 30 bytes are returned (0 if empty)
rxResponseT = ''
while '&' not in rxResponseT: #& is the terminator in the message
    rxResponse = d.asynchRX()['AsynchBytes']
    if rxResponse[0] != 0:
        for x in range(0, len(rxResponse)):
            if rxResponse[x] != 0:
                rxResponseT = rxResponseT + (chr(rxResponse[x]))

    print rxResponseT

The code on the Arduino is very simple... In the main loop I have:

  if (Serial.available()){
    index = 0;
    while(Serial.available()){
      data[index] = Serial.read();
      index++;
      data[index] = '\0';
      delay(5);
    }
    Serial.println(data);
  }
  
  delay(25);

Hopefully this helps those trying to do some fun communication between microcontrollers with the LabJack. From what I have seen in the source code this should work for both the U6 and the U3 with minor changes.

 

Now if only I can figure out why the SPI clock on the LabJack is so variable... but that's an issue for another day.

 

Cheers,

 

Victor



#5 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 26 October 2014 - 02:07 AM

Serial comm on the T7 is software generated. So the clock rates are highly dependent on other running features. Some features, such as stream and DIO_EF, can add a lot of variance. And some base functionality, such as time keeping and USB packet handling, will add occasional delays to the clock signal.





Also tagged with one or more of these keywords: U6, Python, UART, PinOffset, Pin, Offset, DB15, LabJack

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users