![]() |
Forum Index : Microcontroller and PC projects : Mmbasic talking to VB6
Page 1 of 2 ![]() ![]() |
|||||
Author | Message | ||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
Continuing with this project I have come to another mental block and was hoping a learned Back Shedder may be able to correct my thinking. I have a MICROMITE reporting back to a PC via the LoRa modules but I am stuck with my very rusty VB6 coding. The VB6 code generates an ID string ( number between 11 and 23) which it transmits to the receiving LoRa unit which is sitting at an INPUT prompt waiting for its ID. Once the ID is recognised the MICROMITE reads the attached probe and forwards that information back to the PC. I can monitor the ID being received by the remote unit and see it transmit back and also see the information received back on Terterm but I cannot see why the VB6 code wont recognise the response. Here is the VB6 code piece that I am using: Sub RadioRequest() '############################################################################################################# '################################### For LoRa Radio ################################## '############################################################################################################# ' The LoRa Radio is attached to a MICROMITE controller running MMBASIC 5.4. The RS232 output of the LoRa radio ' module is linked to the CONSOLE port pins of the MICROMITE which has a program running and is paused at an ' INPUT prompt waiting for an ID number to respond to. The ID numbers are two digit beginning at 11 and ' increment up to 23 covering 12 LoRa units. If the LoRa unit recognises its ID number the program will awake ' the SDI-12 probe and forward all the readings back via the CONSOLE port. If the MICROMITE cannot access the ' probe in a timely manner it will report an error stating there is an issue with the probe. MSComm1.Commport = 3 MSComm1.Settings = "9600,N,8,1" '"115200,N,8,1" MSComm1.DTREnable = True 'NOM TRUE MSComm1.EOFEnable = False 'NOM FALSE MSComm1.Handshaking = comNone 'OPTIONS comNone* comRTS comRTSXOnXOff comXOnXoff MSComm1.InBufferSize = 1024 'NOM 1024 MSComm1.InputLen = 0 'NOM 0 MSComm1.InputMode = comInputModeText 'comInputModeText* OR comInputModeBinary MSComm1.NullDiscard = False 'NOM FALSE MSComm1.OutBufferSize = 512 'NOM 512 MSComm1.RThreshold = 0 'NOM 0 MSComm1.RTSEnable = False 'NOM FALSE MSComm1.SThreshold = 1 'NOM 0 If Form1.Text8.BackColor = Red Then 'GO THROUGH THE MOTIONS BUT DO NOT SEND DATA TO LoRa Text1.Text = Text3.Text + " ***** LoRa ACTIVITY IS CURRENTLY DE_ACTIVATED " + CRLF$ + Text1.Text Text5.BackColor = Pink 'LoRa COMM PORT BACKGROUND TO PINK Else 'Here for normal radio operation Test=0 Form1.Text8.BackColor = Green 'Radio Port TextBox On Error Resume Next If MSComm1.PortOpen = False Then MSComm1.PortOpen = True If Err Then MsgBox " COM PORT not available. Try checking the setup file to address the correct port." GoTo NoLoRa End If End If MSComm1.InBufferCount = 0 'Flush the Comm port input buffer. Form1.Text13.BackColor = Green 'FLAG BESIDE COMBO BOX TO Green LoRa$ = RadioMessage$ + chr$(13) ' Added CR for entering at the INPUT prompt of MMBASIC MSComm1.Output = LoRa$ 'LoRa$ is a two digit number between 11 and 23 Form1.Text27.Text = RadioMessage$ 'used for monitoring purposes If Test = 0 Then Do Dummy = DoEvents() Loop Until MSComm1.InBufferCount > IBC RadioResponse$ = MSComm1.Input Form1.Text1.Text = Text3.Text + " RadioResponse$ " + RadioResponse$ + CRLF$ + Text1.Text Sleep 500 Form1.Text13.BackColor = Grey End If 'If Test = 0 Then MSComm1.PortOpen = False ' close com port Form1.Text8.BackColor = Grey Form1.Text13.BackColor = Grey NoComms: Form1.Text13.BackColor = Grey ' was set to &H80000008 BLACK NoLoRa: Text7.BackColor = Pink Text12.BackColor = Pink Text5.BackColor = &HC0C0C0 '*********************************************************************************************************************** * '*********************************************************************************************************************** * End Sub Hope someone can help Graeme |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Loop Until MSComm1.InBufferCount > IBC Start by reading the input buffer a character (or two) at a time and printing whatever you receive. You might see what's missing. Jim Don't drink VB so not much help with the code requirements. VK7JH MMedit |
||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
The line in the code should read: Loop Until MSComm1.InBufferCount > 0 But as IBC=0 it is still the same. I had put it through a loop making IBC 0-20 but still no go. The resultant Mscomm1.Input printed nothing each time. Not sure what is going on. I a have used similar code in the past to read strings from RS232 modems etc. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Assuming Test does equal zero, and RadioResponse$ ends up as a zero length string, I would put a big delay before reading the input data to be sure there is 'something' there. In my language of choice, there would be lots of DEBUG statements to see whats really happening. Jim VK7JH MMedit |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
try MSComm1.RThreshold = 1 The OnComm event wont fire unless this is set and although you aren't using events in your code (you should, PCs don't like resource hogs) they may be related I have a fully worked up radio network package for Micromite <-> PC with MMBasic & VB6 - it was written to use HC-12s but wouldn't be difficult to tweak. Has TIME broadcast and encryption ![]() Might give you inspiration. The VB6 is in the ZIP attached (top right). http://www.fruitoftheshed.com/MMBasic.Short-range-radio-network-not-WiFi-SLAVE-module-Uses-cheapie-433MHz-modules-or-HC- 12.ashx |
||||
erbp Senior Member ![]() Joined: 03/05/2016 Location: AustraliaPosts: 195 |
A couple of thoughts on possible things to check / consider: 1. If Test = 0 Then Unless my eyes deceive me Test is not setup anywhere in the posted section of code, so whether it will/won't be 0 at this line is not possible to verify. If it isn't 0, then your code won't even attempt to receive the response. 2. Do Dummy = DoEvents() Loop Until MSComm1.InBufferCount > IBC Again the value of IBC can't be verified from the code. If it is 0 as you say, then this loop will break after the first character of the response has been received. You didn't describe what the response format is, but if the first character were a non-printable one (e.g. control code), then you would get nothing visible printed as the response. Also, this code will not allow you to see the full response if it comprises more that 1 character (unless IBC is something greater than 0). Hope this might help, Phil. |
||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
Thank you for your suggestions. I have tried your suggestions but I am still not able to grasp the returned radio information. If I set the loop to a InbufferCount up to 80, the program exits the loop at that amount. This tells me that the VB6 program is receiving information from the radio unit but the length of the string is still zero. ![]() |
||||
erbp Senior Member ![]() Joined: 03/05/2016 Location: AustraliaPosts: 195 |
Are you sure the data being returned over the radio link is text - maybe it is binary? The following is from a Microsoft MSDN webpage: The Input property is used to store and retrieve data from the receive buffer. For example, if you wanted to retrieve data from the receive buffer and display it in a text box, you might use the following code: txtDisplay.Text = MSComm1.Input To retrieve the entire contents of the receive buffer, however, you must first set the InputLen property to 0. This can be done at design or run time. You can also receive incoming data as either text or binary data by setting the InputMode property to one of the following Visual Basic constants: comInputModeText or comInputModeBinary. The data will either be retrieved as string or as binary data in a Byte array. Use comInputModeText for data that uses the ANSI character set and comInputModeBinary for all other data, such as data that has embedded control characters, Nulls, etc. As each byte of data is received, it is moved into the receive buffer and the InBufferCount property is incremented by one. The InBufferCount property, then, can be used to retrieve the number of bytes in the receive buffer. You can also clear the receive buffer by setting the value of this property to 0. If the data is binary you would need to retrieve it from the MSComm1.Input property into a suitably sized byte array and then parse the array to convert whatever is returned into a format you can use / display. However if the response is just ANSI characters what you currently have should be working. You said you could see the response using Teraterm - did Teraterm display a readable string or was it just 'strange' character(s)? 'Strange' characters being displayed would probably indicate the response is not ANSI characters and you would need to use the binary receive mode. Cheers, Phil. |
||||
Alastair Senior Member ![]() Joined: 03/04/2017 Location: AustraliaPosts: 161 |
I am with Phil here. If you are seeing 'stuff' coming in but it is garbage, then most likely you are seeing binary data. Either it is being transmitted as binary rather than ascii text or it is being displayed as binary rather than ascii. Make sure that what is transmitted is really ascii text. Send a repetition of the same character so you know the ascii code (+ CR,LF probably) and then look at the end result. If the received data is variable then you have a problem. If it is constant garbage then it is likely to be just how the data is being interpreted/displayed. The above is probably obvious to you - if so apologies. Cheers, Alastair |
||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
The Micromite is generating the text string through the PRINT command and forwarding it via the CONSOLE port to the LoRa module. I can read this message ok with Teraterm from the receiving COM PORT. Using VB6 I can get the MSComm1.InputBuffer to read up to 163 but the MSComm1.Input string has no length nor is there any sort of "stuff" visible. I did try to change the MSComm input mode from TEXT to BINARY but it made no difference. Any more thoughts? ![]() |
||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
Here is the code that I am using at the moment: Sub RadioRequest() '############################################################################################################# '################################### For LoRa Radio ################################## '############################################################################################################# ' The LoRa Radio is attached to a MICROMITE controller running MMBASIC 5.4. The RS232 output of the LoRa radio ' module is linked to the CONSOLE port pins of the MICROMITE which has a program running and is paused at an ' INPUT prompt waiting for an ID number to respond to. The ID numbers are two digit beginning at 11 and ' increment up to 23 covering 12 LoRa units. If the LoRa unit recognises its ID number the program will awake ' the SDI-12 probe and forward all the readings back via the CONSOLE port. If the MICROMITE cannot access the ' probe in a timely manner it will report an error stating there is an issue with the probe. MSComm1.Commport = 3 MSComm1.Settings = "9600,N,8,1" '"115200,N,8,1" MSComm1.DTREnable = True 'NOM TRUE MSComm1.EOFEnable = False 'NOM FALSE MSComm1.Handshaking = comNone 'OPTIONS comNone* comRTS comRTSXOnXOff comXOnXoff MSComm1.InBufferSize = 1024 'NOM 1024 MSComm1.InputLen = 0 'NOM 0 MSComm1.InputMode = comInputModeText 'comInputModeText* OR comInputModeBinary MSComm1.NullDiscard = False 'NOM FALSE MSComm1.OutBufferSize = 512 'NOM 512 MSComm1.RThreshold = 1 'NOM 0 MSComm1.RTSEnable = False 'NOM FALSE MSComm1.SThreshold = 1 'NOM 0 MSComm1.InBufferCount = 0 'Flush the Comm port input buffer. MSComm1.NullDiscard = True If Form1.Text8.BackColor = Red Then 'GO THROUGH THE MOTIONS BUT DO NOT SEND DATA TO LoRa Text1.Text = Text3.Text + " ***** LoRa ACTIVITY IS CURRENTLY DE_ACTIVATED " + CRLF$ + Text1.Text Text5.BackColor = Pink 'LoRa COMM PORT BACKGROUND TO PINK Else 'Here for normal radio operation Test=0 Form1.Text8.BackColor = Green 'Radio Port TextBox On Error Resume Next If MSComm1.PortOpen = False Then MSComm1.PortOpen = True If Err Then MsgBox " COM PORT not available. Try checking the setup file to address the correct port." GoTo NoLoRa End If End If Form1.Text13.BackColor = Green 'FLAG BESIDE COMBO BOX TO Green LoRa$ = Text17.Text + chr$(13) ' Added CR for entering at the INPUT prompt of MMBASIC MSComm1.Output = LoRa$ 'LoRa$ is a two digit number between 11 and 23 followed by CR Sleep 500 If Test = 0 Then Do Dummy = DoEvents() Form1.Text8.BackColor = Pink 'COM Port TextBox Pink background during loop Form1.Text27.Text = MSComm1.InputLen & " " & MSComm1.InBufferCount & " " & Len(RadioResponse) Loop Until MSComm1.InBufferCount > 76 RadioResponse = MSComm1.Input ' Typical ERROR response from LoRa "25-09-2017 06:37:35 No Response from probe! Please check connections on unit" sent twice ' Typical NORMAL response from LoRa "25-09-2017 06:37:35 0+008.67+000.85+000.77+000.75+001.43+000.55+000.17+000.00" Sleep 500 Form1.Text36.Text = Form1.Text36.Text + RadioResponse RadioInputLen = MSComm1.InputLen Form1.Text1.Text = Text3.Text + " Response from LoRa Radio= " + RadioResponse + CRLF$ + Text1.Text Form1.Text27.Text = MSComm1.InputLen & " " & MSComm1.InBufferCount & " " & Len(RadioResponse) Form1.Text8.BackColor = Green Form1.Text13.BackColor = Grey End If 'If Test = 0 Then MSComm1.PortOpen = False ' close com port Form1.Text8.BackColor = Grey Form1.Text13.BackColor = Grey NoComms: Form1.Text13.BackColor = Grey ' was set to &H80000008 BLACK NoLoRa: Text7.BackColor = Pink Text12.BackColor = Pink Text5.BackColor = &HC0C0C0 '*********************************************************************************************************************** * '*********************************************************************************************************************** * End Sub |
||||
goc30![]() Guru ![]() Joined: 12/04/2017 Location: FrancePosts: 435 |
hi I don't understand this 2 lines MSComm1.Output = LoRa$ Sleep 500 first: when you begin a prog and want to communicate with externe com, it is best to send char by char and not the complete string like that for i=1 to len(txt) a$=mid$(txt,i,1) 'then you can print a$ to see char to be send mscomm.output=a$ next i second: if you send a string, it is not good to put a sleep, because vb send your string in the rs232 chip, char by char. il send the first char, and wait for buffer empty to send next char. If you put a "Sleep" fonction, proc is stopped and it send only th first char, after it sleep 500ms and after only it send the second char. then you have chance to fall in timeout in micromite's circuit The best is to put a "DoEvents" function. this function suspend vb proc to give hand to system. In this case system can send your string. My english is not the best, so i give you a link for using sleep/pause/doevents http://computer-programming-forum.com/72-visual-basic-vb/e47feecf500426d0.htm |
||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
goc30, thank you for your suggestions. I am not having any trouble with that part of the code as the VB app is transmitting out the correct string and the remote device is recognising it and returning a string which is being received by the local unit. Graeme |
||||
Alastair Senior Member ![]() Joined: 03/04/2017 Location: AustraliaPosts: 161 |
Graeme, apologies for my earlier post. I just jumped to the end of the thread and did not see that you were seeing things correctly via Teraterm. So it is definitely in Vb6 and most likely the display part. In these things I am pretty simple. I would just send say 4 characters with a long pause and look to see what and how many are being received. I used VB6 eons ago and I remember that I used to have trouble with the buffering both in and out. As has been suggested I used to use interrupts to determine when there were characters ready. I recall that with polling it tended to wait until the buffer was full. I think I will opt out of this as my vb6 memory is old and flaky. I will look back in my archive and see if I can find some relevant code but that is ~10+years ago. Cheers, Alastair |
||||
goc30![]() Guru ![]() Joined: 12/04/2017 Location: FrancePosts: 435 |
Oa47 sorry, i have not understand your pb, now it more clear. when i use vb6 comm, for recept string, i work in interrupt mode i use mscomm event Private Sub MSComm1_OnComm() Select Case MSComm1.CommEvent '--- Event messages Case MSCOMM_EV_RECEIVE dummy = DoEvents() ' DoEvents dialapi '(MSComm1.Input) .... end select sub dialapi j = MSComm1.InBufferCount If j = 0 Then dtb = MSComm1.Input 'clear buffer in case of pb 'Call ini_comm1 Exit Sub End If dtb = MSComm1.Input ledrec = 1 'flag to animate circle as led Shape1(1).FillColor = &HFF& timout2rec = 0 'init timeout recept If j > 0 Then For i = 1 To j Mid$(mdbbufrec, mdbnbbufrec, 1) = Mid$(dtb, i, 1) v2 = Asc(Mid$(dtb, i, 1)) .... countrec=countrec+1 'if you work in fix string if countrec=> nbmaxchar then ... 'or if v2=10 or v2=13 then 'end of string ... 'in all cases you put a flag for your main prog who wait end sub in main form... main: ... etq1: if flag=1 then goto etq2 doevents goto etq1 etq2: ... |
||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
I have used code similar to the code that I am using now but in all previous cases the VB app was looking directly at a real COMM Port whereas in this situation the COMM port is a Virtual COMM port created by a USB to Serial module and this may be the area that is causing the issue. I will try to incorporate the code variation that goc30 has suggested and see if that makes a difference. OA47 |
||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
@ goc30 I have not been able to get the code you suggested to work. I don't think it is your code that is the problem but my ability to use the code. Interestingly I modified the received message to a length of 53 characters and used Loop Until Len(MSComm1.Input)>52 But still get nothing when I print the string. It seems that when I test for InBufferCount or MSComm1.Input within the loop the contents are zeroed. ![]() |
||||
OA47 Guru ![]() Joined: 11/04/2012 Location: AustraliaPosts: 986 |
More investigation with this issue. I set up a PC with a "real" com port and tested the software using a TTL-USB adaptor to connect the com port to the LoRa unit and I can see the data from the radio unit. So it seems my problem is the USB-serial converter and how VB6 treats the buffers in the unit. ![]() |
||||
Gizmo![]() Admin Group ![]() Joined: 05/06/2004 Location: AustraliaPosts: 5119 |
Have you thought about porting to Just Basic? Years ago I put together a logger that used a PicAxe to send the data, and a VB6 program to read and display the data. I started to hit a few walls with the VB6 serial com limitations, so ported it to Just Basic. Just Basic is free, your code is easier share, and it can handle a lot more com ports than VB6. Bit more info here, http://www.thebackshed.com/windmill/PicLog4.asp Glenn The best time to plant a tree was twenty years ago, the second best time is right now. JAQ |
||||
MikeO Senior Member ![]() Joined: 11/09/2011 Location: AustraliaPosts: 275 |
If you just want to receive the data and log it to a file perhaps use something like this: search on "rs232 data logger eltima sourceforge" should work. I have used this many times over the years I have generally found it Just Works! Mike Codenquilts |
||||
Page 1 of 2 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |