Skip to content

Commit 8d2fea2

Browse files
committed
Added support for 64 bit linux. Implemented in C instead of C++
1 parent 94a4fd0 commit 8d2fea2

12 files changed

+21
-15
lines changed

c_serial.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
/*
1919
* Platform-specific definitions
2020
*/
21+
#include <mex.h>
2122
#ifdef _WIN32
2223

2324
#include <windows.h>
24-
#include <mex.h>
25+
2526
#define __func__ __FUNCTION__
2627

2728
#define close( x ) CloseHandle( x )
@@ -344,7 +345,11 @@ static int set_baud_rate( c_serial_port_t* desc, int baud_rate ) {
344345
SPEED_SWITCH(19200,newio);
345346
SPEED_SWITCH(38400,newio);
346347
SPEED_SWITCH(115200,newio);
348+
#ifdef _WIN32
347349
case 921600: newio.BaudRate = 921600; break;
350+
#else
351+
case 921600: cfsetospeed( &newio, B921600 ); cfsetispeed( &newio, B921600 ); break;
352+
#endif
348353
}
349354

350355
SET_SERIAL_PORT_STRUCT( desc, newio );

closePort.cpp renamed to closePort.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
1010
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Please send serial port object.");
1111
}
1212
c_serial_port_t* m_port;
13-
m_port = reinterpret_cast<c_serial_port_t*>(*((uint64_t *)mxGetData(prhs[0])));
13+
m_port = (c_serial_port_t*)(*((uint64_t *)mxGetData(prhs[0])));
1414
c_serial_free(m_port);
1515
}
1616

closePort.mexa64

28.1 KB
Binary file not shown.

example.m

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
%Run these mex commands to build the mex functions before executing this
22
%example:
33
%
4-
% mex getPortNames.cpp c_serial.c;
5-
% mex openPort.cpp c_serial.c;
6-
% mex writePort.cpp c_serial.c;
7-
% mex readPort.cpp c_serial.c;
8-
% mex closePort.cpp c_serial.c;
4+
% mex getPortNames.c c_serial.c;
5+
% mex openPort.c c_serial.c;
6+
% mex writePort.c c_serial.c;
7+
% mex readPort.c c_serial.c;
8+
% mex closePort.c c_serial.c;
99

1010
%Need to put n arguments for n ports available you wish to find... probably a cleaner way to
1111
%do this but oh well.
12-
[name1, name2, name3] = getPortNames;
13-
12+
[name1, name2, name3,name4,name5,name6] = getPortNames;
1413
%Currently only works with standard baud rate i.e. 9600, 23400, 115200,
1514
%921600 (note I added 921600 for windows only by editing c_serial.h and c_Serial.c so it is
1615
%possible to add more with a bit of editing)
@@ -22,7 +21,7 @@
2221
portPointer = openPort(name1,baudrate);
2322

2423
%String we will print out to serial port
25-
string = '$ Hello World, hope you are well $';
24+
string = '$Hello World, hope you are well and happily happy$';
2625

2726
%Must convert to uint8 before sending out data
2827
dataOut = uint8(string);
File renamed without changes.

getPortNames.mexa64

24.1 KB
Binary file not shown.

openPort.cpp renamed to openPort.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ static c_serial_port_t* m_port;
77
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
88
{
99
int status;
10-
char portName[6];
10+
char portName[255];
1111
mxGetString(prhs[0], portName, sizeof(portName));
12+
double *baudRate;
13+
baudRate = mxGetPr(prhs[1]);
1214
if( c_serial_new( &m_port, NULL ) < 0 ){
1315
mexPrintf("ERROR: Unable to create new serial port\n" );
1416
return;
@@ -24,7 +26,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
2426
// /*
2527
// * Set various serial port settings. These are the default.
2628
// */
27-
c_serial_set_baud_rate( m_port, CSERIAL_BAUD_921600 );
29+
c_serial_set_baud_rate( m_port,((int) baudRate[0]));
2830
c_serial_set_data_bits( m_port, CSERIAL_BITS_8 );
2931
c_serial_set_stop_bits( m_port, CSERIAL_STOP_BITS_1 );
3032
c_serial_set_parity( m_port, CSERIAL_PARITY_NONE );
@@ -41,7 +43,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
4143
return;
4244
}
4345
mxArray *out = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
44-
*((uint64_t *)mxGetData(out)) = reinterpret_cast<uint64_t>(m_port);
46+
*((uint64_t *)mxGetData(out)) = (uint64_t)(m_port);
4547
plhs[0] = out;
4648
}
4749

openPort.mexa64

28.3 KB
Binary file not shown.

readPort.cpp renamed to readPort.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
2222
data_length = (int) numBytesToRead[0];
2323
uint8_t data[255];
2424

25-
m_port = reinterpret_cast<c_serial_port_t*>(*((uint64_t *)mxGetData(prhs[0])));
25+
m_port = (c_serial_port_t*)(*((uint64_t *)mxGetData(prhs[0])));
2626
int iii;
2727

2828

readPort.mexa64

28.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)