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
Hello agraham,

sorry that I ask again, but there is not yet a way to implement this feature?
I have so tried, unfortunately without success.
B4X:
Sub Process_Globals
   Dim usb As UsbSerial
   Dim astreams As AsyncStreams
   Dim connected As Boolean
End Sub

Sub Globals
   Dim btnSend, btnOpen, btnClose As Button
   Dim lblLogfile As Label
   Dim txtCommand As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   btnClose.Enabled = False
   btnSend.Enabled = False
End Sub

Sub btnOpen_Click
   If usb.UsbPresent = usb.USB_NONE Then
      Log("Msgbox - no device")
      Msgbox("No USB device or accessory detected!", "Error")
      Log("Msgbox - returned")
      Return
   End If
   Log("Checking permission")
   If (usb.HasPermission) Then
      connected = True
      Msgbox(usb.DeviceInfo, "Device Information")
      Dim dev As Int
      dev = usb.Open(9600)
      lblLogfile.Text=dev & ";" & usb.USB_DEVICE & CRLF
      If dev <> usb.USB_NONE Then
'         usb.SetParameters(9600,usb.DATABITS_8,usb.STOPBITS_1,usb.PARITY_NONE)
         Log("Connected successfully!")
         btnOpen.Enabled = False
         btnClose.Enabled = True
         btnSend.Enabled = True         
         astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")
      Else
         Log("Error opening USB port")
      End If
   Else
      usb.RequestPermission
   End If
End Sub

Sub Astreams_NewData (Buffer() As Byte)
   Log("NewData")
   Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
   lblLogfile.Text=lblLogfile.Text & "NewData"
   lblLogfile.Text=lblLogfile.Text & BytesToString(Buffer, 0, Buffer.Length, "UTF8")
End Sub

Sub btnClose_Click
   astreams.Close
   btnOpen.Enabled = True
   btnClose.Enabled = False
   btnSend.Enabled = False   
End Sub

Sub AStreams_Error
   Log("Error: " & LastException)
   astreams.Close
End Sub

Sub Astreams_Terminated
   Log("Terminated")
   astreams.Close
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnSetCommBreak_Click
   Dim b(10) As Byte
   b(0)=0x00
   b(1)=0x00
   b(2)=0x00
   b(3)=0x00
   b(4)=0x00
   b(5)=0x00
   b(6)=0x00
   b(7)=0x00
   b(8)=0x00
   b(9)=0x00
   astreams.Write(b)
   Send_Translatemodus
End Sub

Sub Send_Translatemodus
   Dim b(5) As Byte
   b(0)=0xC0
   b(1)=0xFF
   b(2)=0xEE
   b(3)=0xAD
   b(4)=0x0A
   astreams.Write(b)
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Your btnSetCommBreak_Click canot work because of all the stop bits and start bits. The only way of doing it "artificially" is by a single character at a very low baud rate.

UsbSerial2 will not be able to implement break because the technical information is not available for the different serial adaptors to let it do so.
 
Upvote 0
D

Deleted member 103

Guest
Do you think so?

B4X:
Sub btnSetCommBreak_Click
    Dim b(2) As Byte
    b(0)=0x00
    b(1)=0xA0
   usb.SetParameters(75,usb.DATABITS_8,usb.STOPBITS_1,usb.PARITY_NONE)
    astreams.Write(b)
    Send_Translatemodus
End Sub

Sub Send_Translatemodus
    Dim b(5) As Byte
    b(0)=0xC0
    b(1)=0xFF
    b(2)=0xEE
    b(3)=0xAD
    b(4)=0x0A
   usb.SetParameters(9600,usb.DATABITS_8,usb.STOPBITS_1,usb.PARITY_NONE)
    astreams.Write(b)
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I don't understand why you are sending two bytes. It will only work, if in fact it works at all, for a single character as the stop bit of the first character will interrupt the break. I would try the following, and put a 'scope on the line to see what happens.
B4X:
Sub btnSetCommBreak_Click
     usb.SetParameters(75,usb.DATABITS_8,usb.STOPBITS_1,usb.PARITY_NONE)
    astreams.Write(0)
    usb.SetParameters(9600,usb.DATABITS_8,usb.STOPBITS_1,usb.PARITY_NONE)
End Sub
 
Upvote 0
D

Deleted member 103

Guest
It runs without error message and also without confirmation that it all works. :(

Do you still have a tip for me?
 
Upvote 0
D

Deleted member 103

Guest
To switch this device in the so called "Translate Mode" I need to send this 4 byte. And these 4 bytes, I can only send after break signal.

Only then I can exchange data with this device.

Here are the instructions:
Zum Wechsel in den "Translate-Modus",
müssen die u. g. 4 Zeichen über USB
gesendet werden, die in die eigene
Anwendung implementiert werden können.
Vorher BREAK (USB), danach wieder zurück-nehmen!
Translation with google:
To change to the "Translate" mode,
and g have the 4 characters using USB
be sent to the own
Application may be implemented.
Previously BREAK (USB), then back-take!
Here again the syntax:
B4X:
BYTE data[4] = {0xC0, 0xFF, 0xEE, 0xAD};
handle = createFile(...);
SetCommBreak(handle);
WriteFile(handle, data, ...);
ClearCommBreak(handle);
 
Upvote 0
D

Deleted member 103

Guest
Unfortunately I have no idea of serial interfaces, I have to assume that the manual is correct. :eek:
 
Upvote 0
D

Deleted member 103

Guest
Hi agraham,

I have a big request to you.
I've got a Java code from the device-developers with which the connection works.
But now my problem is that I can not put this Java code into B4a.

Can I send you the Java code via PM, so that you translate it for me?

I can't post this Java code in the forum because I otherwise would injured the secrecy.

I just sent in the Java code to your email address.

Ciao,
Filippo
 
Last edited by a moderator:
Upvote 0

TRudolphi

New Member
Licensed User
Longtime User
I think I solve your problem. You have to add a delay after sending the 'break' char at 75 baud,otherwise the break char will be send at 9600 baud.

I tested it on the oscilloscoop.

This is the code ( I used your own code)

B4X:
Sub Sleep(ms As Long)
Dim now As Long
  If ms > 1000 Then ms =1000  'avoid application not responding error
  now=DateTime.now
  Do Until (DateTime.now>now+ms)
    DoEvents
  Loop
End Sub

Sub SendBreak
    Dim ComBreak(1) As Byte
    usb.SetParameters(75,usb.DATABITS_8,usb.STOPBITS_1,usb.PARITY_NONE)
    ComBreak(0)=0
    astreams.Write(ComBreak)     ' Low for 30mS
    Sleep(50)                    ' Wait till the break character is send!
End Sub

Sub Send_Translatemodus
    Dim b(5) As Byte
    b(0)=0xC0
    b(1)=0xFF
    b(2)=0xEE
    b(3)=0xAD
    b(4)=0x0A
    usb.SetParameters(9600,usb.DATABITS_8,usb.STOPBITS_1,usb.PARITY_NONE)
    astreams.Write(b)
End Sub

Calling is as:

B4X:
    SendBreak
    Send_Translatemodus
 
Upvote 0
Top