OpenCores
URL https://opencores.org/ocsvn/openverifla/openverifla/trunk

Subversion Repositories openverifla

[/] [openverifla/] [trunk/] [openverifla_2.4/] [java/] [UARTSendReceive.java] - Blame information for rev 46

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 46 laurentiud
/*
2
UARTSendReceive.java
3
License: GNU GPL
4
 
5
Revision history:
6
revistion date: 2018/07/20; author: Laurentiu Duca
7
- port of SerialPort jssc instead of rxtx
8
revision date: 2007/Sep/03;  author: Laurentiu Duca
9
- captureOnly feature
10
- consider that the bt_queue_head_address is wrote at the end of the data capture.
11
- use HOUR_OF_DAY (0..23)
12
 
13
revision date: 2007/Jul/4; author: Laurentiu DUCA
14
- v01
15
*/
16
 
17
 
18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.io.OutputStream;
21
import java.io.File;
22
import java.io.FileInputStream;
23
import java.io.FileOutputStream;
24
import java.io.FileNotFoundException;
25
import java.io.FileReader;
26
import java.io.BufferedReader;
27
import java.util.Properties;
28
import java.util.Enumeration;
29
import java.util.Calendar;
30
import java.util.GregorianCalendar;
31
import java.util.StringTokenizer;
32
 
33
import jssc.SerialPort;
34
import jssc.SerialPortException;
35
 
36
public class UARTSendReceive extends Object {
37
 
38
        // Data members are declared at the end.
39
 
40
        /**
41
         * Creates a new object.
42
         *
43
         */
44
        public UARTSendReceive() {
45
                this.serialPort = null;
46
                //this.properties = new Properties();
47
        }
48
 
49
        /**
50
         * Attaches the given serial serialPort to the device object.
51
         * The method will try to open the serialPort.
52
         */
53
        public boolean attach(String portName, String strBaudRate, String parity) {
54
                serialPort = new SerialPort(portName);
55
 
56
                byte pb[]=parity.getBytes();
57
                int b=pb[0]=='1'?SerialPort.PARITY_ODD:SerialPort.PARITY_NONE;
58
                System.out.println("parity="+b);
59
                try {
60
                        serialPort.openPort();//Open serial port
61
                        int baudrate=Integer.parseInt(strBaudRate);
62
                                //strBaudRate.equals("115200")?SerialPort.BAUDRATE_115200:
63
                                //strBaudRate.equals("38400")?SerialPort.BAUDRATE_38400:SerialPort.BAUDRATE_9600;
64
                        serialPort.setParams(baudrate,
65
                             SerialPort.DATABITS_8,
66
                             SerialPort.STOPBITS_1,
67
                             b); //SerialPort.PARITY_NONE);
68
                        //Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);
69
                        //serialPort.writeBytes("This is a test string".getBytes());//Write data to port
70
                } catch (SerialPortException ex) {
71
                        ex.printStackTrace(System.out);
72
                        return false;
73
                }
74
 
75
                return true;
76
        }
77
 
78
        /**
79
         * Detaches the currently attached serialPort, if one exists.
80
         * This will close the serial port.
81
         *
82
         */
83
        public void detach() {
84
                if (serialPort != null) {
85
                        try {
86
                                serialPort.closePort();
87
                        } catch (SerialPortException ex) {
88
                                ex.printStackTrace(System.out);
89
                        }
90
                }
91
        }
92
 
93
        public void sendReceive(String msg) throws IOException, SerialPortException {
94
                byte rawByte[]=new byte[1];
95
                rawByte = msg.getBytes();
96
 
97
                System.out.println("Sending...");
98
                serialPort.writeBytes(rawByte);
99
                System.out.println("Done sending.");
100
 
101
                // Read Captured data
102
                System.out.println("Reading");
103
                byte readByte[] = serialPort.readBytes(1);
104
                System.out.printf("Read: '%c'=0x%x\n", (char) readByte[0], (int) readByte[0]);
105
        }
106
 
107
        public void getCapturedData(String portName, String strBaudRate, String parity, String msg)
108
        {
109
                boolean found;
110
                found = attach(portName, strBaudRate, parity);
111
                if(!found) {
112
                        System.out.println("Port " + portName + " not found.\n");
113
                        System.exit(0);
114
                }
115
                try {
116
                        sendReceive(msg);
117
                } catch (Exception ex) {
118
                        ex.printStackTrace(System.out);
119
                }
120
                detach();
121
        }
122
 
123
 
124
        public static void fatalError(String errorName)
125
        {
126
                System.out.println("Fatal error: " + errorName);
127
                System.exit(-1);
128
        }
129
 
130
 
131
        public static void main(String[] args) throws Exception
132
        {
133
                if(args.length != 4)
134
                        UARTSendReceive.fatalError("Number of arguments is not 4; is "+args.length+"\n"+
135
                                "Sintax is:\njava UARTSendReceive <port> <baudrate> <parity (0|1)> <char>\n"+
136
                                "Examples:\n"+
137
                                "java UARTSendReceive COM5 9600 0 a\n"+
138
                                "java UARTSendReceive /dev/ttyUSB0 115200 0 a\n");
139
                // 1st arg.
140
                System.out.println("port = " + args[0]);
141
                System.out.println("baudrate = " + args[1]);
142
                //if(!args[1].equals("115200") && !args[1].equals("38400") && !args[1].equals("9600"))
143
                //      fatalError("Invalid baudrate");
144
                System.out.println("parity = " + args[2]);
145
                System.out.println("char = " + args[3]);
146
                UARTSendReceive usr;
147
                usr = new UARTSendReceive();
148
                usr.getCapturedData(args[0], args[1], args[2], args[3]);
149
        }
150
 
151
        SerialPort serialPort;
152
        byte [][] memoryDataWords;
153
        int octetsPerWord, idOfTypeBitInLastOctet, totalmemoryDataBytes;
154
        // Properties file members
155
        String portName;
156
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.