Helle all,
I'm planning to connect a TEENSY 3.2 or 5 to a PC.
Teensy is declared as SERIAL
Sytem configuration indicates COM16, but the CreateFile() function don't recognizes this port nor another port under WIN XP.
Under WIN 10, there is no COM port indicated by the system.
I'm using the USBDeview.exe for reading the available USB ports.
Thanks for your help !
-----------------------------------------------------------------------------------------------------------
SOURCE CODE
-----------------------------------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
//To cross-compile on Ubuntu:
//sudo apt-get install mingw32 mingw32-binutils mingw32-runtime
//i586-mingw32msvc-gcc -s -O3 -Wall -o serial_read.exe serial_read.c
const char *begin_string="10 second speed test begins";
const char *end_string="done!\r\n";
#define END_LEN 7
long i;
char data;
int numport;
char carsynchro = {'@'};
int main(void)
{
char bufferout[8] = {1,2,3,4,5,6,7,8};
HANDLE port;
COMMTIMEOUTS timeout;
COMMCONFIG cfg;
BOOL ret;
DWORD n,sum=0;
char com[16][32]={"COM16","COM1","COM2","COM3","COM4","COM5","COM6","COM7"};
// TODO: check if the user typed "COM##" where the number is
// greater than 9. If so, automatically reformat to \\.\COM##
// Open the serial port
for(numport = 0;numport<16;numport++)
{
port = CreateFile((char*)&com[numport][0], GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING, 0, NULL);
if (port == INVALID_HANDLE_VALUE)
{
exit(1);
}
else
goto porttrouve;
}
exit(900);
porttrouve:
// Configure the port
GetCommConfig(port, &cfg, &n);
cfg.dcb.BaudRate = 115200;
cfg.dcb.fBinary = TRUE;
cfg.dcb.fParity = FALSE;
cfg.dcb.fOutxCtsFlow = FALSE;
cfg.dcb.fOutxDsrFlow = FALSE;
cfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
cfg.dcb.fDsrSensitivity = FALSE;
cfg.dcb.fTXContinueOnXoff = TRUE;
cfg.dcb.fOutX = FALSE;
cfg.dcb.fInX = FALSE;
cfg.dcb.fErrorChar = FALSE;
cfg.dcb.fNull = FALSE;
cfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
cfg.dcb.fAbortOnError = FALSE;
cfg.dcb.XonLim = 0x8000;
cfg.dcb.XoffLim = 20;
cfg.dcb.ByteSize = 8;
cfg.dcb.Parity = NOPARITY;
cfg.dcb.StopBits = ONESTOPBIT;
SetCommConfig(port, &cfg, n);
GetCommTimeouts(port, &timeout);
timeout.ReadIntervalTimeout = 250;
timeout.ReadTotalTimeoutMultiplier = 1;
timeout.ReadTotalTimeoutConstant = 500;
timeout.WriteTotalTimeoutConstant = 2500;
timeout.WriteTotalTimeoutMultiplier = 1;
SetCommTimeouts(port, &timeout);
// WRITEdata until we get a timeout or error or see the end string
ret = WriteFile(port,bufferout,8,&n,NULL);
fflush(stdout);
CloseHandle(port);
return 0;
}
I'm planning to connect a TEENSY 3.2 or 5 to a PC.
Teensy is declared as SERIAL
Sytem configuration indicates COM16, but the CreateFile() function don't recognizes this port nor another port under WIN XP.
Under WIN 10, there is no COM port indicated by the system.
I'm using the USBDeview.exe for reading the available USB ports.
Thanks for your help !
-----------------------------------------------------------------------------------------------------------
SOURCE CODE
-----------------------------------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
//To cross-compile on Ubuntu:
//sudo apt-get install mingw32 mingw32-binutils mingw32-runtime
//i586-mingw32msvc-gcc -s -O3 -Wall -o serial_read.exe serial_read.c
const char *begin_string="10 second speed test begins";
const char *end_string="done!\r\n";
#define END_LEN 7
long i;
char data;
int numport;
char carsynchro = {'@'};
int main(void)
{
char bufferout[8] = {1,2,3,4,5,6,7,8};
HANDLE port;
COMMTIMEOUTS timeout;
COMMCONFIG cfg;
BOOL ret;
DWORD n,sum=0;
char com[16][32]={"COM16","COM1","COM2","COM3","COM4","COM5","COM6","COM7"};
// TODO: check if the user typed "COM##" where the number is
// greater than 9. If so, automatically reformat to \\.\COM##
// Open the serial port
for(numport = 0;numport<16;numport++)
{
port = CreateFile((char*)&com[numport][0], GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING, 0, NULL);
if (port == INVALID_HANDLE_VALUE)
{
exit(1);
}
else
goto porttrouve;
}
exit(900);
porttrouve:
// Configure the port
GetCommConfig(port, &cfg, &n);
cfg.dcb.BaudRate = 115200;
cfg.dcb.fBinary = TRUE;
cfg.dcb.fParity = FALSE;
cfg.dcb.fOutxCtsFlow = FALSE;
cfg.dcb.fOutxDsrFlow = FALSE;
cfg.dcb.fDtrControl = DTR_CONTROL_ENABLE;
cfg.dcb.fDsrSensitivity = FALSE;
cfg.dcb.fTXContinueOnXoff = TRUE;
cfg.dcb.fOutX = FALSE;
cfg.dcb.fInX = FALSE;
cfg.dcb.fErrorChar = FALSE;
cfg.dcb.fNull = FALSE;
cfg.dcb.fRtsControl = RTS_CONTROL_ENABLE;
cfg.dcb.fAbortOnError = FALSE;
cfg.dcb.XonLim = 0x8000;
cfg.dcb.XoffLim = 20;
cfg.dcb.ByteSize = 8;
cfg.dcb.Parity = NOPARITY;
cfg.dcb.StopBits = ONESTOPBIT;
SetCommConfig(port, &cfg, n);
GetCommTimeouts(port, &timeout);
timeout.ReadIntervalTimeout = 250;
timeout.ReadTotalTimeoutMultiplier = 1;
timeout.ReadTotalTimeoutConstant = 500;
timeout.WriteTotalTimeoutConstant = 2500;
timeout.WriteTotalTimeoutMultiplier = 1;
SetCommTimeouts(port, &timeout);
// WRITEdata until we get a timeout or error or see the end string
ret = WriteFile(port,bufferout,8,&n,NULL);
fflush(stdout);
CloseHandle(port);
return 0;
}