[Wiltshire] Any Arduino serial comms experts out there?

David Fletcher dave at thefletchers.net
Wed Feb 19 17:39:22 UTC 2020


I'm trying to communicate between a Linux Mint 17 desktop and a UNO via
the USB.

With the following sketch loaded into the UNO:-
#define    LIGHTPIN    6
bool
    //    Flag to announce character received
    NewChar
;

char 
    InChar
;

void setup()
{
    NewChar = false;
    // initialize the digital pin as an output.
    pinMode(LIGHTPIN, OUTPUT); 
    // initialize serial:
    Serial.begin(9600);
}

void loop()
{
    // print the string when a newline arrives:
    if (true == NewChar)
    {
        if ('L' == InChar)
        {
            //    Make the LED Light
            digitalWrite(LIGHTPIN, HIGH);
        }
        if ('D' == InChar)
        {
            //    Make the LED Dark
            digitalWrite(LIGHTPIN, LOW);
        }
        NewChar = false;
    }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
*/

void serialEvent()
{
    if (Serial.available())
    {
        // get the new byte:
        InChar = (char)Serial.read(); 
        NewChar = true;
    }
}

I can use the serial monitor of the Arduino workbench to send L and D to
switch an LED on pin 6 on and off. OK so far, but ultimately I want to
send commands and receive data from a C programme on a headless server
so next I tried the example code from here:-
https://github.com/xanthium-enterprises/Serial-Port-Programming-on-Linux/blob/master/USB2SERIAL_Write/Transmitter%20(PC%20Side)/SerialPort_write.c
There's a lot more code here than is in the sketch so I've just included
the link. When I replace the "A" in write_buffer at line 104 with "L"
and "D" and recompile, it works but ONLY if the workbench serial monitor
is open. If not, all that happens is one of the LEDs on the UNO board
flickers a bit and the one I want to switch on/off always turns off, as
if the UNO is actually doing a reset.

Does anybody know what the magic incantation is, please? I've checked
what I think are the usual things i.e.
ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 Feb 19 17:32 /dev/ttyUSB0
and
groups
dave adm tty dialout sudo vboxusers




More information about the Wiltshire mailing list