Results 1 to 3 of 3
I am currently working with the beagle board in which Linux is ported. I have written program in it so that It continuously reads the data coming to the serial ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 01-24-2012 #1Just Joined!
- Join Date
- Jan 2012
- Posts
- 2
problem with serial port
I am currently working with the beagle board in which Linux is ported. I have written program in it so that It continuously reads the data coming to the serial port.I send the requests(at every 1 second) to it by modbus tester(In another PC) but when it reads the requests, immediately it(beagleboard) sends the garbage bytes to the serial port.I don't write to the serial port or nothing at all do to write anything to the serial port(in beagle board) still it writes immediately after reading the requests.
I have attached a piece of code at beagle board side..(assuming all necessary declaration)
int main()
{
int num_read,i,j,l;
char read_buffer[11];
char *nums_read = &read_buffer;
char nums[] = {0x02,0x04,0x00,0x00,0x00,0x02,0x71,0xf8};
char *nums_write = &nums;
serial_port_open();
while(1)
{
signal (SIGINT, (void*)sigint_handler);
tcflush(serial_port,TCIOFLUSH);
// The main program loop:
for(l=0;l<10;l++)
{ for(i=0;i<65535;i++)
{
if (serial_port != -1)
{
num_read = serial_port_read(nums_read, 9);
if (num_read > 0)
{
goto pa;
}
}
}
}
pa:j=1;
}
}
int serial_port_open(void)
{
struct termios options;
serial_port = open(PORT_NAME, O_RDWR | O_NONBLOCK);
if (serial_port != -1)
{
printf("Serial Port open\n");
tcgetattr(serial_port,&options_original);
tcgetattr(serial_port, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag |= ICANON;
tcsetattr(serial_port, TCSANOW, &options);
}
else
printf("Unable to open /dev/ttyS2\n");
return (serial_port);
}
int serial_port_read(char *nums_read, size_t max_chars_to_read)
{
int i,l;
int chars_read = read(serial_port, nums_read, max_chars_to_read);
usleep(20000);
tcflush(serial_port,TCIOFLUSH);
return chars_read;
}
A part of the observation of free serial monitor is written here.Request is issued by the modbus tester and answer(undesired) is given by beagleboard.I don't know how it writes into the serial port...please help me work out this problem as soon as possible...
Request:
01 01 00 00 00 64 3D E1 01 01 00 00 00 64 3D E1 01 01 00 00 00 64 3D E1
Answer:
5E 41
Request:
01 01 00 00 00 64 3D E1 01 01 00 00 00 64 3D E1 01 01 00 00 00 64 3D E1 01 01 00 00 00 64 3D E1
Answer:
5E 41
Request:
01 01 00 00 00 64 3D E1
Answer:
5E 41
..........
- 01-28-2012 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,160
1) You need to terminate the nums[] variable with a NUL byte. IE, instead of this:
Do this:Code:char nums[] = {0x02,0x04,0x00,0x00,0x00,0x02,0x71,0xf8};
2) The variable nums_write can simple be eliminated, and use 'nums' instead.Code:char nums[] = {0x02,0x04,0x00,0x00,0x00,0x02,0x71,0xf8, 0x00};
3) You don't show the use of nums/nums_write, which is where the problem is likely to be caused.
Also, nums_write shoud be defined as &nums[0], NOT &nums...Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 01-28-2012 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,160
So, all in all, this code needs a SERIOUS re-working...
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
