Using Teensy 3.2
Windows 10 x64
Visual Studio 2019
I can send/receive data to Arduino's (1.8.13) serial monitor just fine using serial.read / serial.write ... etc.
Problem is when connecting to com port (3 in my case) using C language in VS2019. Almost all tutorials on this subject use the same methods (I'll post my code below) to make a connection. In my case when trying to establish connection with teensy, I get microsofts HANDLE error _INVALID_HANDLE_VALUE code 2 which means _FILE_NOT_FOUND, in other words can't find device on com port 3.
I'm not sure if its a windows issue or the teensy. If I run this code as HANDLE port = CreateFile("COM3", ) .... no connection is established, but if I run as CreateFileA or as (L"COM3")... then it acts like a port is established but see no data going through. Microsoft says to only use CreateFile because it's a macro that will make the correct actions based on the device.
I've literally tried copy/paste tutorial code examples (on both ends) to test with and get same results.
I'll place the C code and then the arduino code below.
ARDUINO CODE:
Windows 10 x64
Visual Studio 2019
I can send/receive data to Arduino's (1.8.13) serial monitor just fine using serial.read / serial.write ... etc.
Problem is when connecting to com port (3 in my case) using C language in VS2019. Almost all tutorials on this subject use the same methods (I'll post my code below) to make a connection. In my case when trying to establish connection with teensy, I get microsofts HANDLE error _INVALID_HANDLE_VALUE code 2 which means _FILE_NOT_FOUND, in other words can't find device on com port 3.
I'm not sure if its a windows issue or the teensy. If I run this code as HANDLE port = CreateFile("COM3", ) .... no connection is established, but if I run as CreateFileA or as (L"COM3")... then it acts like a port is established but see no data going through. Microsoft says to only use CreateFile because it's a macro that will make the correct actions based on the device.
I've literally tried copy/paste tutorial code examples (on both ends) to test with and get same results.
I'll place the C code and then the arduino code below.
Code:
BOOL* Status;
const char* ComPort = "\\\\.\\COM3";
HANDLE port = CreateFile(ComPort, //port name
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
NULL, // No Security
OPEN_EXISTING, // Open existing port only
0, // Non Overlapped I/O
NULL); // Null for Comm Devices
printf_s("port: %d\n", port);
if (port == INVALID_HANDLE_VALUE) {
//CHAR error = GetLastError;
printf_s("\nSerial port failed Reason: %d\n", GetLastError());
printf_s("%d", GetLastError());
}
else {
printf_s("\nSerial port successful\n");
}
DCB dcb = { 0 }; // Initializing DCB structure
dcb.DCBlength = sizeof(dcb);
Status = GetCommState(port, &dcb);
if (!GetCommState(port, &dcb)) {
printf_s("CSerialCommHelper: Failed to Get Comm State Reason: %d\n", GetLastError());
}
dcb.BaudRate = CBR_9600; // Setting BaudRate = 9600
dcb.ByteSize = 8; // Setting ByteSize = 8
dcb.StopBits = ONESTOPBIT; // Setting StopBits = 1
dcb.Parity = NOPARITY; // Setting Parity = None
Status = SetCommState(port, &dcb);
if (!SetCommState(port, &dcb)) {
printf_s("CSerialCommHelper: Failed to Set Comm State Reason: %d\n", GetLastError());
}
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50; // in milliseconds
timeouts.ReadTotalTimeoutConstant = 50; // in milliseconds
timeouts.ReadTotalTimeoutMultiplier = 10; // in milliseconds
timeouts.WriteTotalTimeoutConstant = 50; // in milliseconds
timeouts.WriteTotalTimeoutMultiplier = 10; // in milliseconds
int SerialBuffer = 0;
int BytesWritten = 0;
SerialBuffer = 123;
Status = WriteFile(port,
SerialBuffer,
sizeof(SerialBuffer),
&BytesWritten,
NULL);
printf_s("port: %d, SerialBuffer: %d, BytesWritten: %d\n",port, SerialBuffer, BytesWritten);
CloseHandle(port); //Closing the Serial Port
}
Sleep(1000);
}
ARDUINO CODE:
Code:
int inByte = 0; // incoming serial byte
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
while (Serial.available()) {
// get incoming byte:
inByte = Serial.read();
Serial.write("\nValue Read:\n",inByte);
}
if( inByte > 0){
led();
}
}
void led(){
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000);
}