I'm trying to write to and read from serial port (arduino) using win32 api and C langauge. I want to send a number to the arduino, calculate the square there and read it by C program. But with the code below, either the program leaves the loop neither reads the incoming data. My C code:
#include <stdio.h>
#include <windows.h>
#include <string.h>
#define BUFFER_SIZE 256
int main(){
HANDLE hComm = INVALID_HANDLE_VALUE;
hComm = CreateFile("\\.\COM5", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(hComm == INVALID_HANDLE_VALUE){
printf("CreateFile() failed
");
return -1;
}else{
printf("CreateFile() succeeded
");
}
DCB dcb;
memset(&dcb, 0, sizeof(DCB));
if(!GetCommState(hComm, &dcb)){
printf("GetCommState() failed
");
return -1;
}
printf("## PLEASE SELECT FROM THE FOLLOWING ##
");
printf("## MENU : ##
");
printf("(1) TURN ON LED ON ARDUINO
");
printf("(2) TURN OFF LED ON ARDUINO
");
printf("(3) FLASH ARDUINO 3 TIMES
");
printf("(4) SEND A NUMBER TO ARDUINO TO COMPUTE SQUARE BY ARDUINO
");
printf("(5) Button press counter (bonus item) **not working**
");
printf("(0) EXIT
");
char writeBuff[BUFFER_SIZE];
char readBuff;
char buff[BUFFER_SIZE];
char intSend[6];
int choice;
int writtenBytes = 0;
int allReadBytes = 0;
int readByte = 0;
int i = 0;
char abc[10];
unsigned char loop = 0;
int a;
int index = 0;
do{
printf("PLEASE SELECT: ");
scanf("%d", &choice);
switch(choice){
case 1:
strcpy(writeBuff, "a");
if(WriteFile(hComm, writeBuff, 1, (LPDWORD)&writtenBytes, NULL)){
if(writtenBytes == 0){
printf("WriteFile() timed out
");
return -1;
}
}else{
printf("WriteFile() failed
");
return -1;
}
printf("%d bytes were written
", writtenBytes);
writtenBytes = 0;
break;
case 2:
strcpy(writeBuff, "b");
if(WriteFile(hComm, writeBuff, 1, (LPDWORD)&writtenBytes, NULL)){
if(writtenBytes == 0){
printf("WriteFile() timed out
");
return -1;
}
}else{
printf("WriteFile() failed
");
return -1;
}
printf("%d bytes were written
", writtenBytes);
writtenBytes = 0;
break;
case 3:
strcpy(writeBuff, "c");
if(WriteFile(hComm, writeBuff, 1, &writtenBytes, NULL)){
if(writtenBytes == 0){
printf("WriteFile() timed out
");
return -1;
}
}else{
printf("WriteFile() failed
");
return -1;
}
printf("%d bytes were written
", writtenBytes);
writtenBytes = 0;
break;
case 4:
printf_s("Please enter a number
");
scanf_s(" %s", &abc);
writeBuff[0] = abc[0];
writeBuff[1] = abc[1];
writeBuff[2] = abc[2];
WriteFile(hComm,
writeBuff,
sizeof(writeBuff),
&writtenBytes,
NULL);
do{
ReadFile(hComm, &readBuff, sizeof(readBuff), &readByte, NULL);
buff[loop]=readBuff;
++loop;
}while(readByte > 0);
--loop;
a = index;
for(index = a; index < loop; ++index){
printf_s("%c", buff[index]);
}
break;
}
}while(choice != 0);
if(!CloseHandle(hComm)){
printf("CloseHandle() failed
");
return -1;
}
return 0;
}
With debugger, I noticed that ReadFile reads what I send from C program and not what arduino sends to the serial port. Also I tested turning on led with, let's say, the number 12 and it works so I'm sure that I'm able to send data with WriteFile. My arduino code:
int i;
String value;
long sqCalc;
long karesi;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
if(Serial.available() > 0){
value = Serial.readString();
Serial.println(value);
if(value == "a"){
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("LED is ON");
delay(250);
}else if(value == "b"){
digitalWrite(LED_BUILTIN, LOW);
Serial.println("LED is OFF");
}else if(value == "c"){
if(digitalRead(LED_BUILTIN) == HIGH){
for(i=0;i<3;i++){
digitalWrite(LED_BUILTIN, LOW);
delay(250);
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
}
}
if(digitalRead(LED_BUILTIN) == LOW){
for(i=0;i<3;i++){
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
}
}
sqCalc = value.toInt();
if(sqCalc > 0){
karesi = pow(sqCalc, 2);
Serial.println(karesi, DEC);
}
}
}
Lastly, tested arduino code with PuTTy and it works fine.
question from:
https://stackoverflow.com/questions/65876136/problem-reading-from-serial-port-using-win32-api-c-langauge