Go Down

Topic: disable auto-reset by serial connection (Read 16 times) previous topic - next topic

dcb

#10
Nov 20, 2008, 09:58 am Last Edit: Nov 20, 2008, 10:01 am by dcb Reason: 1
Also with just the right sized resistor as a jumper it is possible to prevent serial resets:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213719666/28#28

Zac

#11
Jun 18, 2010, 04:14 pm Last Edit: Jun 18, 2010, 04:15 pm by chmzc Reason: 1
Fantastic took me ages searching to find the solution but this worked thanks to : cacb for the post above:

My full C++ code to send 7 characters of user input to the board without resetting it was as follows:

Code: [Select]

// SensorWebPC.cpp
// Pass the port name in the command line - e.g. sensorwebpc comm2
// Original code from sensorwebpc
// code slightly modified by Z Coldrick 18-Jun-2010

#include "stdio.h"
#include <windows.h>
#include <iostream>

using namespace std;

void PrintError( LPCSTR str)
{
LPVOID lpMessageBuffer;
int error = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
(LPTSTR) &lpMessageBuffer,
0,
NULL
);
printf("%s: (%d) %s\n\n",str,error,lpMessageBuffer);
LocalFree( lpMessageBuffer );
}

int main(int argc, char* argv[])
{
// open port for I/O
HANDLE h = CreateFile("com4",
GENERIC_READ|GENERIC_WRITE,
0,NULL,
OPEN_EXISTING,0,NULL); // my adruino on com4 but this value can be read from argv

if(h == INVALID_HANDLE_VALUE) {
PrintError("E012_Failed to open port");
} else {
// set timeouts
COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
DCB dcb;
if(!SetCommTimeouts(h,&cto))
PrintError("E013_SetCommTimeouts failed");

// set DCB
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 9600;
dcb.fBinary = 1;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow = 1;
dcb.fRtsControl = DTR_CONTROL_HANDSHAKE;
dcb.fDtrControl = DTR_CONTROL_DISABLE; // avoid reset of arduino board
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;

if(!SetCommState(h,&dcb))
PrintError("E014_SetCommState failed");

char buf[8] = "";
cout << "Enter upto 7 characters to send : ";
cin.getline(buf, 8);

DWORD write = 8; // Number of bytes to write to serial port

WriteFile(h,buf,write,&write,NULL); // write is updated with the number of bytes written

DWORD i;
for (i=0; i<write; i++)
{cout << buf[i]; // repeat written chars back in console
}

CloseHandle(h);

}

return 0;
}



This C++ code can be easily compiled with the MinGw/ g++ compiler set to create a stand-alone console app.

(On windows OS)



Zac

Ok so now I have encountered a new problem:

My above code works fine to write to the arduino serial com port however I can seem to read from it:

I've tried adding the following code below before I close the handle but with no joy.


Code: [Select]


DWORD read = 0;
ReadFile(h,buf,sizeof(buf),&read,NULL); // read is updated with the number of bytes read
for (i=0; i<read; i++)
cout << (unsigned char)buf[i];



Any ideas?

Once I have a full solution I will post it on my webpage:

http://www.personal.leeds.ac.uk/~chmzc/


Yosserg

And for anyone using a Arduino that has physical serial connection like the Mini Pro or breadboard Arduino's you can make a connector bridge that doesn't include the DTR pin and it will work without resetting.

pluggy

The easiest way is to stick a larger value capacitor between gnd and the reset pin. I use a 10uF - works a treat.   It has the effect of 'swallowing' the small ground pulse transferred by the 0.1uF auto reset capacitor and preventing the board from resetting.  It avoids the current drain of holding it high with a resistor. too.

Go Up