Doddy
Broken In
I am doing a hurry to complete a project in Linux, to read binary data from a meter or device through a serial port and write the data into a binary or text file. I really need the solutions fast.
How am I going to receive the BINARY data correctly from the serial port?
I encountered problem when I tried to read the data using the way I read ASCII data, but it don't work as binary data will make the ASCII value, out of range in the ASCII table and return me with a question mark, '?' instead.
I have no idea of how to change the code below to read in binary data or straight away write into a file. I know some people suggested to open the port as using a file handler or something similar and write the data read from the port straight into a file.
But as I am very new to Comms and Linux programming, how am I going to do that.
It is best if you can provide a workable solution.
Below is my subroutine of the code to read in ASCII data:
int read_reg()
{
int bytes_received, bytes_required;
char file_name[18];
bytes_received= 0;
bytes_required = 255; // For example i'm reading 255 bytes
sprintf(file_name, "Test.dat");
fa = fopen (file_name, "a");
while (bytes_received < bytes_requried)
{
fcntl(ttyfd, F_SETFL, FNDELAY);
rxchar = rxchar & 0xff;
recv[bytes_received] = rxchar;
bytes_received++;
fprintf(fa, "%0.2X", rxchar);
}
fclose(fa);
}
How am I going to receive the BINARY data correctly from the serial port?
I encountered problem when I tried to read the data using the way I read ASCII data, but it don't work as binary data will make the ASCII value, out of range in the ASCII table and return me with a question mark, '?' instead.
I have no idea of how to change the code below to read in binary data or straight away write into a file. I know some people suggested to open the port as using a file handler or something similar and write the data read from the port straight into a file.
But as I am very new to Comms and Linux programming, how am I going to do that.
It is best if you can provide a workable solution.
Below is my subroutine of the code to read in ASCII data:
int read_reg()
{
int bytes_received, bytes_required;
char file_name[18];
bytes_received= 0;
bytes_required = 255; // For example i'm reading 255 bytes
sprintf(file_name, "Test.dat");
fa = fopen (file_name, "a");
while (bytes_received < bytes_requried)
{
fcntl(ttyfd, F_SETFL, FNDELAY);
rxchar = rxchar & 0xff;
recv[bytes_received] = rxchar;
bytes_received++;
fprintf(fa, "%0.2X", rxchar);
}
fclose(fa);
}