Hi all!
I have made some progresses in writing my drivers.
First of all I have used a new PC equipped with a rs232 port and I do not use the serial to usb converter anymore.
At least now I'm able to communicate with my device! Anyway there are some points that are still cryptic to me.
The device is designed to not respond to all commands.
Here is the code I use to issue commands for which the device responds:
Code:
1. #include <termios.h>
2. #include <stdio.h>
3. #include <stdlib.h>
4. #include <unistd.h>
5. #include <fcntl.h>
6. #include <sys/signal.h>
7. #include <sys/stat.h>
8. #include <sys/types.h>
9. #include <string.h>
10.
11. #define BAUDRATE B9600
12. #define MODEMDEVICE "/dev/ttyS0"
13.
14. #define FALSE 0
15. #define TRUE 1
16.
17. volatile int STOP=FALSE;
18. void signal_handler_IO(int status);
19. int wait_flag=TRUE;
20.
21. int main()
22. {
23. int fd,c,res;
24. struct termios oldtio,newtio;
25. struct sigaction saio;
26. char buf[255];
27.
28. fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
29. if (fd <0) {perror(MODEMDEVICE); exit(-1); }
30.
31. saio.sa_handler=signal_handler_IO;
32. saio.sa_flags=0;
33. saio.sa_restorer=NULL;
34. sigaction(SIGIO,&saio,NULL);
35.
36. fcntl(fd,F_SETOWN,getpid());
37. fcntl(fd,F_SETFL,FASYNC);
38.
39. tcgetattr(fd,&oldtio);
40. bzero(&newtio,sizeof(newtio));
41. newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
42. newtio.c_iflag = IGNPAR | ICRNL;
43. newtio.c_oflag = 0;
44. newtio.c_lflag = ICANON;
45. newtio.c_cc[VMIN]=1;
46. newtio.c_cc[VTIME]=0;
47. tcflush(fd, TCIFLUSH);
48. tcsetattr(fd,TCSANOW,&newtio);
49.
50. printf("Send command SE\n");
51. if ((res=write(fd,"SE\r",3))!=3)
52. return -1;
53. printf("Done! Status=%d\n",res);
54. res=read(fd,buf,255);
55.
56. while (STOP==FALSE)
57. {
58. if (wait_flag==FALSE)
59. {
60. res=read(fd,buf,255);
61. buf[res]=0;
62. printf(":%s:%d\n",buf,res);
63. STOP=TRUE;
64. wait_flag=TRUE;
65. }
66. }
67.
68. tcsetattr(fd,TCSANOW,&oldtio);
69. return 0;
70. }
71.
72. void signal_handler_IO(int status)
73. {
74. printf("received SIGIO signal\n");
75. wait_flag=FALSE;
76. }
This code works but I cannot understand why it needs the line 54. More precisely without line 54 the values read at line 60 sometimes are correct and sometimes not. With the line 54 the values read at line 60 are always correct! Why?!?
Similar behavior is observed when I use commands to which the device does not respond at all:
Code:
...
printf("Send command ST 1\n");
if ((res=write(fd,"ST 1\r",5))!=5)
return -1;
printf("Done! Status=%d\n",res);
res=read(fd,buf,255);
...
Also in that case even if I don't have to read anything from the device I have to include a read() call to write the data correctly to the device! Why?!?
Thanks in advance for your help.
Bye