SetCommBreak, ClearCommBreak

D

Deleted member 103

Guest
Hi guys,

I have to exchange via USB some data but for the first connection I need the two functions "SetCommBreak, ClearCommBreak".
Can someone help me with it?

Thanks already in advance
Filippo
 
D

Deleted member 103

Guest
So basically, I need this function:
B4X:
void SerialComm::TransmitBreakSignal()
{
//Transmits a break signal
::SetCommBreak(m_hPort);

//suspends the execution of the current thread for 100 milliseconds.
::Sleep( 100);

::ClearCommBreak(m_hPort);
}

How can this function convert to b4a?
 
Upvote 0
D

Deleted member 103

Guest
Hi agraham,

Unfortunately, it is not PL2303 adapter, it's from Atmel, look the attached picture.
 

Attachments

  • koko.jpg
    koko.jpg
    27.7 KB · Views: 237
Upvote 0
D

Deleted member 103

Guest
No, the ":: Sleep (100)" is only in the example, I need to send this four byte.

B4X:
void SerialComm::TransmitBreakSignal()
{
BYTE data[4] = {0xC0, 0xFF, 0xEE, 0xAD};
//Transmits a break signal
::SetCommBreak(m_hPort);

WriteFile(handle, data, ...);

::ClearCommBreak(m_hPort);
}
 
Upvote 0
D

Deleted member 103

Guest
I don't understand. Why would you want to set a break state on a serial line while you are writing a file
Anyway it doesn't look like you will be able to do it at all.
I also do not understand it currently.:(

I have a PDF file with this content.:confused:
 

Attachments

  • Koko_ll_2.jpg
    Koko_ll_2.jpg
    50.9 KB · Views: 256
Upvote 0
D

Deleted member 103

Guest
To switch this device in the so called "Translate Mode" I need to send this 4 byte.

Only then I can exchange data with this device.
 
Upvote 0

Rigsby

Member
Licensed User
Longtime User
That doesn't make sense to me! Why set a serial line to a break state then try to write to it?

In the olden days you used to press the break key, say on an electro-mechanical device like a teletype, and it told the receiving device at the other end that a message was coming. It woke it up in effect and got it ready to receive data. I'm guessing it's the same type of thing here.
 
Upvote 0

Rigsby

Member
Licensed User
Longtime User
To switch this device in the so called "Translate Mode" I need to send this 4 byte.

Only then I can exchange data with this device.

Have you tried sending a null character. Then wait some time depending on baudrate, then send the 4 bytes ?

I'm almost 100% you can't emulate the break with any byte character, but if it is possible to emulate, then a null would make the most sense.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
It woke it up in effect and got it ready to receive data. I'm guessing it's the same type of thing here.
No. The sample code sets the break state, writes to the port then unsets the break state. It doesn't make sense.

I'm almost 100% you can't emulate the break with any byte character
You can, as I posted earlier. However it does rely on being able to switch the port baud rate to a very low rate and then back to the required rate without causing a hiatus on the line. i.e maintaining the static mark state during the change.
 
Upvote 0
D

Deleted member 103

Guest
So when I connect this unit to the PC, then I immediately get a response (see picture below).
But when I plug in this unit to the Nexus-7, then get no answer, but recognized as a USB device.
I use the example of "UsbSerialDemo.b4a".
 

Attachments

  • Koko_ll_3.jpg
    Koko_ll_3.jpg
    33.4 KB · Views: 232
  • Koko_ll_4.jpg
    Koko_ll_4.jpg
    43.3 KB · Views: 229
Upvote 0
D

Deleted member 103

Guest
I have found again what. Can this be converted to a b4a-library?
B4X:
// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Win32SerialPort.java

package com.sun.comm;

import java.io.*;
import java.util.TooManyListenersException;
import javax.comm.*;

// Referenced classes of package com.sun.comm:
//            NotificationThread, Win32SerialInputStream, Win32SerialOutputStream

class Win32SerialPort extends SerialPort
{

    Win32SerialPort(String s)
        throws IOException
    {
        rcvThreshold = -1;
        rcvTimeout = -1;
        framing = false;
        dtr = true;
        rts = true;
        closed = false;
        super.name = s;
        if(!nativeConstructor(s))
        {
            String s1 = "Unable to create port " + s;
            throw new IOException(s1);
        }
        outs = new Win32SerialOutputStream(this);
        ins = new Win32SerialInputStream(this);
        readSignal = new Object();
        wa = new byte[1];
        try
        {
            setFlowControlMode(0);
            setSerialPortParams(9600, 8, 1, 0);
        }
        catch(UnsupportedCommOperationException _ex) { }
        notificationThread = new NotificationThread("Win32SerialPort Notification thread", this);
        notificationThread.start();
    }

    private native boolean nativeConstructor(String s);

    public InputStream getInputStream()
        throws IOException
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return ins;
    }

    public OutputStream getOutputStream()
        throws IOException
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return outs;
    }

    public void enableReceiveThreshold(int i)
        throws UnsupportedCommOperationException
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(i < 0)
        {
            throw new UnsupportedCommOperationException("This threshold value is not supported");
        } else
        {
            rcvThreshold = i;
            return;
        }
    }

    public void disableReceiveThreshold()
    {
        if(closed)
        {
            throw new IllegalStateException("Port Closed");
        } else
        {
            rcvThreshold = -1;
            return;
        }
    }

    public boolean isReceiveThresholdEnabled()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        return rcvThreshold != -1;
    }

    public int getReceiveThreshold()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return rcvThreshold;
    }

    public void enableReceiveTimeout(int i)
        throws UnsupportedCommOperationException
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(i < 0)
        {
            throw new UnsupportedCommOperationException("This timeout value is not supported");
        } else
        {
            rcvTimeout = i;
            return;
        }
    }

    public void disableReceiveTimeout()
    {
        if(closed)
        {
            throw new IllegalStateException("Port Closed");
        } else
        {
            rcvTimeout = -1;
            return;
        }
    }

    public int getReceiveTimeout()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return rcvTimeout;
    }

    public boolean isReceiveTimeoutEnabled()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        return rcvTimeout != -1;
    }

    public void disableReceiveFraming()
    {
        if(closed)
        {
            throw new IllegalStateException("Port Closed");
        } else
        {
            framing = false;
            nativeDisableFraming();
            return;
        }
    }

    public void enableReceiveFraming(int i)
        throws UnsupportedCommOperationException
    {
        if(closed)
        {
            throw new IllegalStateException("Port Closed");
        } else
        {
            framing = true;
            framingByte = i & 0xff;
            nativeEnableFraming(i);
            return;
        }
    }

    public int getReceiveFramingByte()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return framingByte;
    }

    public boolean isReceiveFramingEnabled()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return framing;
    }

    native void nativeEnableFraming(int i);

    native void nativeDisableFraming();

    public void setInputBufferSize(int i)
    {
        if(closed)
        {
            throw new IllegalStateException("Port Closed");
        } else
        {
            nsetInputBufferSize(i);
            return;
        }
    }

    private native void nsetInputBufferSize(int i);

    public int getInputBufferSize()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return ngetInputBufferSize();
    }

    private native int ngetInputBufferSize();

    public void setOutputBufferSize(int i)
    {
        if(closed)
        {
            throw new IllegalStateException("Port Closed");
        } else
        {
            nsetOutputBufferSize(i);
            return;
        }
    }

    private native void nsetOutputBufferSize(int i);

    public int getOutputBufferSize()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return ngetOutputBufferSize();
    }

    private native int ngetOutputBufferSize();

    public int getBaudRate()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return baudrate;
    }

    public int getDataBits()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return dataBits;
    }

    public int getStopBits()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return stopBits;
    }

    public int getParity()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return parity;
    }

    public void setFlowControlMode(int i)
        throws UnsupportedCommOperationException
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if((i & 3) != 0 && (i & 0xc) != 0)
            throw new UnsupportedCommOperationException("Cannot mix hardware and software flow control");
        flowcontrol = i;
        nativeSetFlowcontrolMode(i);
        if((i & 1) == 0)
        {
            rts = true;
            dtr = true;
        }
    }

    native void nativeSetFlowcontrolMode(int i);

    public int getFlowControlMode()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return flowcontrol;
    }

    public void setSerialPortParams(int i, int j, int k, int l)
        throws UnsupportedCommOperationException
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        for(int i1 = 0; i1 < speeds.length; i1++)
        {
            if(i == speeds[i1])
                break;
            if(i < speeds[i1])
                throw new UnsupportedCommOperationException("Unsupported baud rate");
        }

        if(j != 5 && j != 6 && j != 7 && j != 8)
            throw new UnsupportedCommOperationException("Unsupported num of databits");
        if(k != 1 && k != 2 && k != 3)
            throw new UnsupportedCommOperationException("Unsupported num of stopbits");
        if(l != 2 && l != 1 && l != 0)
            throw new UnsupportedCommOperationException("Unsupported parity value");
        baudrate = i;
        parity = l;
        dataBits = j;
        stopBits = k;
        setCommDeviceParams(i, l, j, k);
        saveCommDeviceState();
        try
        {
            setFlowControlMode(flowcontrol);
            return;
        }
        catch(UnsupportedCommOperationException _ex)
        {
            return;
        }
    }

    public native void setCommDeviceParams(int i, int j, int k, int l);

    public void sendBreak(int i)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        synchronized(this)
        {
            setCommBreak();
            try
            {
                Thread.sleep(i);
            }
            catch(InterruptedException _ex) { }
            clearCommBreak();
        }
    }

    private native void setCommBreak();

    private native void clearCommBreak();

    public void setDTR(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if((flowcontrol & 1) == 1)
        {
            return;
        } else
        {
            nativeSetDTR(flag);
            dtr = flag;
            return;
        }
    }

    private native void nativeSetDTR(boolean flag);

    public boolean isDTR()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return dtr;
    }

    public void setRTS(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if((flowcontrol & 1) == 1)
        {
            throw new IllegalStateException("Cannot modify RTS when Hardware flowcontrol is on.");
        } else
        {
            nativeSetRTS(flag);
            rts = flag;
            return;
        }
    }

    private native void nativeSetRTS(boolean flag);

    public boolean isRTS()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return rts;
    }

    public boolean isCTS()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return nativeIsCTS();
    }

    private native boolean nativeIsCTS();

    public boolean isDSR()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return nativeIsDSR();
    }

    private native boolean nativeIsDSR();

    public boolean isRI()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return nativeIsRI();
    }

    private native boolean nativeIsRI();

    public boolean isCD()
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        else
            return nativeIsCD();
    }

    private native boolean nativeIsCD();

    public synchronized void addEventListener(SerialPortEventListener serialporteventlistener)
        throws TooManyListenersException
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(eventListener != null)
            throw new TooManyListenersException();
        eventListener = serialporteventlistener;
        if(eventListener != null && notificationThread == null)
        {
            notificationThread = new NotificationThread("Win32SerialPort Notification thread", this);
            notificationThread.start();
        }
    }

    public synchronized void removeEventListener()
    {
        if(closed)
        {
            throw new IllegalStateException("Port Closed");
        } else
        {
            eventListener = null;
            return;
        }
    }

    public synchronized void notifyOnDataAvailable(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 1;
        else
            notifyMask &= -2;
        nnotifyOnDataAvailable(flag);
    }

    private native void nnotifyOnDataAvailable(boolean flag);

    public synchronized void notifyOnOutputEmpty(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 2;
        else
            notifyMask &= -3;
        nnotifyOnOutputEmpty(flag);
    }

    private native void nnotifyOnOutputEmpty(boolean flag);

    public synchronized void notifyOnCTS(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 8;
        else
            notifyMask &= -9;
        nnotifyOnCTS(flag);
    }

    private native void nnotifyOnCTS(boolean flag);

    public synchronized void notifyOnDSR(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 0x10;
        else
            notifyMask &= 0xffffffef;
        nnotifyOnDSR(flag);
    }

    private native void nnotifyOnDSR(boolean flag);

    public synchronized void notifyOnCarrierDetect(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 0x20;
        else
            notifyMask &= 0xffffffdf;
        nnotifyOnCarrierDetect(flag);
    }

    private native void nnotifyOnCarrierDetect(boolean flag);

    public synchronized void notifyOnRingIndicator(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 4;
        else
            notifyMask &= -5;
        nnotifyOnRingIndicator(flag);
    }

    private native void nnotifyOnRingIndicator(boolean flag);

    public synchronized void notifyOnOverrunError(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 0x40;
        else
            notifyMask &= 0xffffffbf;
        nnotifyOnOverrunError(flag);
    }

    private native void nnotifyOnOverrunError(boolean flag);

    public synchronized void notifyOnParityError(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 0x80;
        else
            notifyMask &= 0xffffff7f;
        nnotifyOnParityError(flag);
    }

    private native void nnotifyOnParityError(boolean flag);

    public synchronized void notifyOnFramingError(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 0x100;
        else
            notifyMask &= 0xfffffeff;
        nnotifyOnFramingError(flag);
    }

    private native void nnotifyOnFramingError(boolean flag);

    public synchronized void notifyOnBreakInterrupt(boolean flag)
    {
        if(closed)
            throw new IllegalStateException("Port Closed");
        if(flag)
            notifyMask |= 0x200;
        else
            notifyMask &= 0xfffffdff;
        nnotifyOnBreakInterrupt(flag);
    }

    private native void nnotifyOnBreakInterrupt(boolean flag);

    native int waitForEvent();

    private void saveCommDeviceState()
    {
        stateRI = isRI();
        stateCTS = isCTS();
        stateDSR = isDSR();
        stateCD = isCD();
    }

    void sendCTSevent()
    {
        if((notifyMask & 8) == 8)
        {
            boolean flag = isCTS();
            if(flag != stateCTS)
            {
                SerialPortEvent serialportevent = new SerialPortEvent(this, 3, stateCTS, flag);
                eventListener.serialEvent(serialportevent);
                stateCTS = flag;
            }
        }
    }

    void sendDSRevent()
    {
        if((notifyMask & 0x10) == 16)
        {
            boolean flag = isDSR();
            if(flag != stateDSR)
            {
                SerialPortEvent serialportevent = new SerialPortEvent(this, 4, stateDSR, flag);
                eventListener.serialEvent(serialportevent);
                stateDSR = flag;
            }
        }
    }

    void sendCDevent()
    {
        if((notifyMask & 0x20) == 32)
        {
            boolean flag = isCD();
            if(flag != stateCD)
            {
                SerialPortEvent serialportevent = new SerialPortEvent(this, 6, stateCD, flag);
                eventListener.serialEvent(serialportevent);
                stateCD = flag;
            }
        }
    }

    void sendRIevent()
    {
        if((notifyMask & 4) == 4)
        {
            boolean flag = isRI();
            if(flag != stateRI)
            {
                SerialPortEvent serialportevent = new SerialPortEvent(this, 5, stateRI, flag);
                eventListener.serialEvent(serialportevent);
                stateRI = flag;
            }
        }
    }

    void sendBIevent()
    {
        if((notifyMask & 0x200) == 512)
        {
            SerialPortEvent serialportevent = new SerialPortEvent(this, 10, false, true);
            eventListener.serialEvent(serialportevent);
        }
    }

    void sendOEevent()
    {
        if((notifyMask & 0x40) == 64)
        {
            SerialPortEvent serialportevent = new SerialPortEvent(this, 7, false, true);
            eventListener.serialEvent(serialportevent);
        }
    }

    void sendPEevent()
    {
        if((notifyMask & 0x80) == 128)
        {
            SerialPortEvent serialportevent = new SerialPortEvent(this, 8, false, true);
            eventListener.serialEvent(serialportevent);
        }
    }

    void sendFEevent()
    {
        if((notifyMask & 0x100) == 256)
        {
            SerialPortEvent serialportevent = new SerialPortEvent(this, 9, false, true);
            eventListener.serialEvent(serialportevent);
        }
    }

    void sendOutputEmptyEvent()
    {
        if((notifyMask & 2) == 2)
        {
            SerialPortEvent serialportevent = new SerialPortEvent(this, 2, false, true);
            eventListener.serialEvent(serialportevent);
        }
    }

    void sendDataAvailEvent()
    {
        if((notifyMask & 1) == 1)
        {
            SerialPortEvent serialportevent = new SerialPortEvent(this, 1, false, true);
            eventListener.serialEvent(serialportevent);
        }
    }

    protected void finalize()
        throws Throwable
    {
        nativeFinalize();
    }

    private native void nativeFinalize();

    void write(int i)
        throws IOException
    {
        wa[0] = (byte)i;
        write(wa, 0, 1);
    }

    void write(byte abyte0[], int i, int j)
        throws IOException
    {
        for(int k = 0; k < j;)
        {
            int l = nwrite(abyte0, i + k, j - k <= 512 ? j - k : 512);
            if(l > 0)
                k += l;
            else
                throw new IOException("write error");
        }

    }

    private native int nwrite(byte abyte0[], int i, int j);

    private native int nread(byte abyte0[], int i, int j)
        throws IOException;

    native int available()
        throws IOException;

    int read()
        throws IOException
    {
        byte abyte0[] = new byte[1];
        switch(read(abyte0, 0, 1))
        {
        case 1: // '\001'
            return abyte0[0] & 0xff;
        }
        return -1;
    }

    int read(byte abyte0[], int i, int j)
        throws IOException
    {
        int k = 200;
        int l = 0;
        if(rcvTimeout == 0)
            l = 0;
        else
        if(rcvTimeout == -1)
        {
            l = 0;
        } else
        {
            k = rcvTimeout >= 200 ? 200 : rcvTimeout;
            l = rcvTimeout / k;
        }
        int i1 = 0;
        int j1 = 0;
        while(i1 < j) 
        {
            int k1 = available();
            if(k1 > 0)
            {
                int l1 = nread(abyte0, i + i1, j - i1);
                if(l1 >= 0)
                    i1 += l1;
                else
                    return i1;
            }
            if(rcvTimeout == 0)
                return i1;
            if(i1 == j)
                return i1;
            if(framing && framingByteReceived)
            {
                framingByteReceived = false;
                return i1;
            }
            if(rcvTimeout == -1)
            {
                if(rcvThreshold == -1)
                {
                    if(i1 > 0)
                        return i1;
                } else
                if(i1 >= Math.min(rcvThreshold, j))
                    return i1;
            } else
            {
                if(j1 >= l)
                    return i1;
                if(rcvThreshold == -1)
                {
                    if(i1 > 0)
                        return i1;
                } else
                if(i1 >= Math.min(rcvThreshold, j))
                    return i1;
            }
            synchronized(readSignal)
            {
                j1++;
                try
                {
                    readSignal.wait(k);
                }
                catch(InterruptedException _ex)
                {
                    byte byte0 = -1;
                    return byte0;
                }
            }
        }
        return i1;
    }

    public void close()
    {
        eventListener = null;
        nativeFinalize();
        nativeHandle = 0;
        closed = true;
        super.close();
    }

    int nativeHandle;
    private InputStream ins;
    private OutputStream outs;
    private int rcvThreshold;
    int rcvTimeout;
    private boolean framing;
    private int framingByte;
    boolean framingByteReceived;
    private int baudrate;
    private int parity;
    private int dataBits;
    private int stopBits;
    private int flowcontrol;
    private static int speeds[] = {
        75, 110, 134, 150, 300, 600, 1200, 2400, 4800, 9600, 
        19200, 38400, 57600, 0x1c200
    };
    private boolean dtr;
    private boolean rts;
    NotificationThread notificationThread;
    SerialPortEventListener eventListener;
    private int notifyMask;
    static final int NOTIFY_DATA_AVAIL = 1;
    static final int NOTIFY_OUTPUT_EMPTY = 2;
    static final int NOTIFY_RI = 4;
    static final int NOTIFY_CTS = 8;
    static final int NOTIFY_DSR = 16;
    static final int NOTIFY_CD = 32;
    static final int NOTIFY_OE = 64;
    static final int NOTIFY_PE = 128;
    static final int NOTIFY_FE = 256;
    static final int NOTIFY_BI = 512;
    private boolean stateRI;
    private boolean stateCTS;
    private boolean stateDSR;
    private boolean stateCD;
    private boolean stateOE;
    private boolean statePE;
    private boolean stateFE;
    private boolean stateBI;
    Object readSignal;
    private byte wa[];
    private static final int READ_POLL = 200;
    boolean closed;

}
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The code above does not need to be converted to B4A. It is effectively a driver and does no more than UsbSerial2 does.

The VendorID and ProductID in your screenshot are ones the UsbSerial2 library should recognise so if you set the correct baud rate and line configuration it should work.

However, as I said before, UsbSerial2 does not support line break so if you really do need that then it won't work.
 
Upvote 0
D

Deleted member 103

Guest
However, as I said before, UsbSerial2 does not support line break so if you really do need that then it won't work.
That would be very bad! Then I can not use this device with B4A. :BangHead:

Thanks anyway for your help agraham.
 
Upvote 0
Top