Sponsored Content
Full Discussion: Setup Serial Port
Top Forums Web Development Setup Serial Port Post 302927935 by Meow613 on Sunday 7th of December 2014 03:03:47 PM
Old 12-07-2014
OK
Here is what works.

It passes the tests of loss of power and loss of serial connection, and is not depedent on minicom.

Code:
/*
*Scale7  Write data to Serial Port and Read it
*
*/ 

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stropts.h>
#include <asm/termios.h>
#include <unistd.h>
#include <string.h>

#define MODEMDEVICE "/dev/ttyS0"


int main(void) 
{
    char buf[8];
    int pos=0, num, num2,num3;
    FILE *fp1, *fp2;   
    int i, n3;

    struct termios options;


    speed_t baud = B9600; /* baud rate */

    

    cfsetospeed(&options, baud); /* baud rate */


    options.c_cflag |= PARENB;  // Allow parity to be set
    options.c_cflag &= ~PARODD; // Even parity
    options.c_cflag &= ~CSTOPB; // 1 stop bit 
    options.c_cflag &= ~CSIZE;  // Mask the character size bits 
    options.c_cflag |= CS7;     // 7 bits
    tcflush(fp1, TCOFLUSH);
    
    system("touch /tmp/testfile.txt");  


    system("chmod 666 /tmp/testfile.txt");  //ensure that the permissions are 666
    
    fp1 = fopen(MODEMDEVICE, "w+");
    fp2 = fopen("/tmp/testfile.txt", "w+");

    if(fp2 == NULL)
    {
        printf("testfile error. \n");
            return 1;
    }

    
    if(fp1 == NULL)
    {
        printf("initiation error. \n");
            return 1;
    }

    tcgetattr(fp1, &options); //The tcgetattr function fills the termios structure 
    //you provide with the current serial port configuration. 
    //After we set the baud rates and enable local mode and serial data receipt, 
    //we select the new configuration using tcsetattr. The TCSANOW constant specifies 
    //that all changes should occur immediately without waiting for output data to finish 
    //sending or input data to finish receiving. There are other constants 
    //to wait for input and output to finish or to flush the input and output buffers.
    
    tcsetattr(fp1, TCSANOW, &options); /* apply the settings */
    
    
//printf("scale7");
    fprintf(fp1, "%s","W\r\n");
    fread(buf, 1, 7,fp1 );
    fprintf(stdout,"%s", buf);
//printf("\n%s", buf);

    fclose(fp1);
    fwrite(buf, 1, 7,fp2 );
    
    fclose(fp2);

    system("/usr/bin/php /var/www/testscale2.php"); // call a PHP script

}

Thanks to all
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Serial port communication

We're running SCO Unix Openserver 5.05 and I'm having trouble with serial communication between the com2 serial port and a handheld device. Downloading data from Unix to the handheld works perfectly, but the other way around creates a major problem. I don't know whether it's a buffer overflow or... (1 Reply)
Discussion started by: Aretha
1 Replies

2. UNIX for Dummies Questions & Answers

serial port configuration

I am having trouble with a device connected through a serial port on my sun workstation ( I am running solaris 7). The seriel device works on an identical workstation running the same o/s version but when I move it to the initial workstation it doesn't work. I am using an rs232 serial cable and... (1 Reply)
Discussion started by: Henrik
1 Replies

3. SCO

Serial port configuration

Hi I have just re-installed Compaq ML350 G2 Server with Unix SCO 5.0.5 and informix DBMS . on this server we have 2 serial ports , unfortunately its not working. Can anyone help me to check the serial ports working or not OR guide me to reconfigure it. I have attached a device with... (1 Reply)
Discussion started by: munirh
1 Replies

4. Solaris

Serial port not found

Hi, I am working with solaris 9,SUN-Blade-100 and I want to communicate with the Serial port.To check whether the port is working or not.I write code and when I execute the file,then I got the output-- According to this,serial port is not found.can anyone please tell me how to configure the... (1 Reply)
Discussion started by: smartgupta
1 Replies

5. AIX

Serial port in AIX

Hi, How can i configure my modem in AIX thru serial port (sa0-->tty0) I have two port serial card configured as sa0 I created tty1 which port is tty0 and which port is tty1 how can i know?? (1 Reply)
Discussion started by: pchangba
1 Replies

6. Solaris

Serial port on the T5140

What is the serial port on the T5140 used for. (4 Replies)
Discussion started by: pgsanders
4 Replies

7. Shell Programming and Scripting

Need help with serial port

Hi, I have a external board connected to my serial port. I need to execute "shutdown -r now" command when system boot up. When system boots up it requires a username ans password. Then I need to run my command. I can use rc script but that is rebooting system before it asks for username and... (0 Replies)
Discussion started by: charlie.arya
0 Replies

8. Solaris

How to enable Serial port on ILOM, when Network Port is enabled in parallel

Hi Everyone, In my environment, I have few T5220. On the iLOM Management Card, I have both Network and Serial port are cabled, I don't have any issues while I try to connect using Network Management port, but when I try to connect the serial port for the same server which is actually connected... (3 Replies)
Discussion started by: bobby320
3 Replies

9. Solaris

How to setup a serial console?

i am running solaris 10 in my vmware workstation. How to setup a console for my solaris box. in Vmware i could see a serial port option. can some one help me on how i can setup a console . (0 Replies)
Discussion started by: chidori
0 Replies

10. Solaris

Cabling and adapters to communicate to service processor serial port from Windows PC with USB port.

Hello, I have an unloaded T5140 machine and want to access the ILOM for the first time and subsequently the network port after that., and then load Solaris 10 the final January 2011 build. The first part is what confuses me -the cabling. I am coming from a Windows machine (w/appropriate... (5 Replies)
Discussion started by: joboy
5 Replies
TCSETATTR(3)						   BSD Library Functions Manual 					      TCSETATTR(3)

NAME
cfgetispeed, cfsetispeed, cfgetospeed, cfsetospeed, cfsetspeed, cfmakeraw, tcgetattr, tcsetattr -- manipulating the termios structure LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <termios.h> speed_t cfgetispeed(const struct termios *t); int cfsetispeed(struct termios *t, speed_t speed); speed_t cfgetospeed(const struct termios *t); int cfsetospeed(struct termios *t, speed_t speed); int cfsetspeed(struct termios *t, speed_t speed); void cfmakeraw(struct termios *t); int tcgetattr(int fd, struct termios *t); int tcsetattr(int fd, int action, const struct termios *t); DESCRIPTION
The cfmakeraw, tcgetattr and tcsetattr functions are provided for getting and setting the termios structure. The cfgetispeed, cfsetispeed, cfgetospeed, cfsetospeed and cfsetspeed functions are provided for getting and setting the baud rate values in the termios structure. The effects of the functions on the terminal as described below do not become effective, nor are all errors detected, until the tcsetattr function is called. Certain values for baud rates set in the termios structure and passed to tcsetattr have special meanings. These are discussed in the portion of the manual page that describes the tcsetattr function. GETTING AND SETTING THE BAUD RATE
The input and output baud rates are found in the termios structure. The unsigned integer speed_t is typdef'd in the include file <termios.h>. The value of the integer corresponds directly to the baud rate being represented, however, the following symbolic values are defined. #define B0 0 #define B50 50 #define B75 75 #define B110 110 #define B134 134 #define B150 150 #define B200 200 #define B300 300 #define B600 600 #define B1200 1200 #define B1800 1800 #define B2400 2400 #define B4800 4800 #define B9600 9600 #define B19200 19200 #define B38400 38400 #ifndef _POSIX_SOURCE #define EXTA 19200 #define EXTB 38400 #endif /*_POSIX_SOURCE */ The cfgetispeed function returns the input baud rate in the termios structure referenced by tp. The cfsetispeed function sets the input baud rate in the termios structure referenced by tp to speed. The cfgetospeed function returns the output baud rate in the termios structure referenced by tp. The cfsetospeed function sets the output baud rate in the termios structure referenced by tp to speed. The cfsetspeed function sets both the input and output baud rate in the termios structure referenced by tp to speed. Upon successful completion, the functions cfsetispeed, cfsetospeed, and cfsetspeed return a value of 0. Otherwise, a value of -1 is returned and the global variable errno is set to indicate the error. GETTING AND SETTING THE TERMIOS STATE
This section describes the functions that are used to control the general terminal interface. Unless otherwise noted for a specific command, these functions are restricted from use by background processes. Attempts to perform these operations shall cause the process group to be sent a SIGTTOU signal. If the calling process is blocking or ignoring SIGTTOU signals, the process is allowed to perform the operation and the SIGTTOU signal is not sent. In all the functions, although fd is an open file descriptor, the functions affect the underlying terminal file, not just the open file description associated with the particular file descriptor. The cfmakeraw function sets the flags stored in the termios structure (initialized by tcgetattr) to a state disabling all input and output processing, giving a ``raw I/O path''. It should be noted that there is no function to reverse this effect. This is because there are a variety of processing options that could be re-enabled and the correct method is for an application to snapshot the current terminal state using the function tcgetattr, setting raw mode with cfmakeraw and the subsequent tcsetattr, and then using another tcsetattr with the saved state to revert to the previous terminal state. The tcgetattr function copies the parameters associated with the terminal referenced by fd to the termios structure referenced by tp. This function is allowed from a background process, however, the terminal attributes may be subsequently changed by a foreground process. The tcsetattr function sets the parameters associated with the terminal from the termios structure referenced by tp. The action field is created by or'ing the following values, as specified in the include file <termios.h>. TCSANOW The change occurs immediately. TCSADRAIN The change occurs after all output written to fd has been transmitted to the terminal. This value of action should be used when changing parameters that affect output. TCSAFLUSH The change occurs after all output written to fd has been transmitted to the terminal. Additionally, any input that has been received but not read is discarded. TCSASOFT If this value is or'ed into the action value, the values of the c_cflag, c_ispeed, and c_ospeed fields are ignored. The 0 baud rate is used to terminate the connection. If 0 is specified as the output speed to the function tcsetattr, modem control will no longer be asserted on the terminal, disconnecting the terminal. If zero is specified as the input speed to the function tcsetattr, the input baud rate will be set to the same value as that specified by the output baud rate. RETURN VALUES
If tcsetattr is unable to make any of the requested changes, it returns -1 and sets errno. Otherwise, it makes all of the requested changes it can. If the specified input and output baud rates differ and are a combination that is not supported, neither baud rate is changed. Upon successful completion, the functions tcgetattr and tcsetattr return a value of 0. Otherwise, they return -1 and the global variable errno is set to indicate the error, as follows: [EBADF] The fd argument to tcgetattr or tcsetattr was not a valid file descriptor. [EINTR] The tcsetattr function was interrupted by a signal. [EINVAL] The action argument to the tcsetattr function was not valid, or an attempt was made to change an attribute represented in the termios structure to an unsupported value. [ENOTTY] The file associated with the fd argument to tcgetattr or tcsetattr is not a terminal. SEE ALSO
tcsendbreak(3), termios(4) STANDARDS
The cfgetispeed, cfsetispeed, cfgetospeed, cfsetospeed, tcgetattr and tcsetattr functions are expected to be compliant with the IEEE Std 1003.1-1988 (``POSIX.1'') specification. The cfmakeraw and cfsetspeed functions, as well as the TCSASOFT option to the tcsetattr function are extensions to the IEEE Std 1003.1-1988 (``POSIX.1'') specification. BSD
May 1, 2004 BSD
All times are GMT -4. The time now is 02:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy