|
Post by stan58 on Nov 19, 2018 16:35:39 GMT -5
I can open a serial port as follows
open "com";(portn);":19200,n,8,1,ds0,cs0,rs" for random as #commwt . This works and allows the port to be user specified.
portn is a variable selected by the user for the port to be used. My problem is what is the syntax in order to allow the baudrate to be specified by a variable in the same way?
|
|
|
Post by stan58 on Nov 20, 2018 4:26:04 GMT -5
Sorry for another stupid question worked it out brate=9600 portn= 8 open "com(portn);(brate),n,8,1" for random as #commwt
|
|
|
Post by stan58 on Nov 20, 2018 6:17:22 GMT -5
No that doesn't work after all. I just want the user to select the port and baud rate and then use that data to open the serial port , at the moment I can use the port number but I cant get the baud rate syntax right.
|
|
|
Post by Brandon Parker on Nov 20, 2018 8:46:54 GMT -5
You will first need to check to see which COM Ports are actually available to make it clean. There are many things that need to be checked (or be known) for the actual communication to work correctly. You just need to get the concatenation for the COM Port string down correctly to be able to build it.
You can use the "+" operator or the ";" to concatenate the string, but remember that you have to use the str$() function to convert numeric values to strings when you use the "+" operator and this is optional if you use the ";".
Open "Com" + str$(i) + ":" + str$(BaudRate) + ",n,8,1,ds0,cs0,rs0" For random As #COM
See if this will get you started.
Hint... You can use a looping structure to go through all possible COM Port Numbers and use an Array of BaudRates; looping through those as well to get all possible permutations of COM Port Number/ BaudRates.
Hint 2... You can containerize the opening of the COM Port that way you can catch any error and handle it within that container (i.e. function/ subroutine) to make it very clean.
I hope this helps point you in the right direction and continue asking questions if you need to.
{:0)
Brandon R. Parker
|
|
|
Post by stan58 on Nov 20, 2018 9:38:59 GMT -5
Thank you I will try again.
|
|
|
Post by Rod on Nov 21, 2018 2:34:27 GMT -5
A tip would be to print the command line instead of using the open command . That way you can see the string you have created.
|
|
|
Post by stan58 on Nov 22, 2018 4:52:10 GMT -5
Sorry but struggling with the syntax. My serial port is working fine if I just put in the baudrate as a number along with the other parameters. My problem is that I want the user to have the ability to set them . So I have a settings window and allow the user to run a check on available com ports and also to manually set the baudrate, this works fine and I then store the data . Before opening the port I get the data into portn and brate variables . Now if I run the example code the port opens fine with the correct port number. My problem Is how do I add the brate variable to the string?
open "com";(portn);":19200,n,8,1,ds0,cs0,rs" for random as #commwt (this works fine, but how do I include the brate variable into the string) (brate)
|
|
|
Post by Rod on Nov 22, 2018 8:06:20 GMT -5
This appears to work for me.
portn=3 baud=19200
open "com3:19200,n,8,1,ds0,cs0,rs" for random as #comm close #comm print "com";(portn);":";baud;",n,8,1,ds0,cs0,rs" open "com";(portn);":";baud;",n,8,1,ds0,cs0,rs" for random as #comm close #comm
|
|
|
Post by Brandon Parker on Nov 22, 2018 11:15:15 GMT -5
Rod is correct; that should work although I do have to say that the parenthesis around the portn variable are not required. Frankly I'm surprised this doesn't throw an error since the parenthesis are not actually doing anything or part of something; they are just kind of there.... LB doesn't seem to mind them though...
This should work..... Using str$() with the variables and concatenating using "+" operator.
i = 3 BaudRate = 19200 Print "Com" + str$(i) + ":" + str$(BaudRate) + ",n,8,1,ds0,cs0,rs0" Open "Com" + str$(i) + ":" + str$(BaudRate) + ",n,8,1,ds0,cs0,rs0" For random As #COM
Not using str$() and using the ";" operator for concatenation.
i = 3 BaudRate = 19200 Print "Com";i;":";BaudRate;",n,8,1,ds0,cs0,rs0" Open "Com";i;":";BaudRate;",n,8,1,ds0,cs0,rs0" For random As #COM
The two examples provided produce the same output with respect to the concatenated string and should thus be interchangeable.
What error are you getting?
{:0)
Brandon R. Parker
|
|
|
Post by Rod on Nov 22, 2018 11:50:13 GMT -5
Yes quite right the () are not required. Printing the command string is the fastest way to see your mistakes.
|
|
|
Post by stan58 on Nov 22, 2018 12:28:51 GMT -5
thanks everybody for all your help I will try the suggestions soon. The error would suggest a baudrate error ,I have two error messages in my application main window.if the port can not be opened "oncommerror" i prompt "NO USB DEVICE FITTED" if the serial data string is recieved but does not have the correct terminator then I prompt "COMS" and in the main weight display text box ?. It is the latter prompt that occured.
|
|
|
Post by stan58 on Nov 22, 2018 12:31:00 GMT -5
Dont know where the imogys came from? should of read " ??"
|
|
|
Post by Rod on Nov 22, 2018 12:32:06 GMT -5
Use the ladybug icon and step through your program in the debugger to establish EXACTLY what line of code generates your error.
|
|
|
Post by stan58 on Nov 22, 2018 12:32:07 GMT -5
now I know should have read question marks!
|
|
|
Post by stan58 on Nov 22, 2018 12:35:15 GMT -5
Thanks rod will do and report back
|
|