Arduino Nano: Unusual serial device, bit arrays and more...

Hi people. It’s my first post here - hope the answer to my question would be helpful to all of you.

Here’s the situation… I have an Arduino Nano v3 (ATmega328) and a serial device which requires communication to be exactly 19200 baud UART, Even parity, 1 stop bit, 1 start bit. The device is controlled by changing bit values in array of 20 bytes.

So far I’m unable to make it respond to the code I ported from a desktop application on C++
Here are parts of the code that as I believe should work:

void setup()
{                
  Serial.begin(19200, SERIAL_8E1);
}

As I understand after uploading the code to Nano and disconnecting it from a computer pins 0 and 1 should be Serial interface. So this code should confiure those pins to interface properly with the device. (And yes I’ve connected proper external power source, connected rx-tx accordingly)

void sendMessage()
{
uint8_t *buff = new uint8_t[20];
    buff[0] = 0xB0;
    buff[1] = 0x3B;
    buff[2] = 0x55;
    buff[3] = 0x60;
    buff[4] = 0x00; 
    ...
    buff[18] = 0x00; 

    //checksum
    uint8_t cs = 0;
    
    for(int i=0;i<19;++i)
        cs += buff[i];

    buff[19] = cs;

    Serial.write(buff, 20);
    
    //clears
    delete []buff;
    buff = NULL;  
}

Structure is simple - first three bytes are headers, last is checksum others are various parameters. This is what worked perfectly on desktop. Well not exactly - there I used GCC compiler that understood 0b01101010 or used std::bitset. But I suppose that Arduino’s compiler is ok with HEX so what is written should be ok as well… But as you may guess it isn’t - I get 0 response.

So I will be very thankful if you could shed some light on the matter: maybe it is advised to use bitset struct and unions to write byte arrays into serial device?

Thanks in advance!

Does your device expect non-inverted TTL levels, or inverted RS232 levels?

Arduino is fine with binary notation.

Try this. The third write is just to show it's writing the right array elements.

void setup() {
uint8_t *buff = new uint8_t[20];
  buff[0] = 0x31;
  buff[1] = 0b00110001;
  buff[2] = 0x32;
  
  Serial.begin(115200, SERIAL_8E1);
  Serial.write(buff[0]);
  Serial.write(buff[1]);
  Serial.write(buff[2]);
}

void loop() {
}

I set up Teraterm for 8E1, and I get "112" sent from the Arduino, so I can't really see what might be the problem.

I do wonder about your line delete []buff; I don't think I've seen anything like that before.

Thanks I'll give it a try though i hardly see the difference between writing an array with known size and byte by byte writing... What is more if device doesn't receive checksum it won't respond...

About last line... That's how you delete an array, actually...

If you're deleting it without [] it only deletes the pointer (first array item as well) and skips the others = memory leak http://en.wikipedia.org/wiki/Delete_(C%2B%2B)

AWOL: Does your device expect non-inverted TTL levels, or inverted RS232 levels?

Good question I'm not sure... It's basic RS232 UART - can you point to a nice place where I can read?