I am trying to read serial data from my Duet2 board using the following code
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int main(int argc, char ** argv)
{
int fd;
struct termios SerialPortSettings;
// Open the Port. We want read/write, no "controlling tty" status, and open it no matter what state DCD is in
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
// setting baud rates
cfsetispeed(&SerialPortSettings,B115200);
cfsetospeed(&SerialPortSettings,B115200);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyACM0 - ");
return(-1);
}
fcntl(fd, F_SETFL, 0);
// Read up to 255 characters from the port if they are there
char buf[256];
while (1)
{
int k = read(fd, (void*)buf, 255);
if (k < 0)
{
perror("Read failed - ");
return -1;
}
else if (k == 0)
{
printf("No data on port\n");
}
else
{
printf("Read line is : %s \n",buf);
}
}
close(fd);
return 0;
}
The output that I expect looks something like :
N 0.500000 169.649002 108.239998 0.300000 0.000000 0.000000 0.000000 41926.273438 0.000000 41926.273438 83852 0.0000e+0 1.1926e-5 0.0000e+0 0 -0.500000
N 0.500000 169.649002 108.239998 0.300000 0.000000 0.000000 0.000000 41926.273438 0.000000 41926.273438 83852 0.0000e+0 1.1926e-5 0.0000e+0 0 -0.500000
N 6.769390 169.649002 108.239998 0.300000 5.300995 4.209999 0.000000 96530.593750 0.000000 96530.593750 193061 0.0000e+0 7.0127e-5 0.0000e+0 1 0.000000
N 0.500000 174.949997 112.449997 0.300000 0.000000 0.000000 0.000000 41926.273438 0.000000 41926.273438 83852 0.0000e+0 1.1926e-5 0.0000e+0 1 0.500000
N 15.100006 174.949997 112.449997 0.300000 15.100006 0.000000 0.000000 53037.558594 917231.625000 970269.187500 970269 0.0000e+0 1.6000e-5 1.6000e-5 1 1.265568
Warning: Tool 0 was not driven because its heater temperatures were not high enough or it has a heater fault
N 15.100006 190.050003 112.449997 0.300000 0.000000 15.100006 0.000000 0.000000 943750.375000 943750.375000 943750 1.6000e-5 1.6000e-5 1.6000e-5 1 1.265663
N 6.769390 169.649002 108.239998 0.300000 5.300995 4.209999 0.000000 96530.593750 0.000000 96530.593750 193061 0.0000e+0 7.0127e-5 0.0000e+0 1 0.000000
N 0.500000 174.949997 112.449997 0.300000 0.000000 0.000000 0.000000 41926.273438 0.000000 41926.273438 83852 0.0000e+0 1.1926e-5 0.0000e+0 1 0.500000
N 15.100006 174.949997 112.449997 0.300000 15.100006 0.000000 0.000000 53037.558594 917231.625000 970269.187500 970269 0.0000e+0 1.6000e-5 1.6000e-5 1 1.265568
Warning: Tool 0 was not driven because its heater temperatures were not high enough or it has a heater fault
N 15.100006 190.050003 112.449997 0.300000 0.000000 15.100006 0.000000 0.000000 943750.375000 943750.375000 943750 1.6000e-5 1.6000e-5 1.6000e-5 1 1.265663
i.e. a line starting with N
and followed by some numerical values and occasionally get some warnings about the heater.
However, the output I get is :
Read line is Warning: Tool 0 was not driven because its heater temperatures were not high enough or it has a heater fault
Read line is Warning: Tool 0 was not driven because its heater temperatures were not high enough or it has a heater fault
Read line is Warning: Tool 0 was not driven because its heater temperatures were not high enough or it has a heater fault
Read line is Warning: Tool 0 was not driven because its heater temperatures were not high enough or it has a heater fault
Read line is Warning: Tool 0 was not driven because its heater temperatures were not high enough or it has a heater fault
Read line is Warning: Tool 0 was not driven because its heater temperatures were not high enough or it has a heater fault
Read line is Warning: Tool 0 was not driven because its heater temperatures were not high enough or it has a heater fault
Why does my code choose to ignore the values I am interested in? I know the Duet board gives me the expected result on the serial port since I am able to read it using python however I would like to do it using C.
EDIT : The python code
import serial
import time
import subprocess
LINUX = 1
output = subprocess.check_output("python -m serial.tools.list_ports", shell=True) # sending command to terminal
output = output.decode('utf-8') # the output is of type b'somestring' This removes that
output = output.splitlines() # so we can access the COM part
if LINUX:
temp = output[0].split()
port = temp[0]
else:
port = output[0]
data = open("Input.txt", "w")
ser = serial.Serial(port, 115200, timeout=30)
print("Serial Port opened on : " + ser.name)
counter = 0
while (1):
response = ser.readline().decode('utf-8')
print(response)
data.write(response)
if response == "":
break
time.sleep(1)
data.close()