|
I did not see any code that initialises the serial port. So it will still use the current settings as shown by the stty command line tool. You have to use tcsetattr to select the same basic settings as on the PIC (probably 8N1; baud rate seems to mach already), disable all kinds of flow control (software and hardware), and all additional features like echoing.
Example:
memset(&config, 0, sizeof(config));
config.c_cflag = CS8 | CREAD | CLOCAL; cfsetospeed(&config, B9600); cfsetispeed(&config, B9600);
tcsetattr(hComPort, TCSANOW, &config); With new line terminated data transfers, you may also use the canonical mode (ICANON ).
|
|
|
|
|
Jochen Arndt wrote: I did not see any code that initialises the serial port.
There is, I was having trouble with the forum editor, thought maybe my code was too long.
The first part is here ..
class ComPort
{
public:
ComPort();
~ComPort();
void WriteComPortDataMsg(char *data, char num);
void ReadComPortDataMsg(char *data, char dlimc);
void ReadComPortDataMsg(char *data);
void SetDlimiter(char dlimc);
private:
char dlim;
struct termios config;
int hComPort;
bool Error;
};
ComPort::ComPort()
{
dlim = '\n';
char c;
Error = false;
hComPort = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (hComPort == -1)
{
cout << "failed to open port\n" << endl;
cout << "Hit <S> to Stop .." << endl;
cin >> c;
c = (char)toupper(c);
if (c == 'S') Error = true;
}
if(!isatty(hComPort))
{
cout << "file descriptor is not pointing to a TTY device\n" << endl;
cout << "Hit <S> to Stop .." << endl;
cin.get(c);
if ((c == 's' || c == 'S')) Error = true;
}
if(tcgetattr(hComPort, &config) < 0)
{
cout << "configuration of the serial interface not found\n" << endl;
cout << "Hit <S> to Stop .." << endl;
cin >> c;
c = (char)toupper(c);
if (c == 'S') Error = true;
}
config.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
config.c_oflag = 0;
config.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
config.c_cflag &= ~(CSIZE | PARENB);
config.c_cflag |= CS8;
if(cfsetispeed(&config, B9600) < 0 || cfsetospeed(&config, B9600) < 0)
{
cout << "Baud Rate did not set correctly\n" << endl;
cout << "Hit <S> to Stop .." << endl;
cin >> c;
c = (char)toupper(c);
if (c == 'S') Error = true;
}
if(tcsetattr(hComPort, TCSAFLUSH, &config) < 0)
{
cout << "Configuration set did not work\n" << endl;
cout << "Hit <S> to Stop .." << endl;
cin >> c;
c = (char)toupper(c);
if (c == 'S') Error = true;
}
}
ComPort::~ComPort() { close(hComPort); }
So, any ideas?
Thanks for the reply also!!
OdlG
|
|
|
|
|
Use the code from my post. It sets all while you are modifying the existing where something might be still set / cleared. CREAD is probably set but what about CLOCAL ?
|
|
|
|
|
Jochen Arndt wrote: CREAD is probably set but what about CLOCAL ?
I had the same code except .. memset
if(tcgetattr(hComPort, &config) < 0)
{
cout << "configuration of the serial interface not found\n" << endl;
cout << "Hit <S> to Stop .." << endl;
cin >> c;
c = (char)toupper(c);
if (c == 'S') Error = true;
}
memset(&config, 0, sizeof(config));
this is baudrate code ..
config.c_cflag = CS8 | CREAD | CLOCAL;
if(cfsetispeed(&config, B9600) < 0 || cfsetospeed(&config, B9600) < 0)
{
cout << "Baud Rate did not set correctly\n" << endl;
cout << "Hit <S> to Stop .." << endl;
cin >> c;
c = (char)toupper(c);
if (c == 'S') Error = true;
}
I get the same result as before.
OdlG
|
|
|
|
|
The source is probably here:
read(hComPort, &c, 1);
if ((c >= 0x20) && (c <= 0x7E)) data[cnt++] = c; See read(2): read from file descriptor - Linux man page[^]:
Quote: Return Value
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.
read() will return zero when there are no data available. So you have either to clear your c variable before, or - better - check the return value:
int received = read(hComPort, &c, 1);
if (received == 1 && c >= 0x20 && c <= 0x7E)
data[cnt++] = c;
else if (received < 0)
Another option is setting blocking mode (attributes c_cc[VMIN] to non zero and c_cc[VTIME] to the timeout value). Then you will not get zero return values but still have to check for negative return values indicating timeout or other errors.
|
|
|
|
|
I was beginning to think I needed some way to wait on the USB/Com Port, but wasn't sure how to do that.
This works great. My code change ..
void ComPort::ReadComPortDataMsg(char *data)
{
int cnt = 0;
int received;
char c;
data[0] = 0x00;
do
{
received = read(hComPort, &c, 1);
if ((received == 1) && (c >= 0x20) && (c <= 0x7E))
{
data[cnt++] = c;
}
else if (received < 0)
{
}
} while ((c != dlim) && (cnt < 255));
data[cnt++] = '\0';
}
Not sure how to deal with received < 0. Do you know where can I read up on
struct termios and
global errno variable
?
Thanks!!!
OdlG
|
|
|
|
|
|
Question on how to error handle.
I am falling into the received < 0 but if I just ignore it and keep looking for chars, I get a complete, correct message. Is it safe to do that or should I clear the error? And How?
Thanks in advance
OdlG
|
|
|
|
|
What is the error (errno ) code then?
|
|
|
|
|
Looks like it is 11
modified 14-Jul-18 16:04pm.
|
|
|
|
|
That is EGAIN (you can look it up in errno.h and get the corresponding error message with strerror(3) - Linux manual page[^] ) and in most cases (as here) not an error. You are just calling read again too fast (the next character has not been received so far).
To avoid this you can set a timeout value. Then the read() will block until data are available or a timeout occured.
|
|
|
|
|
hi,
Which do I use ide for learn c ++ ?.
|
|
|
|
|
|
vi (vim perhaps) on a Linux box.
IDEs are for sissies.
|
|
|
|
|
CPallini wrote: IDEs are for sissies.
So are displays and WYSIWYG editors. An ASR-33 and ed are all you need!
(And yes, I'm old enough to have actually written programs that way. )
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
I am young enough to continue using vim .
|
|
|
|
|
Hello, there's a code.
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
int main()
{
char* x = (char*)calloc(sizeof(char),150);
char* p = x;
free(x);
x = NULL;
.....
return 0;
}
<pre>
Tell me how to get NULL under p when x is removed and set to NULL.
|
|
|
|
|
You have to set it explicitly, there is no garbage collector or reference counts in C. The two pointers have no connection, so when you free x , pointer p still points to the original block of memory. You must be very careful to manage dynamic memory properly in C, and even in C++.
|
|
|
|
|
You have not a pointer to pointer. As Richard correctly pointed out, you have two independent pointers to the same block of memory; you have to explicitely set p = NULL; .
With a pointer to pointer the scenario would change:
char* x = (char*)calloc(sizeof(char),150);
char** p = &x;
free(x);
x = NULL;
printf("%p\n", *p);
|
|
|
|
|
CPallini wrote: *p is NULL But p still holds the address of x.
|
|
|
|
|
Of course. That's the purpose of 'pointer to pointer' (I suppose).
|
|
|
|
|
I thought it was just to confuse people with small brains (me).
|
|
|
|
|
Nope, you are simply drunk because of English team success.
|
|
|
|
|
What success, they lost 2:1 to South Africa. They need someone like this guy[^] to lead and inspire.
|
|
|
|
|
OMG, Rugby!
|
|
|
|