NXC: converting a BT program to RS485

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: converting a BT program to RS485

Post 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?
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: converting a BT program to RS485

Post 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?
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: NXC: converting a BT program to RS485

Post 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
---> Link to lots of MINDSTORMS stuff under my picture --->
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: converting a BT program to RS485

Post by HaWe »

yes, I'm afraid so, too....
haydenstudios
Posts: 252
Joined: 22 Oct 2010, 20:05
Location: The United States of America
Contact:

Re: NXC: converting a BT program to RS485

Post 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. Image
-Hayden
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: converting a BT program to RS485

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: converting a BT program to RS485

Post 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.
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXC: converting a BT program to RS485

Post 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.
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: converting a BT program to RS485

Post 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?
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: NXC: converting a BT program to RS485

Post 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

Code: Select all

char RS485Write(byte buffer[])
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!
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming
http://www.mindstorms.rwth-aachen.de
MotorControl now also in Python, .net, and Mathematica
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 5 guests