472,989 Members | 2,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

Reading data from serial port

Hello everyone...

I have written a program to write and read data from serial port....
I am able to write 80(in binary)..and is expecting 1B but i am uable to read it...

My code goes as follows,,....

Expand|Select|Wrap|Line Numbers
  1.  
  2. int main()
  3.  
  4. {
  5.  
  6. int mainfd=0;
  7.  
  8.  
  9.  
  10.  
  11. mainfd = open_port();
  12. close(mainfd);
  13.  
  14. fd = open( "/dev/ttyS0" ,O_RDWR |O_NOCTTY | O_NDELAY);
  15. if (fd == -1) {
  16. perror("open_port: Unable to open /dev/ttyS0 - ");
  17. return 1;
  18. } else {
  19. fcntl(fd, F_SETFL,0);
  20. }
  21. /* Clear the line */
  22. tcflush(fd,TCIFLUSH);
  23.  
  24. initport(fd);
  25.  
  26.  
  27. char sCmd[1];
  28. sCmd[0] =0010;//0010;
  29. sCmd[1] =0000;//0000;
  30.  
  31. if (!writeport(fd, sCmd)) {
  32. printf("write failed\n");
  33. close(fd);
  34. return 1;
  35. }
  36.  
  37.  
  38. printf("written: %d%d\n", sCmd[0],sCmd[1]);
  39. close(fd);
  40.  
  41. usleep(30000);
  42. fd = 0;
  43. fd = open( "/dev/ttyS0" ,O_RDONLY |O_NOCTTY | O_NDELAY);
  44. char sResult[1];
  45. fcntl(fd, F_SETFL, FNDELAY); // don't block serial read
  46.  
  47. if (!readport(fd,sResult)) {
  48. printf("read failed\n");
  49. close(fd);
  50. return 1;
  51. }
  52. else
  53. close(fd);
  54. return 0;
  55. }
  56.  
  57.  
  58. the read write functions are as follows....
  59.  
  60.  
  61. int fd;
  62. char iIn[1];
  63.  
  64. int open_port (void)
  65. {
  66.  
  67.  
  68. int ser_cmd, ser_stat;
  69. ser_cmd = TIOCM_RTS;
  70.  
  71. fd = open("/dev/ttyS0", O_RDONLY); // Open the serial port.
  72.  
  73. if (fd == -1)
  74. {
  75. perror("open_port: Unable to open /dev/ttyS0 -%s\n "); 
  76. return 1; 
  77. } else{
  78. printf("Port 0 opened\n");
  79. fcntl(fd, F_SETFL, 0);
  80. }
  81.  
  82. // Read the RTS pin status.....
  83.  
  84. ioctl(fd, TIOCMGET, &ser_stat);
  85. if (ser_stat & TIOCM_RTS)
  86. {
  87. printf("RTS pin is set.\n");
  88. }
  89. else
  90. {
  91. printf("RTS pin is reset.\n");
  92. ioctl(fd, TIOCMBIC, &ser_cmd); // set the RTS pin.
  93. if (ser_stat & TIOCM_RTS)
  94. printf("RTS pin is set.\n");
  95. else
  96. printf("RTS pin is reset.\n");
  97.  
  98. }
  99. return (fd);
  100. }
  101.  
  102.  
  103.  
  104.  
  105. int writeport(int fd, char *chars) 
  106.  
  107. {
  108. int len =strlen(chars);
  109.  
  110. int n = write(fd, chars,strlen(chars));
  111. if (n < 0)
  112. {
  113. fputs("write failed!\n", stderr);
  114.  
  115. return 0;
  116. }
  117. fcntl(fd, F_SETFL,0);//changed
  118. return 1;
  119. }
  120.  
  121. int readport(int fd, char *result) {
  122. int iIn = read(fd, result, 1);
  123. printf("Value Received: %x\n",*result);
  124.  
  125.  
  126. return 1;
  127. }
  128.  
  129.  
  130. **************************************
  131. here it gives 40...
  132. ************************************
  133. int initport(int fd){
  134.  
  135. struct termios options;
  136. printf("Setting Parameters...\n");
  137. tcgetattr(fd, &options);
  138. cfsetispeed(&options, B9600); /* Set the baud rates to 9600 */
  139. cfsetospeed(&options, B9600);
  140.  
  141. /* Enable the receiver and set local mode */
  142. options.c_cflag |= (CLOCAL | CREAD);
  143. options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */
  144. options.c_cflag &= ~CSTOPB;
  145. options.c_cflag &= ~CSIZE;
  146. options.c_cflag |= CS8; /* Select 8 data bits */
  147. options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */ 
  148.  
  149. /* Enable data to be processed as raw input */
  150. options.c_lflag &= ~(ICANON | ECHO | ISIG);
  151.  
  152. options.c_cc[VMIN]=0;//it will wait for one byte at a time.
  153. options.c_cc[VTIME]=10;// it will wait for 0.1s at a time.
  154. /* Set the new options for the port */
  155. tcsetattr(fd, TCSANOW, &options);
  156. printf("Parameters SET!!!...\n");
  157. return 1;
  158. }
  159.  
Jan 12 '09 #1
6 8317
gpraghuram
1,275 Expert 1GB
Hi,
whenever you are reading froma serial port try to read in chunks.
After reading a certain chunk check whther you have read desired bytes or else read the remaining bytes.
Do this in a loop always.

Thanks
Raghu
Jan 13 '09 #2
okk but can u plz guide me how to do this...
can u suggest modifications for the same in my program...

thanks
in advance
Jan 13 '09 #3
gpraghuram
1,275 Expert 1GB
I have made some changes to the code . I wont say that this is perfectly right and this is only to make you understand the idea
Expand|Select|Wrap|Line Numbers
  1.  
  2. int readport(int fd, char *result) 
  3. //Have a while loop here
  4. int lnReadLength=0;
  5. while(lnReadLength <= 100)//I am assuming u want to read 100 bytes
  6. {
  7. int iIn = read(fd, result, 10); 
  8. lnReadLength += iln;
  9. result = (result+lnReadLength);
  10. }
  11. //printf("Value Received: %x\n",*result); 
  12. return 1; 
  13.  
Raghu
Jan 13 '09 #4
ya thank you very much...for ur help...

i hope u read my code i think i making a mistake in writing the data..as i mentioned that i have to send 80(hex value) to the serial port and then read..

i think the write function is not working and hence the read function...

the write function i am pasting again...........
can u plz have a look at it and confirm its correctness...

thanks

code as follows...

...
...
int main()
{

...
...
...

initport(fd);

int sCmd[1];

sCmd[0]=0200;

(this value is gives given in octal as we cannot give 80 in hex directly...
and serial port accepts only hex or octal values)



if (!writeport(fd, sCmd)) {
printf("write failed\n");
close(fd);
return 1;
}

printf("written: %x\n", sCmd[0]);
close(fd);

************************************************** ***********************
and this is the writeport function.....




int writeport(int fd, char *chars)

{
int len =strlen(chars);

int n = write(fd, chars,strlen(chars));
if (n < 0)
{
fputs("write failed!\n", stderr);

return 0;
}
fcntl(fd, F_SETFL,0);//changed
return 1;
}
Jan 13 '09 #5
gpraghuram
1,275 Expert 1GB
i think u shuld follow the same approach like writing in chunks and then return the status.

Thanks
Raghu
Jan 14 '09 #6
i have tried that but dnot know the program is not reading data from port...now it is writing correctly....

can u please guide me..

thanks
Jan 16 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: lec | last post by:
Hi, I'm trying to write a program to read from the serial port & write whatever that is read to the X console (/dev/tty7). For X to recognize the characters sent, I believe you have to send...
0
by: Kris | last post by:
try this free ActiveX control: http://ourworld.compuserve.com/homepages/richard_grier/NETC ommOCX.htm also you can buy ActiveX controls for serial communications from www.sax.net and other...
4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
5
by: Mo | last post by:
I am trying to set a text box value when data is received from the com port (barcode reader). I am getting the following error when I try to set the text box TXNumber after data is received ...
4
by: Petr Jakes | last post by:
I am trying to save data it is comming from the serial port continually for some period. (expect reading from serial port is 100% not a problem) Following is an example of the code I am trying to...
1
by: ss2000 | last post by:
I have a scale that attached to my pc, i created a vb program using mscomm it's not working, i copied several programs from internet and it's not working. do i need to buy another software that can...
3
by: Shark | last post by:
Hi, I need a help. My application reads data from COM port, this data is then parsed and displyed on: 1. two plotters 2. text box. I'm using Invoke method to update UI when new data is...
4
by: cmdolcet69 | last post by:
This code below write a serial command to the com port then read the results and disaplays it into the label1.text property when button1_click event is triggered. I think what is going on is that...
9
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
3
by: Sean | last post by:
I'm trying to read data from the Serial Port but I'm running in to a strange problem. The data parses correctly in a Console Application, but when I try to read the same way in a Windows Form...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.