Hello,
I wanted in the past to send some commands to a RAID5 box (promise
sx8000) over the serial port of a PC running linux..
This particular raid5 box wanted the characters of the command to be
sent one at a time with a delay in between.
the attached program is doing this...
I have tested the same prog to sent ATZ and stuff without delays
between the command characters to a serial modem and worked OK...
Are you sure that the modem gets the commands ?
IMHO the error is in the timeout calculations in the receive_data function
christos
------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
main ( int argc, char *argv[] ){
int fd; /*The file descriptor*/
char buffer[16384]; /*Input buffer */
int read_size; /*Buffer character counter */
if ( argc < 2 ){
printf("Usage is :\n");
printf("serial /dev/ttyS0 : for an SX8000 connected on COM1\n");
printf("serial /dev/ttyS1 : for an SX8000 connected on COM2\n");
printf("serial /dev/ttyS2 : for an SX8000 connected on COM3\n");
printf("serial /dev/ttyS3 : for an SX8000 connected on COM4\n");
exit(1);
}
struct termios options;
fd = open( argv[1], O_RDWR | O_NOCTTY | O_NDELAY );
if (fd == -1){
fprintf(stdout, "Unable to open port %s\n", argv[1]);
}
fcntl(fd, F_SETFL, 0);
/*set parameters fot the port*/
/*first read the status of the port*/
if (tcgetattr(fd, &options) < 0){
fprintf(stdout, "Can not get port atributes\n");
exit(-1);
}
/*set BAUD 8N1 */
options.c_cflag |= (CLOCAL | CREAD);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag = IGNPAR;
options.c_iflag = IGNCR;
/* options.c_cflag &= ~CRTSCTS; */
options.c_oflag = CR2;
/* push the values to the port */
if (tcsetattr(fd, TCSANOW, &options) < 0){
fprintf(stdout, "Can not set port attributes\n");
exit(1);
}
/* Write the command ld (list disks) to the SX8000 */
else write(fd, "l", 1);
usleep(100000);
write(fd, "d", 1);
usleep(100000);
write(fd, "\r", 1);
usleep(100000);
bzero(buffer, 16384); /* clear the input buffer */
fprintf(stdout, "Getting data from %s\n", argv[1]);
if (read_size = read (fd, buffer, 16384) > 0){
printf("%s\n", buffer );
usleep(100000);
close(fd);
}
return 0;
}
On 12/6/05, alipiec <> wrote:
> I have TS-MODEM2 additional board with MultiTech socket modem on it.
> The device is recognized at boot procedure. It's visible on
> /dev/tty/3. So until this moment everything looks fine. The board is
> responding - because I have RTC on it and date is backed up during
> power off time.
>
> I tried to send any command: "ATZ\r" and "AT\r" and "AT+WOPEN=1\r" and
> modem doesn't seems to respond. Serial settings for /dev/tty/3 are:
> baudrate 115200, 8N1. What's wrong? Modem should respond "OK". I got
> nothing.
> What could be wrong with following code?
>
> Alus
> ===========
>
> /*
> RS simple test
> Arkadiusz Lipiec - arkadiusz.lipiec AT NO_SPAM gazeta.pl
> */
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <unistd.h>
> #include <fcntl.h>
>
> #include <termios.h>
> #include <sys/ioctl.h>
> #include <sys/time.h>
>
> int fd; //! RS file descriptor
>
> int rs_open()
> {
> struct termios options;
>
> fd = open("/dev/tts/3", O_RDWR | O_NOCTTY | O_NONBLOCK);
>
> if(fd == -1)
> {
> perror("cannot open port\n");
> return fd;
> }
>
> fcntl(fd, F_SETFL, FASYNC);
>
> tcgetattr(fd, &options);
> bzero(&options, sizeof(options));
> options.c_cflag |= (CS8 | B115200 | CREAD | CLOCAL);
>
> // Enable data to be processed as raw input
> options.c_lflag &= ~(ICANON | ECHO | ISIG);
> options.c_iflag = IGNPAR;
> options.c_oflag = 0;
> tcflush(fd, TCIFLUSH);
>
> // Set the new options for the port
> tcsetattr(fd, TCSANOW, &options);
> return 0;
> }
>
>
> // timeout in ms...
> int receive_data(int fd, unsigned char* rcvbuffer, int buflen, int
> timeout)
> {
> unsigned long now;
> unsigned long diff;
> int i=0;
>
> int bytes;
> struct timeval tv;
> struct timezone tz;
>
> gettimeofday(&tv, &tz);
> now = tv.tv_sec * 1000 + (tv.tv_usec/1000);
>
> diff = 0;
> while(diff < (unsigned long) timeout)
> {
> gettimeofday(&tv, &tz);
> diff = ((tv.tv_sec * 1000) + (tv.tv_usec/1000) - now);
> ioctl(fd, FIONREAD, &bytes);
>
> if(bytes > 0 && i < buflen)
> {
> read(fd, rcvbuffer+i, bytes);
> i+=bytes;
> }
> usleep(1);
> }
>
> return i;
> }
>
>
> void rs_close()
> {
> if(fd != -1 && fd != 0)
> { close(fd); }
> }
>
>
> //-----------------------------------------------
> int main()
> {
> int i,j,k;
> unsigned char buf[512]; // buffer
>
> if(rs_open() != 0)
> {
> perror("COM problem\n");
> exit(EXIT_FAILURE);
> }
>
> j = sprintf(buf, "ATZ\r");
>
> k = write(fd, buf, j);
> memset(buf, 0, sizeof(buf));
> i = receive_data(fd, buf, sizeof(buf), 3000);
> if(i > 0)
> {
> printf("RESPONSE: %s\n", buf);
> }
>
> rs_close();
> return 0;
> }
>
>
>
>
>
>
>
>
>
>
>
> SPONSORED LINKS
> Computer internet security
> Linux os Computer internet
> business
> Computer internet access
> Computer internet help Single
> board
>
> ________________________________
YAHOO! GROUPS LINKS
>
>
> Visit your group "ts-7000" on the web.
>
> To unsubscribe from this group, send an email to:
>
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
>
> ________________________________
------------------------ Yahoo! Groups Sponsor --------------------~-->
Most low income households are not online. Help bridge the digital divide today!
http://us.click.yahoo.com/I258zB/QnQLAA/TtwFAA/CFFolB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|