Communicating via RS485

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
alphasucht
Posts: 10
Joined: 27 Dec 2011, 14:30

Communicating via RS485

Post by alphasucht »

Hello!

Is there any possibility that the communication via RS485 will be improved in one of the next firmware versions? Right now the way to communicate via cable is very complicated. It works only occasionally.
I tried different settings:
- 2 NXTs directly connected on port 4
- 3 NXTs connected per Mindsensors port splitter
- different delays while sending (0 - 250ms)
- different delays while receiving (0-50ms)
- different connection speed
But the result is always the same: to many messages get lost. Sometimes I have to send the same message 10 times till one of the slaves receives it. If I try to implement some error correction: "Master send message until the slave responds -> Master send token to the next NXT until it responds" I would loose so much time with every message, that the RS485 communication wouldn't be useful.

The communication between NXTs should be a helpful tool, not the main job on a project. ;)
A communication like the Bluetooth mailbox system would be a lot easier. I heard Lejos is very comfortable in that way, but I am afraid of Java.

The NXT Brick is getting cheaper and cheaper. Many of the users call 2 or more NXTs their own. I think the Brick to Brick communication is one of the most wanted NXC features. And via cable it is much more "real time" than Bluetooh.

I hope my englisch was understandable.

Thank You!

alphasucht
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Communicating via RS485

Post by afanofosc »

The only way I can help you fix the problems with the way you are trying to communicate between two NXTs via RS485 is if you show me your code.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
milluzaj
Posts: 31
Joined: 28 Sep 2010, 22:15

Re: Communicating via RS485

Post by milluzaj »

The RS485 blocks on my site use a handshake to ensure the message is sent. They then try to resend. However, sometimes it does fail completely...

I am not sure this is something to do in the FW, rather in the code layered on top. Maybe an RS485 library needs to be made for NXC with some of this message handling.
Blog: 08milluz.wordpress.com
LMS Shuttle: facebook.com/lmsshuttle
alphasucht
Posts: 10
Joined: 27 Dec 2011, 14:30

Re: Communicating via RS485

Post by alphasucht »

Okay, first: Ashes on my head! Before I tried to post my code, I compared your 485sendms and 485receivems with my code.
In my Sender Programm where no kind of this:

Code: Select all

 while(HSOutputBufferOutPtr() < HSOutputBufferInPtr())
    Wait(1);
and in my Receiver Programm where no kind of this:

Code: Select all

while (mlen == 0)
      mlen = HSInputBufferInPtr();
I noticed how important these 2 lines are. The situation is now much better, but there are still problems.

In 100 messages up to 5 messages will be lost. Also sometimes the message won't arrive successfully.

I check this with my code, but also with your example programms 485sendms and 485receivems. Same results!

If you activate the last "wait" command in the 485sendms and set it up to 500 or 1000ms, than you can see it with your own eyes. "Goofy" will be crippled! I think the receiver will read the string while the sender writes it!?

My Sender

Code: Select all

//Master Programm

byte laengenachricht;
string versenden, empfangen;
int i = 0;

sub SendeNachricht(string versenden)

{                          //Sendevorgang
    laengenachricht = ArrayLen(versenden);
    SetHSOutputBuffer(0, laengenachricht, versenden);
    SetHSOutputBufferOutPtr(0);
    SetHSOutputBufferInPtr(laengenachricht);
    SetHSState(HS_SEND_DATA);
    SetHSFlags(HS_UPDATE);
}

void WaitForMessageToBeSent()
{
  while(HSOutputBufferOutPtr() < HSOutputBufferInPtr())
    Wait(1);
}


task send()
{




     versenden += NumToStr(i);
     SendeNachricht(versenden);
     WaitForMessageToBeSent();
     i++;
     Wait(1);

}


task connect()
{

     SetSensorType(IN_4, SENSOR_TYPE_HIGHSPEED); //Verbindung zum zweiten NXT
     SetHSState(HS_INITIALISE);
     SetHSFlags(HS_UPDATE);
     SetHSInputBufferInPtr(0);



}



task main()
{
int laststate = 0;
versenden = "";
empfangen = "Nichts bekommen";

SetSensorTouch(IN_1);
start connect;


while(true)
{

 if(SENSOR_1 == 1)
   {
    versenden = "WASSERER";
    laststate = 1;
   }
else
    {
    versenden = "wasserer";
    laststate = 0;
    }
start send;
WaitForMessageToBeSent();
Wait(500);


}

}
My Receiver

Code: Select all

//Slave Programm

byte laengenachricht;
string versenden, empfangen;
int i = 0;





task connect()
{

     SetSensorType(IN_4, SENSOR_TYPE_HIGHSPEED); //Verbindung zum zweiten NXT
     SetHSState(HS_INITIALISE);
     SetHSFlags(HS_UPDATE);
     /
     SetHSInputBufferInPtr(0);


     while(true)
     {
     laengenachricht = 0;
     while (laengenachricht == 0)
           laengenachricht = HSInputBufferInPtr();

     GetHSInputBuffer(0, laengenachricht, empfangen); //empfangen der Nachricht
     Wait(2);
     SetHSInputBufferInPtr(0);
     Wait(1);
     TextOut(0, LCD_LINE2, empfangen);




     }
}



task main()
{
int laststate = 0;
versenden = "";
empfangen = "Nichts bekommen";

SetSensorTouch(IN_1);
start connect;

}
If you press the touch sensor, the the string will be changed to upper case. Sometimes just some of the letters will be changes. Same behavior like goofy.

I think it's practicable to intercept this errors inside the program. But maybe it is possible that the Firmware takes care of some of this problems? Some error correction or useful errorcodes. A stable concept of token based protocol. In most cases an unidirectional communication won't be useful.

With NXC have you done a great job! But maybe you would be able to improve it even more with this feature.

Thank you very much.

alphasucht
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Communicating via RS485

Post by afanofosc »

Can you show me where you got my code from? I have written several different versions of code for RS485 communication. Your code is using the oldest and lowest-level functions that you can use. Are you using the standard firmware or the enhanced NBC/NXC firmware? If you are using the enhanced firmware then you really ought to be using the newer higher-level API functions instead. I am nearly certain that these forums contain some examples that I have written using the higher level functions. My recollection was that with proper request/response code between the master and the slave that no messages were ever dropped. But I could be wrong.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Communicating via RS485

Post by afanofosc »

By the way, you should rewrite your code with task send as a subroutine, such as void send() and just call it via send(); rather than using start send; It is highly frowned upon to use start or stop keywords if you can instead use Precedes or Follows (which is always the case) and a task that ends immediately after a single pass through should always be called as a subroutine rather than started as a task.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
alphasucht
Posts: 10
Joined: 27 Dec 2011, 14:30

Re: Communicating via RS485

Post by alphasucht »

I got your code from http://bricxcc.sourceforge.net/nbc/. (2008/01/06)
I use the newest Enhanced Firmware. (lms_arm_nbcnxc_131_20111019_1659.rfw)

If there is a better way to communicate, than I would be happy to use it.

Yes, I know. My code is not really good. It's only my 3rd project. I will try to improve my skills.

If I try to use your 485Sendms without any changes, I can't compile the code.
"SendRS485Bool" - "SendRS485Number" - "SendRS485String" -> Error: Identifier expected. Should I have to include something?

Sorry for my poor question!

alphasucht
alphasucht
Posts: 10
Joined: 27 Dec 2011, 14:30

Re: Communicating via RS485

Post by alphasucht »

I found this thread https://sourceforge.net/apps/phpbb/mind ... ing#p10885 about my compiling problem. I've got the same check boxes activated like "mattallen37", but the compile process failes. What am I doing wrong?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Communicating via RS485

Post by afanofosc »

The code you are trying to compile was written before I added macros to NXCDefs.h with the same names as the functions defined in my old NXC code. That is why you are getting compiler errors. All of the SendRS485* functions in that old code should be commented out or deleted since these functions are now built into NXCDefs.h and are available for you to use in your program without needing to #include anything or to define them again. But, again, that is old low level should not be used anymore code.

You should definitely be running the latest 1.31 enhanced NBC/NXC firmware from the latest test release zip (http://bricxcc.sourceforge.net/test_releases/) and you should be using the newer higher-level API functions that are supported by the enhanced NBC/NXC firmware. See this post and all the subsequent posts in the same thread by me:

http://sourceforge.net/apps/phpbb/mindb ... 926&p=8945

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Communicating via RS485

Post by HaWe »

just curious:

what do these outcommented lines mean?

Code: Select all

//  // hi level API function call
//  RS485Initialize();
(similarily in sender and receiver),
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests