Page 1 of 3
NXC: converting a BT program to RS485
Posted: 23 Jun 2011, 14:42
by HaWe
hi,
I'm using a BT program for 1 master and currently 2 (in future: 3) slaves.
It widely uses for the master:
Code: Select all
#define BT_CONN_1 1 // Slave 1
#define OUTBOX_1 1 //
#define INBOX_11 2 //
#define INBOX_12 3 //
#define BT_CONN_2 2 // Slave 2
#define OUTBOX_2 4 //
#define INBOX_21 5 //
#define INBOX_22 6 //
#define BT_CONN_3 3 // Slave 3
#define OUTBOX_3 7 //
#define INBOX_31 8 //
#define INBOX_32 9 //
SendRemoteString(OUTBOX, 1, cmd);
ReceiveRemoteString(INBOX_11, true, msg)
ReceiveRemoteNumber(INBOX, true, ack);
and for the slaves:
Code: Select all
#define INBOX 1 // for slave 1
#define OUTBOX_1 2
#define OUTBOX_2 3
#define INBOX 4 // for slave 2
#define OUTBOX_1 5
#define OUTBOX_2 6
#define INBOX 7 // for slave 3
#define OUTBOX_1 8
#define OUTBOX_2 9
SendResponseString(OUTBOX_1, out);
ReceiveRemoteString(INBOX, true, in);
Is there an easy way to convert them into RS485 cmds so that all master-slave-communications will work fine just as they did by BT?
Re: NXC: converting a BT program to RS485
Posted: 29 Jun 2011, 14:28
by HaWe
no one around here who is experienced in rs485 and who nows how to convert those bt mailbox functions for sending and receiving strings and numbers into rs485 functions?
Re: NXC: converting a BT program to RS485
Posted: 29 Jun 2011, 17:55
by hassenplug
doc-helmut wrote:no one around here who is experienced in rs485 and who nows how to convert those bt mailbox functions for sending and receiving strings and numbers into rs485 functions?
From what I've seen, very few people have used the rs485.
Steve
Re: NXC: converting a BT program to RS485
Posted: 29 Jun 2011, 18:05
by HaWe
yes, I'm afraid so, too....
Re: NXC: converting a BT program to RS485
Posted: 30 Jun 2011, 02:36
by haydenstudios
Oh man, this is such a Déjà vu. It's like
this is happening all over again... this time, in NXC, not NXT-G.
Re: NXC: converting a BT program to RS485
Posted: 30 Jun 2011, 04:06
by mattallen37
Well, I use RS485 a lot more than BT. Not that I use either very often, but when I do need NXT->NXT communication I always try to use RS485.
The times I do use BT, I only use one mailbox per NXT per direction. I don't use multiple mailboxes for multiple numbers. I always take the numbers I need to transfer, and put them into an array to flatten them to send as a string.
With RS485, I do the same thing I do for BT. I take the variables, put them into an array, and then flatten them to send as a string.
I don't do anything with multiple mailboxes, and I don't think the NXT has support for it with RS485.
If I were you, I would take a set of example programs (one for master and one for slave), and build from that. You should build a library of functions to support all you need.
I have an old library I can share if you're interested, but it doesn't do any data checking (like CRC or checksum), and I think it's limited to a fixed number of 10 user-bytes (it's been a while, so I'm not really sure about that). I also have a master and slave program that demonstrate functionality.
Maybe sometime when I have a couple hours I will attempt to make some RS485 functions that function similarly to the BT ones.
Re: NXC: converting a BT program to RS485
Posted: 30 Jun 2011, 06:28
by HaWe
I'm absolutely unexperienced in rs485 commands but if I start I will need to connect at least 3 NXT's to each other by a port splitter (the 4th will be still connected by BT).
Because the BT mailbox system is already established in my BT multiplexer program, I thought there must be an easy way to make sort of wrappers around the rs485 functions in order to get a user interface that works the same as the BT commands do addressing specific slaves one after the other).
IIRC Java uses this system in a similar way: passing a "0" to the remote commands they work as BT commands, passing a "1" they work for rs485.
I also remember a youtube video where 3 NXTs are connected by rs485 via bradboards or a port splitter and showing one NXT controlling outputs of the other 2 NXTs.
But the documentation of rs485 is too hard to understand for me to do that by my own without any help of other already experienced users, that's why I asked.
Re: NXC: converting a BT program to RS485
Posted: 30 Jun 2011, 10:36
by linusa
doc-helmut wrote:
Because the BT mailbox system is already established in my BT multiplexer program, I thought there must be an easy way to make sort of wrappers around the rs485 functions in order to get a user interface that works the same as the BT commands do addressing specific slaves one after the other).
The way mattallen suggested, you should have 1 (flattened) array/string to send. So you could pack your "virtual mailbox messages" together using TLV encoding (type length value). So put the mailbox number as byte, then the length of the mailbox message, and then the payload. On the receiver side unpack accordingly. Voila, wrapper-funtions. If you want "real" mailbox-like feeling, you'll have to cache the unpacked messages in in a queue.
Re: NXC: converting a BT program to RS485
Posted: 30 Jun 2011, 11:10
by HaWe
I'm not quite sure what you mean.
as I wrote, I already use these functions:
SendRemoteString(OUTBOX, 1, cmd);
ReceiveRemoteString(INBOX_11, true, msg)
ReceiveRemoteNumber(INBOX, true, ack);
SendResponseString(OUTBOX_1, out);
ReceiveRemoteString(INBOX, true, in);
These are the commands which have to be converted. The (varying) mailbox numbers are not about a feeling but about addressing the correct brick, they refer to BT connections.
The packing/unpacking of strings is already done by the corresponding master and slave programs, there is nothing more to pack or unpack.
Here is the complete BT source code:
https://sourceforge.net/apps/phpbb/mind ... plexer#p66
The BT msg strings are up to 67 bytes long (max. limited length), but as you might have discovered, there is also a ReceiveRemoteNumber cmd in use.
Could you show by a code example what exactly you meant how to convert them to rs485?
Re: NXC: converting a BT program to RS485
Posted: 30 Jun 2011, 13:24
by linusa
doc-helmut wrote:SendRemoteString(OUTBOX, 1, cmd);
The (varying) mailbox numbers are not about a feeling but about addressing the correct brick, they refer to BT connections.
No, you got your naming wrong. The function signature is
Code: Select all
char SendRemoteString (byte conn, byte queue, string str)
So your "OUTBOX" is the connection, and your 1 is the mailbox (outgoing I assume).
doc-helmut wrote:
The packing/unpacking of strings is already done by the corresponding master and slave programs, there is nothing more to pack or unpack.
If that was the case, you'd be already done. What you do with your mailbox messages doesn't matter. You need to pack/unpack them on a higher level.
doc-helmut wrote:
Could you show by a code example what exactly you meant how to convert them to rs485?
This is all based on
http://bricxcc.sourceforge.net/nbc/nxcd ... e.html#a17
The main function I focus on now is this one:
http://bricxcc.sourceforge.net/nbc/nxcd ... 6ebb6cf127
So you can send one buffer-array to a slave, very fast. Apparently you use hardware-multiplexing or whatever, so you probably broadcast this message to all slaves. So they need to unpack this buffer and decide which mailbox is for them.
Pseudocode:
Oh, while I was creating the rough guideline, I just noticed this:
http://bricxcc.sourceforge.net/nbc/nxcd ... ac541b4952
conn The connection slot (0..4). Connections 0 through 3 are for bluetooth connections. Connection 4 refers to the RS485 hi-speed port. See Remote connection constants.
Code: Select all
//Constants for specifying remote connection slots. More...
#define CONN_BT0 0x0
#define CONN_BT1 0x1
#define CONN_BT2 0x2
#define CONN_BT3 0x3
#define CONN_HS4 0x4
#define CONN_HS_ALL 0x4
#define CONN_HS_1 0x5
#define CONN_HS_2 0x6
#define CONN_HS_3 0x7
#define CONN_HS_4 0x8
#define CONN_HS_5 0x9
#define CONN_HS_6 0xa
#define CONN_HS_7 0xb
#define CONN_HS_8 0xc
So there already are individual highspeed-connections. Just use your existing
SendRemoteString command then with the right connection!