From dave at thefletchers.net Wed Feb 19 17:39:22 2020 From: dave at thefletchers.net (David Fletcher) Date: Wed, 19 Feb 2020 17:39:22 -0000 Subject: [Wiltshire] Any Arduino serial comms experts out there? Message-ID: <1582133958.4645.18.camel@boss> 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 From RobertCL at iname.com Wed Feb 19 17:56:06 2020 From: RobertCL at iname.com (Robert Longbottom) Date: Wed, 19 Feb 2020 17:56:06 -0000 Subject: [Wiltshire] Any Arduino serial comms experts out there? In-Reply-To: <1582133958.4645.18.camel@boss> References: <1582133958.4645.18.camel@boss> Message-ID: Some of the more recent UNOs (and maybe other Arduinos) reset when you connect to the serial port and I think maybe they reset when you disconnect as well.  It looks like the C program is closing the serial port when it exits, which will probably be resetting the Arduino and so it will briefly blink one of the LEDs and go back to the start state with the LED off. I've never tried to disable this behaviour, but it looks like it's possible.  A few links that google threw up: https://arduino.stackexchange.com/questions/38468/disable-reset-when-com-port-connected-disconnected This one suggests setting the flow control to None on the serial port may work (you might already be doing this in your C program, but I'm not familiar enough with C and serial ports to be sure): https://arduino.stackexchange.com/questions/439/why-does-starting-the-serial-monitor-restart-the-sketch It would make sense for it to be this reset on close thing though, if it doesn't do it when you leave the Arduino serial monitor open.  I guess you could also check by adding a delay before the close in you C program and you should see the LED come on and then go off again as the C program exists. Cheers, Rob. On 19/02/2020 17:39, David Fletcher via Wiltshire wrote: > 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 > > From dave at thefletchers.net Wed Feb 19 18:39:34 2020 From: dave at thefletchers.net (David Fletcher) Date: Wed, 19 Feb 2020 18:39:34 -0000 Subject: [Wiltshire] Any Arduino serial comms experts out there? In-Reply-To: References: <1582133958.4645.18.camel@boss> Message-ID: <1582137570.14616.11.camel@thefletchers.net> On Wed, 2020-02-19 at 17:56 +0000, Robert Longbottom via Wiltshire wrote: > Some of the more recent UNOs (and maybe other Arduinos) reset when > you > connect to the serial port and I think maybe they reset when you > disconnect as well.  It looks like the C program is closing the > serial > port when it exits, which will probably be resetting the Arduino and > so > it will briefly blink one of the LEDs and go back to the start state > with the LED off. > > I've never tried to disable this behaviour, but it looks like it's > possible.  A few links that google threw up: Hi Robert, It's great that the good old traditional engineering solutions still work - "Just stick a capacitor across the f'ing thing!". In this case the 100nF ceramic from RESET to GND appears to do the job. I can now turn the LED on and off at will from the desktop keyboard with the workbench completely shut down. I've not yet tried uploading a new sketch with the capacitor in place, maybe later. Thanks for the quick reply. BTW I'm using one of the £5 UNOs from eBay. Dave From dave at thefletchers.net Wed Feb 19 22:25:06 2020 From: dave at thefletchers.net (David Fletcher) Date: Wed, 19 Feb 2020 22:25:06 -0000 Subject: [Wiltshire] Any Arduino serial comms experts out there? In-Reply-To: References: <1582133958.4645.18.camel@boss> Message-ID: <1582151101.18182.23.camel@thefletchers.net> On Wed, 2020-02-19 at 17:56 +0000, Robert Longbottom via Wiltshire wrote: > Some of the more recent UNOs (and maybe other Arduinos) reset when > you > connect to the serial port and I think maybe they reset when you > disconnect as well.  It looks like the C program is closing the > serial > port when it exits, This is a little personal interest hobby project, to do a four terminal measurement of a Pt100 sensor hanging outside the back of my house where the sun can never shine on it, every 15 minutes as a cron task on a server. The C program starts up, gets some numbers from the arduino, does the maths, writes a file and terminates. At the moment I have it working with an Ethernet shield which I don't like very much hence I'm having a go at making it work with the USB connection instead. After that, try to rework the analogue electronics to use an instrumentation amplifier to improve the measurement resolution. > This one suggests setting the flow control to None on the serial port > may work (you might already be doing this in your C program, but I'm > not > familiar enough with C and serial ports to be sure): Looks like the C program code is intending to switch off all the control functions but it's evidently not enough to prevent the reset. I can easily enough plug the capacitor into the top header when in use. It prevents loading a new sketch into the Arduino. Dave