We’ll get going, and as we go through with the next videos we’ll just iterate on the same t script.

So begin by creating on start block. We’ll leave this empty then on stop block, leave that one empty, as well. So we’re going to initialize the first two channels of this device. We’re gonna start by setting the bit rate. So canSetBitrate first channel we’re going to set to canBITRATE_500K and the second channel will do the same. So now that we’ve set the bitrate for both channels we’ll turn them on by using canBusOn function. All right, so when this device boots up it will set both channels to 500k and will turn both channels on.

Now, when we stop we want to make sure to shut both channels off as well, so we don’t get errors, so let’s do that. All right. So now when the device powers down it’ll shut down both channels and we won’t get any errors. So ultimately what we want this to do is receive a CAN message with any ID on channel 1 and then write that same message to CAN in channel 2. And same thing with channel 2 when we get a message of any ID we want to take that message and write it on channel 1. So let’s create a hook – an on CanMessage hook.

All right, and in between the greater than (>) and less than (<) symbols is going to be the channel that we want to target. So this channel CAN ID any, so asterisks says: hey, whatever CanMessage we have, we get any ID, then we want to invoke this function block here. So when we receive a CanMessage with any ID on CAN channel 1, we want to canWrite to channel 1, channel 2 – I’m sorry, this message and this is the message that we receive.

You can see that this is what’s called, a structure. We’ll get into this a little bit later. So, we’ve put dot ID, that would be the ID of the message. we’re gonna use this here in a printf statement. So printf is gonna help us out with determining whether or not we hit this block.

So let’s go ahead and just write a simple printf message in here. Says “We received a CAN message with ID of 0x%x”. All right, so this is an escape sequence. When we use the percentage sign we can insert a variable or we can say: hey, this.ID you want to see right here. Now, the X says that we want this variable to be printed as a hexadecimal character and we would use %d for decimal. We’ll do that here in just a second. So we received the CAN message with ID of hex this.ID on channel %d, this.id, this.channel +1 to put it into human readable context anyway saying that we were receiving it on CAN channel 1. So let’s do the same thing, I’m just gonna copy and paste.We’ll do the same thing when we receive any message on CAN channel 1 or CAN channel 2. So we receive any CAN message on CAN channel 2 we want to write it to CAN channel 1 and there’s our first t script. So let’s go ahead and save this script. And we’ll compile it. Now when you compile it you’ll see that we have an unsaved project so it will force us to create a new project when we compile it. So we’ll go ahead and save this project. And we have an error, let’s see what did we do wrong. Line 14 unexpected, colon (:) , expecting a semi-colon (;) . There we go. So, I made a mistake, the compiler tells me where the issue is, I can go in and fix it, recompile and we’re good.

Back to: Intro to t Programming > Intro to t Programming: Part 2