summaryrefslogtreecommitdiff
path: root/tools/scilab/windows/src/main.c
blob: d4877eb6e1571f8d98c369aa80a1fa6502eeabb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**************************************************

file: main.c
purpose: serial librairie for Scilab

Alain Caignot 
**************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

//#ifdef _WIN32
#include <Windows.h>
//#else
//#include <unistd.h>
//#endif

#define MAXPORTS 5 //unused : can be used to opened several COM to have several Arduino card

// Static definition to stock HANDLE of Port.
static HANDLE hport;

// Function to open port COM
__declspec(dllexport) void __stdcall open_serial(int *handle,int *port, int *baudrate, int *OK){
	DCB dcbSerialParams ;
	DWORD dwBytesWrite = 0;
	DWORD dwBytesRead = 10;
	COMMTIMEOUTS timeouts={0};

	char tmp[5]="COM5";
	itoa(*port,&tmp[3],10);
	
	*OK=0;
	

  	hport = CreateFile(tmp,
							GENERIC_READ | GENERIC_WRITE,
							0,//FILE_SHARE_READ | FILE_SHARE_WRITE //to test : recuperation COM port if simulation crashes
							0,
							OPEN_EXISTING,
							FILE_ATTRIBUTE_NORMAL,
							0);

	if(hport==INVALID_HANDLE_VALUE){
	  if(GetLastError()==ERROR_FILE_NOT_FOUND){
	     //serial port does not exist. Inform user.
	     *OK = GetLastError();
		 return;
	  }
	//some other error occurred. Inform user.
	  *OK = GetLastError();
	  return;
	}



	dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
	if (!GetCommState(hport, &dcbSerialParams)) {
	//error getting state
	     *OK = GetLastError();
		 return;
	}
	dcbSerialParams.BaudRate=*baudrate;
	dcbSerialParams.ByteSize=8;
	dcbSerialParams.StopBits=ONESTOPBIT;
	dcbSerialParams.Parity=NOPARITY;
	if(!SetCommState(hport, &dcbSerialParams)){
		//error setting serial port state
	    *OK = GetLastError();
		return;
	}
    

	timeouts.ReadIntervalTimeout=50;
	timeouts.ReadTotalTimeoutConstant=50;
	timeouts.ReadTotalTimeoutMultiplier=1;
	timeouts.WriteTotalTimeoutConstant=50;
	timeouts.WriteTotalTimeoutMultiplier=1;
	if(!SetCommTimeouts(hport, &timeouts)){
	//error occureed. Inform user
		*OK = GetLastError();
	    return;
	}
	Sleep(1000);
}


__declspec (dllexport) __stdcall void close_serial(int *handle, int *OK){
	int res;
	*OK=0;
	res=CloseHandle(hport);
	if (res==0)//error
		*OK = GetLastError();
}

__declspec (dllexport) __stdcall void write_serial(int *handle, char str[],int *size, int *OK){
	DWORD dwBytesWrite = 0;
	int res;
	*OK=0;
	res=WriteFile(hport,str,*size,&dwBytesWrite,NULL);

	if (res==0) //error
		*OK = GetLastError();

}

__declspec (dllexport) __stdcall void status_serial(int *handle, int *OK,int *nbread, int *nbwrite){
	DWORD dwErrorFlags;
	COMSTAT ComStat;
	int res;

	*OK=0;
	res=ClearCommError( hport, &dwErrorFlags, &ComStat );
	if (res==0) {//error
		*OK = GetLastError();
        return;
	}
	*nbread=ComStat.cbInQue;
	*nbwrite=ComStat.cbOutQue;
}

__declspec (dllexport) __stdcall void read_serial(int *handle,char buf[],int *size){
	DWORD dwBytesRead = 0;
	int res;

	//*OK=0;
	res=ReadFile(hport, buf, *size, &dwBytesRead, NULL);
	//if (res==0) {//error
	//	*OK = GetLastError();
    //    return;
	//}
}