문제

I am encountering a problem with my Arduino Uno. It is supposed to transmit data from a Novint Falcon to Dspace and receive sensor data from Dspace to be sent back to the Falcon. Dspace is programmed with Simulink.

My Arduino code runs like this:

  1. Read 6 bytes from laptop using Serial.
  2. Write 8 bytes to Dspace using SoftwareSerial.
  3. Read 3 bytes from Dspace using SoftwareSerial.
  4. Write those 3 bytes back to laptop.

In Dspace, the procedure is:

  1. Read 8 bytes from Arduino using Serial Receive.
  2. Send 3 bytes back to Arduino with Serial Transmit.

The problem I have now is, the Arduino hangs after about 1s when Dspace is turned on (on my laptop, I do actually see the 3 bytes read from Dspace). However, this problem does not occur when I only send ONE byte from Dspace. Anything more and it does not work.

The Baudrate of the serial ports is 115200. The Simulink model has a step time of 2ms.

It looks as though this has something to do with a buffer overflow in the Arduino, but I'm not sure. Can anybody please help me or offer suggestions as to how to pinpoint the root of the problem?? Been stuck on this for some time now.. thanks!

#include <SoftwareSerial.h>

char Axispos[6] ={0,0,0,0,0,0};

SoftwareSerial mySerial(7,6); //RX, TX

void setup() {

  Serial.begin(115200);
  Serial.flush();
  mySerial.begin(115200);
}

void loop(){

  if ( Serial.available() >= 6) 
  {
    char Fx=0;
    char Fy=0;
    char Fz=0;

    Serial.readBytes(Axispos,6);

    mySerial.write(33);
    mySerial.write(22);
    mySerial.write(Axispos);

    Fx = mySerial.read();
    Fy = mySerial.read();
    Fz = mySerial.read();

    Serial.write(Fx);
    Serial.write(Fy);
    Serial.write(Fz);
  }
}

C-code in simulink:

function [xPos, yPos, zPos, Force] = fcn(Axispos, NumRX)

persistent xPer yPer zPer count output;

Force=uint8(zeros(1,3));    
if isempty(xPer)
    xPer = int16(0);
    yPer = int16(0);
    zPer = int16(0);
    count = 0;
    output = uint8(0);
end

StartByte1 = char(Axispos(1));
StartByte2 = char(Axispos(2));
xh = uint8(Axispos(3));
xl = uint8(Axispos(4));
yh = uint8(Axispos(5));
yl = uint8(Axispos(6));
zh = uint8(Axispos(7));
zl = uint8(Axispos(8));

if (NumRX >=8 && StartByte1 == 33 && StartByte2 == 22)

xwert = uint16(bitshift(uint16(xh),8,16) + bitand(uint16(xl),uint16(255)));
ywert = uint16(bitshift(uint16(yh),8,16) + bitand(uint16(yl),uint16(255)));
zwert = uint16(bitshift(uint16(zh),8,16) + bitand(uint16(zl),uint16(255)));

var1 = typecast(uint16(xwert),'int16');
var2 = typecast(uint16(ywert),'int16');
var3 = typecast(uint16(zwert),'int16'); 

xPer = var1(1);
yPer = var2(1);
zPer = var3(1);

end

xPos = xPer;
yPos = yPer;
zPos = zPer;

Force(1) = uint8(10);
Force(2) = uint8(20);
Force(3) = uint8(30);
도움이 되었습니까?

해결책

Using simultaneous hardware and software serials on Arduino can actually cause issues.

The open source library AltSoftSerial has been written to overcome this problem.

From their main page :

AltSoftSerial is particularly useful when simultaneous data flows are needed

Then...

However, the maximum baud rate is often not the most important question. Each library imposes interrupt latency on other libraries. AltSoftSerial causes approximately 2-3 µs latency. NewSoftSerial causes 10 bit times of latency for other libraries. Running at 57600 baud, that's 174 µs! This latency is the primary difference between AltSoftSerial and NewSoftSerial.

To see this in action, you can try the example that comes with SoftwareSerial in Arduino 1.0. If you type "Goodnight" in the Arduino Serial Monitor, you'll see what actually comes out of pin 3 at 4800 baud is "Goot". The characters "dnigh" are lost. The reason is because while SoftwareSerial is sending the letter "G" at 4800, the letters "oodnigh" arrive at 57600 baud. Only "oo" are held in the UART registers. The rest are lost because interrupts were disabled for too long. AltSoftSerial can handle this test easily, since it does not lock out interrupts for long times.

I'm almost certain this is exactly the problem you're facing with your Arduino project. So I suggest you to use this library instead of the standard SoftwareSerial.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top