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.