Serial strange behaviour

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
vebryn
Posts: 18
Joined: 16 Dec 2017 02:38

Serial strange behaviour

Post by vebryn »

Hello,

I suspect an issue using baudrate less than 9600. Let me explain some cases :

Case 1, 2 and 3: Z-Uno connected to PC using Serial

Code: Select all

int ByteReceived;

void setup(){
  Serial.begin(9600); 
}

void loop(){
  if (Serial.available() > 0){
    ByteReceived = Serial.read();     
    Serial.print(char(ByteReceived));
  }
}
Case 1: both Z-Uno and PC serial baudrate set to 9600
When I send Hello World, I get back Hello World. :arrow: OK

Case 2: Z-Uno serial set to 1200 and PC serial set to 1200
When I send Hello World, I get back Hello World. :arrow: OK

Case 3: Z-Uno serial set to 1200 and PC serial set to 9600
When I send Hello World, I get back Hello World. What ? I must not be able to read my message with a wrong baudrate.

Case 4, 5 and 6: Z-Uno connected to Sparkfun FDTI Basic (https://www.sparkfun.com/products/9873) using Serial0

Code: Select all

int ByteReceived;

void setup(){
  Serial0.begin(9600); 
}

void loop(){
  if (Serial0.available() > 0){
    ByteReceived = Serial0.read();     
    Serial0.print(char(ByteReceived));
  }
}
Case 4: both Z-Uno and Sparkfun FDTI serial set to 9600
When I send Hello World, I get back Hello World. :arrow: OK

Case 5: Serial connected to Sparkfun FDTI Basic https://www.sparkfun.com/products/9873, Z-Uno serial set to 1200 and Sparkfun FDTI serial set to 9600
When I send Hello World, I get back �����������. :arrow: OK

Case 6: Serial connected to Sparkfun FDTI Basic https://www.sparkfun.com/products/9873, Z-Uno serial set to 1200 and Sparkfun FDTI serial set to 1200
When I send Hello World, I get back �����������. What ? I must be able to read my message with a proper baudrate.

Case 3 and 6 are abnormal situations. Z-Uno seems not working well using a baudrate of 1200. I test a few other speed; 2400, 4800 and 19200. Both 2400 and 4800 are not working. 19200 is working fine. Is it related to oscillator frequency ? Too fast to cadence a speed below 9600 ?

Best regards.
vebryn
Posts: 18
Joined: 16 Dec 2017 02:38

Re: Serial strange behaviour

Post by vebryn »

argh! I just saw on Reference page that 9600 is the minimum speed accepted.
Sets the data rate in bits per second (baud) for serial data transmission. For USB baudrate is fixed on 115200 bps, on UART it is possible to select baudrate. Valid values are: 9600, 14400, 19200, 38400, 57600, 115200, 230400 bps
Any hope to support speed below 9600 :?: Arduino supports speed from 300 to 115200.
Is SoftwareSerial from Arduino compatible with Z-Uno :?:
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Serial strange behaviour

Post by PoltoS »

Z-Uno uses h/w UART from the Z-Wave chip. You can do s/w UART, but Arduino SoftwareSerial uses a lot of ASM instructions, hence not portable at all. You will have to adopt it for Z-Uno. For lower rates where speed is not that fast this should not be hard.
vebryn
Posts: 18
Joined: 16 Dec 2017 02:38

Re: Serial strange behaviour

Post by vebryn »

AltSoftSerial library seems nice : https://github.com/PaulStoffregen/AltSoftSerial.

However, it is based on an frequency timer (FTM) or an interrupt register (ICR).

What is the easy way to adapt this library ? Is FTM close to GPT ? Is there any ICR available on Z-UNO ?

Best regards.
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Serial strange behaviour

Post by PoltoS »

GPT is exactly what you need. Just make sure not to use this SoftSerial in higher bause rate (anyway useless, since Z-Way can handle it using h/w Serial).
vebryn
Posts: 18
Joined: 16 Dec 2017 02:38

Re: Serial strange behaviour

Post by vebryn »

I tried some sketchs. I think it's easier to code my own than to adapt existing code.

I connect rx on pins 16-17. First one is used as fast reading. Second one is used as falling detection.

A falling edge starts the gpt timer. The handler of the gpt timer reads 10 bits (start bit, 8 bits of data and stop bit). Gpt is set to 26 with flag ZUNO_GPT_SCALE1024.

This method is working fine when I send chars one by one. In my case, chars are sent continuously. Any ideas to detect start of each chars properly :?:
p0lyg0n1
Posts: 242
Joined: 04 Aug 2016 07:14

Re: Serial strange behaviour

Post by p0lyg0n1 »

Hi,
First of all I checked the documentation to chip and Z-Uno supports for UART0/UART1 only those baudrates:
Valid values:
9600  9.6kbaud
14400  14.4kbaud
19200  19.2kbaud
38400 38.4kbaud,
57600  57.6kbaud,
115200  115.2kbaud
230400  230.4kbaud
It's a hardware feauture...
Do you realy need baudrate < 9600?
Do you want to use it in duplex or for reading only?
Could you post a code with GPT timer? 1200KBits it's about 0.8 ms for one bit... It have to work in syncmode without a timer if a package is not big, we use this technique for 1Wire/DHT22/I2C(~100Kbauds). You can see the code of these libraries in the package folder of Z-Uno.
vebryn
Posts: 18
Joined: 16 Dec 2017 02:38

Re: Serial strange behaviour

Post by vebryn »

In France, our electricity provider send power consumption using serial at a speed of 1200. It sends data continuously.

I only want reading.

Thank you for syncmode implementation. I'm going to read about this mode into libs that you mentioned.

This code is working when I send char one by one :

Code: Select all

ZUNO_SETUP_ISR_INT0(onRxPinChange);
ZUNO_SETUP_ISR_GPTIMER(getCycleCount);

s_pin rxPin = 16;
byte rxBits;
byte rxNumBits;
bool rxEnable = true;

void setup() {
  // ZUNO_GPT_SCALE1024 — specify tick frequency as 32 MHz/1024 = 31.25 kHz (tick is 32 μs), otherwise default 32 MHz/8 = 4 MHz (tick is 0.25 μs)is used
  zunoGPTInit(ZUNO_GPT_SCALE1024|ZUNO_GPT_CYCLIC);
  
  // 26x32µs=832µs vs 1/1200=833.33µs
  zunoGPTSet(26);

  // use interruptions to detect start/stop bit
  zunoExtIntMode(ZUNO_EXT_INT0, CHANGE);
  
  // debug
  Serial.begin(9600);

  pinMode(rxPin, INPUT_PULLUP);
}

void loop() {
  // just to ensure alive status
  Serial.print('.');
  delay(1000);
}

void getCycleCount() {  
  // read bits
  if (rxNumBits < 8) {    
    // disable interruption
    rxEnable = false;
       
    // if high level, set MSB at 1L
    if (digitalRead(rxPin) != LOW){
      rxBits |= B10000000;
    }

    // shift bits
    rxBits = rxBits >> 1;

    // increase counter
    rxNumBits++;
  } else {
    // enable interruption detection after reading
    rxEnable = true;
  }
}

void onRxPinChange() {
  if (rxEnable){
    // start bit detection
    if (digitalRead(rxPin) == LOW){
      // prepare reading
      rxBits = 0;
      rxNumBits = 0;
      
      // enable reading
      zunoGPTEnable(1);
      
    // stop bit detection
    } else {
      // display my byte
      Serial.print(rxBits, BIN);
      Serial.print(' ');
      Serial.println(char(rxBits));

      // disable timer until next start bit
      zunoGPTEnable(0);
    }
  }
}

xibriz
Posts: 10
Joined: 27 Oct 2016 09:45

Re: Serial strange behaviour

Post by xibriz »

Hi. Any progress on your project?

I have somewhat the same problem, only my power meter sends data at 2400 baud every 10. second.
vebryn
Posts: 18
Joined: 16 Dec 2017 02:38

Re: Serial strange behaviour

Post by vebryn »

Hi,

I tried a lot of solutions. None are working properly. Best solution was to identify chars using bit mask. This solution was working but occuring too many reading errors.

risca, xibriz and me are looking for a solution. Please, implement a software serial or give us some tips.

Best regards.
Post Reply