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,,.... -
-
int main()
-
-
{
-
-
int mainfd=0;
-
-
-
-
-
mainfd = open_port();
-
close(mainfd);
-
-
fd = open( "/dev/ttyS0" ,O_RDWR |O_NOCTTY | O_NDELAY);
-
if (fd == -1) {
-
perror("open_port: Unable to open /dev/ttyS0 - ");
-
return 1;
-
} else {
-
fcntl(fd, F_SETFL,0);
-
}
-
/* Clear the line */
-
tcflush(fd,TCIFLUSH);
-
-
initport(fd);
-
-
-
char sCmd[1];
-
sCmd[0] =0010;//0010;
-
sCmd[1] =0000;//0000;
-
-
if (!writeport(fd, sCmd)) {
-
printf("write failed\n");
-
close(fd);
-
return 1;
-
}
-
-
-
printf("written: %d%d\n", sCmd[0],sCmd[1]);
-
close(fd);
-
-
usleep(30000);
-
fd = 0;
-
fd = open( "/dev/ttyS0" ,O_RDONLY |O_NOCTTY | O_NDELAY);
-
char sResult[1];
-
fcntl(fd, F_SETFL, FNDELAY); // don't block serial read
-
-
if (!readport(fd,sResult)) {
-
printf("read failed\n");
-
close(fd);
-
return 1;
-
}
-
else
-
close(fd);
-
return 0;
-
}
-
-
-
the read write functions are as follows....
-
-
-
int fd;
-
char iIn[1];
-
-
int open_port (void)
-
{
-
-
-
int ser_cmd, ser_stat;
-
ser_cmd = TIOCM_RTS;
-
-
fd = open("/dev/ttyS0", O_RDONLY); // Open the serial port.
-
-
if (fd == -1)
-
{
-
perror("open_port: Unable to open /dev/ttyS0 -%s\n ");
-
return 1;
-
} else{
-
printf("Port 0 opened\n");
-
fcntl(fd, F_SETFL, 0);
-
}
-
-
// Read the RTS pin status.....
-
-
ioctl(fd, TIOCMGET, &ser_stat);
-
if (ser_stat & TIOCM_RTS)
-
{
-
printf("RTS pin is set.\n");
-
}
-
else
-
{
-
printf("RTS pin is reset.\n");
-
ioctl(fd, TIOCMBIC, &ser_cmd); // set the RTS pin.
-
if (ser_stat & TIOCM_RTS)
-
printf("RTS pin is set.\n");
-
else
-
printf("RTS pin is reset.\n");
-
-
}
-
return (fd);
-
}
-
-
-
-
-
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;
-
}
-
-
int readport(int fd, char *result) {
-
int iIn = read(fd, result, 1);
-
printf("Value Received: %x\n",*result);
-
-
-
return 1;
-
}
-
-
-
**************************************
-
here it gives 40...
-
************************************
-
int initport(int fd){
-
-
struct termios options;
-
printf("Setting Parameters...\n");
-
tcgetattr(fd, &options);
-
cfsetispeed(&options, B9600); /* Set the baud rates to 9600 */
-
cfsetospeed(&options, B9600);
-
-
/* Enable the receiver and set local mode */
-
options.c_cflag |= (CLOCAL | CREAD);
-
options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */
-
options.c_cflag &= ~CSTOPB;
-
options.c_cflag &= ~CSIZE;
-
options.c_cflag |= CS8; /* Select 8 data bits */
-
options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */
-
-
/* Enable data to be processed as raw input */
-
options.c_lflag &= ~(ICANON | ECHO | ISIG);
-
-
options.c_cc[VMIN]=0;//it will wait for one byte at a time.
-
options.c_cc[VTIME]=10;// it will wait for 0.1s at a time.
-
/* Set the new options for the port */
-
tcsetattr(fd, TCSANOW, &options);
-
printf("Parameters SET!!!...\n");
-
return 1;
-
}
-
6 8445 gpraghuram 1,275
Recognized Expert Top Contributor
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
okk but can u plz guide me how to do this...
can u suggest modifications for the same in my program...
thanks
in advance
gpraghuram 1,275
Recognized Expert Top Contributor
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 -
-
int readport(int fd, char *result)
-
{
-
//Have a while loop here
-
int lnReadLength=0;
-
while(lnReadLength <= 100)//I am assuming u want to read 100 bytes
-
{
-
int iIn = read(fd, result, 10);
-
lnReadLength += iln;
-
result = (result+lnReadLength);
-
}
-
//printf("Value Received: %x\n",*result);
-
return 1;
-
}
-
Raghu
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(ch ars));
if (n < 0)
{
fputs("write failed!\n", stderr);
return 0;
}
fcntl(fd, F_SETFL,0);//changed
return 1;
}
gpraghuram 1,275
Recognized Expert Top Contributor
i think u shuld follow the same approach like writing in chunks and then return the status.
Thanks
Raghu
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
"scancodes".
Any suggestion is appreciated.
This is my attempt which doesn't work:
|
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 places.
>-----Original Message-----
>Perhaps I have looked in the wrong place, but I have not
>been able to located any code that allows me to read the
>serial port. Could anyone please point me in the right
|
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. What I would like to do is make
the serial port accessible via a web service.
The web service and the legacy application would be running on the same
machine. The mobile application would access the web service via a network
connection. It...
|
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
Cross-thread operation not valid: Control 'TXNumber' accessed from a
thread other than the thread it was created on.
Any ideas how to work around this problem?
Thanks
|
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 write. It works, but
it produce an empty gz file (0kB size) even I am sure I am getting data
from the serial port. It looks like g.close() does not close the gz
file.
I was reading in the doc:
Calling a GzipFile object's close() method does...
| |
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 read the serial port. how can i make the program work. HELP. please send the answer to my email address
|
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 received (through
delegate).
|
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 the
ocp.read is on a timer and reads char by char. How can i change this
code so that the read reads the whole result and not char by char?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
|
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, "That's easy, there's a lot
out there, Google it,", 1 is a discussion on it without examples and the
other is who knows what.
I did find some info on it and have been experimenting. The one example
that I liked the best in terms of...
|
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 Application it
drops data randomly.
In the windows form application I read data using the following code:
public Form1()
{
InitializeComponent();
serialPort1.Open();
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |