HyperNext Studio

HyperNext Studio => Networks => Topic started by: minius on June 20, 2016, 06:19:07 AM

Title: Connect to USB Serial Port
Post by: minius on June 20, 2016, 06:19:07 AM
Hey Guys!
I need to connect to an Arduino Mega 2560 board to exchange some information. Is it possible to communicate by USB port? I have tried with serial port commands. I'm not sure what numbers should I put for the port. The Arduino Board is connected on COM10. My project is time sensitive, so any help is welcomed!
Thanks!
Title: Re: Connect to USB Serial Port
Post by: Malkom on June 20, 2016, 09:50:46 AM
Hey Guys!
I need to connect to an Arduino Mega 2560 board to exchange some information. Is it possible to communicate by USB port? I have tried with serial port commands. I'm not sure what numbers should I put for the port. The Arduino Board is connected on COM10. My project is time sensitive, so any help is welcomed!
Thanks!

Hi,

I know very little about Arduino but they seem to be USB HID devices so requiring a Vendor & Product ID in order to communicate with them.

There is a Windows HyperNext USB project for playing around with USB devices - the set of projects can be downloaded from here:-
http://www.tigabyte.com/hnfiles/rescreator.html (http://www.tigabyte.com/hnfiles/rescreator.html)

The HN USB project allows one to see the attached USB devices, as well as their Vendor & Product iDs.
Chapter 29 of the HN's Language Reference PDF and HN's built in help both detail how to interact with USB devices.

Regarding Arduino, here are a few links that might be useful:-

http://forum.arduino.cc/index.php?topic=92346.0 (http://forum.arduino.cc/index.php?topic=92346.0)

http://stackoverflow.com/questions/22687399/register-arduino-board-to-receiver-vendor-id

http://forum.arduino.cc/index.php?topic=233614.0 (http://forum.arduino.cc/index.php?topic=233614.0)


Best of luck.

Malkom



Title: Re: Connect to USB Serial Port
Post by: minius on June 20, 2016, 11:16:54 AM
Thank you, Malkom, for the prompt and explicit reply. I got the HN USB project (I don't know why I didn't get it in the first download last week) and I hope to solve the communication.

I'm really new to HN and haven't been coding since Basic-S, more than 20 years ago. I was looking for an easy solution to make a nice-looking software that handles some data and communication with Arduino Mega. I need to finish the project in two weeks and it took me about one week to this stage.

Your support is life-saving!
Title: Re: Connect to USB Serial Port
Post by: Malkom on June 20, 2016, 12:09:07 PM
I've just noticed that Arduino also supports serial ports for two way comms. HN also supports serial ports comms and has more commands and functionality for serial than it does for USB ports. Unfortunately most modern PCs & laptops don't have serial ports but I've found that USB to serial converters work very well for controlling things like motors and LEDs etc. There is a HN serial port project in that download. Chapter 23 of the Language Reference PDF details serial ports.

I hope things go well for you especially as your time is so short - getting comms to work reliably with hardware can be so tricky although it depends greatly upon the requirements.


Title: Re: Connect to USB Serial Port
Post by: minius on June 26, 2016, 08:17:47 PM
I have done some progress with the USB HID Comms procedure as I have the Arduino board now connected to the HN software. Or that is what I thought. The command USBConnectFN(1) returns the value 1, but I can't get to make any communication. The Write is not sending anything and the Read is making HN to freeze. There is not much with USBReadFN and USBWriteFN in the manual. Could you help me get to the bottom of this? Thank you!
Title: Re: Connect to USB Serial Port
Post by: Malkom on June 27, 2016, 04:54:15 PM
I have done some progress with the USB HID Comms procedure as I have the Arduino board now connected to the HN software. Or that is what I thought. The command USBConnectFN(1) returns the value 1, but I can't get to make any communication. The Write is not sending anything and the Read is making HN to freeze. There is not much with USBReadFN and USBWriteFN in the manual. Could you help me get to the bottom of this? Thank you!
I've noticed from the links below that Arduino users set up the baud rate first before trying to communicate with it. It appears they use some Arduino software on their PC to set the baud rate etc:-

https://www.safaribooksonline.com/library/view/arduino-cookbook/9781449399368/ch04.html (https://www.safaribooksonline.com/library/view/arduino-cookbook/9781449399368/ch04.html)

https://www.arduino.cc/en/Reference/Serial (https://www.arduino.cc/en/Reference/Serial)

http://www.ladyada.net/learn/arduino/lesson4.html (http://www.ladyada.net/learn/arduino/lesson4.html)

I don't know the protocols needed to communicate with an Arduino - how to set it into receive mode or how to get it to get it to transmit some info - the links above might help you.

I still think that the HyperNext serial port commands might be more useful than the USB commands as I mentioned in my previous post.
If you could get your Arduino to repeatedly send out a number then you could try to get HyperNext to read it.
Next is to get the Arduino into read mode so that it could respond to some command you sent it -  there must be some command strings that  Arduino understands so it knows how to treat the data it has just received.

By the way, personally i prefer HyperNext 3.83 over v4.x because I have used v3.83 in several serial and USB projects whereas V4.x is untested. v4.x should work but the newer RB compiler used to compile it had many issues.





Title: Re: Connect to USB Serial Port
Post by: minius on July 30, 2016, 02:19:17 PM
I got the HN to communicate with Arduino board quite easy by sending text. The main problem now is that I need to send data as bytes and not as text. Is there a function in HN to convert or set variables to bytes?
Title: Re: Connect to USB Serial Port
Post by: Malkom on August 01, 2016, 07:14:28 AM
I got the HN to communicate with Arduino board quite easy by sending text. The main problem now is that I need to send data as bytes and not as text. Is there a function in HN to convert or set variables to bytes?

Yes, its a bit long-winded but use the function ChrBFN to build a string of bytes.

Example, to build a byte string of the following three byte: 95, 32, 251

Code: [Select]
Local sb,res,uuid

Put ChrBFN(95) into sb
Append ChrBFN(32) onto sb
Append ChrBFN(251) onto sb

Put USBWriteFN(uuid,sb) into res


Title: Re: Connect to USB Serial Port
Post by: minius on August 01, 2016, 08:03:03 PM
Thank you for the example. I forgot to mention, that I use the SerialWrite function. I have succeeded to use the serial port commands for a USB connected Arduino board.

I get back with the question if it is possible to send via SerialWrite function information formatted as bytes? I have the information in HN as numbers that represent bytes (0-255). At the moment I have formated each number in the array to have 3 digits.

There is the possibility to convert the message with Arduino, but there is a mis-match between the sent message and the return message. I noticed there is a delay of 3 messages (I get the answer from the Arduino board for the message sent 3 times earlier). I can't tell if the delay is from the HN software or from the Arduino, but with the Arduino serila monitor software it works well.

Here is the code for the communication:

SerialWrite(1,info)
SerialTXWait(1)

Append SerialReadAllFN(1) onto field 14
Title: Re: Connect to USB Serial Port
Post by: Malkom on August 02, 2016, 03:41:00 PM
Thank you for the example. I forgot to mention, that I use the SerialWrite function. I have succeeded to use the serial port commands for a USB connected Arduino board.

Thank you for letting me know you have made some progress and are using the SerialWrite function.


I get back with the question if it is possible to send via SerialWrite function information formatted as bytes? I have the information in HN as numbers that represent bytes (0-255). At the moment I have formated each number in the array to have 3 digits.

The strings in HN are intended to be useable with different languages and character sets like Cyrillic - therefore each character in a string actually uses 2 bytes.

Note, there are byte specific functions such as ChrBFN, LenBFN etc - perhaps you should be using these when building your byte-strings so as not to send extra information to the Arduino.


There is the possibility to convert the message with Arduino, but there is a mis-match between the sent message and the return message. I noticed there is a delay of 3 messages (I get the answer from the Arduino board for the message sent 3 times earlier). I can't tell if the delay is from the HN software or from the Arduino, but with the Arduino serila monitor software it works well.

Here is the code for the communication:

SerialWrite(1,info)
SerialTXWait(1)

Append SerialReadAllFN(1) onto field 14

Its been perhaps 10 years since I played around with this but do recall having similar problems with the serial NMEA messages coming from a GPS system. Once I understood the format then the thing worked. Not sure, but perhaps instead of using the SerialReadAllFN function I actually used the SerialReadBytesFN to read an expected number of bytes - perhaps the NMEA sentences are much simpler than an Arduino.

Have you had any luck getting the Arduino to slowly and repeatedly send out a simple string of bytes so HN can echo them to a field - it might help show up the mismatch.


Title: Re: Connect to USB Serial Port
Post by: Malkom on August 02, 2016, 04:20:16 PM
Just a thought - if I were doing this I would use HyperNext V3.83 because it is well tested on serial/USB comms ports and there weren't any bugs reported on those aspects.

Regarding HyperNext 4.x - it is built using a much more modern complier that had lots of issues. HN 4.x was hardly tested on comms ports apart from some simple serial reading on Windows/OS X- but hopefully it will work for you.
Title: Re: Connect to USB Serial Port
Post by: minius on August 05, 2016, 06:34:53 PM
Malkom, Thank you for the continuous support and advice. I like HN as it helped me show some results to my customer after a few days, with a couple of hours of learning the language and making the software. I already plan to make new programs in the future using HN.

I am to advance with the software in HN 4 to switch to HN3 now, but I think we found the problem in the Arduino software and I hope to have it fixed by tomorrow.

I could send and receive continuous text with HN to and from Arduino since one of the first trials as per your advice. The difficulties came when I need to send a precise message and receive a precise response. It is not very clear to me how all this works and how I could use the SerialReadBytesFN, ChrBFN, LenBFN etc functions. It will not matter if we succeed with the new Arduino software. Sending and receiving string  text was working almost well.

I have to install the new hardware and present the software to the customer on Monday, so I hope it will all work-out.

I have another issue now that HN is freezing when I try to compile or run the program. The compiler stops at about 15% and will not finish. Any advice on what I could do to solve this?
Title: Re: Connect to USB Serial Port
Post by: Malkom on August 08, 2016, 06:47:58 AM
Malkom, Thank you for the continuous support and advice. I like HN as it helped me show some results to my customer after a few days, with a couple of hours of learning the language and making the software. I already plan to make new programs in the future using HN.

I am to advance with the software in HN 4 to switch to HN3 now, but I think we found the problem in the Arduino software and I hope to have it fixed by tomorrow.

I could send and receive continuous text with HN to and from Arduino since one of the first trials as per your advice. The difficulties came when I need to send a precise message and receive a precise response. It is not very clear to me how all this works and how I could use the SerialReadBytesFN, ChrBFN, LenBFN etc functions. It will not matter if we succeed with the new Arduino software. Sending and receiving string  text was working almost well.

I have to install the new hardware and present the software to the customer on Monday, so I hope it will all work-out.

I have another issue now that HN is freezing when I try to compile or run the program. The compiler stops at about 15% and will not finish. Any advice on what I could do to solve this?

Thank you for your project - very impressive what you have done - nice to see it :)

I'm so sorry, the freezing is due to a HN compiler bug - it found a syntax error in your code but couldn't recover.

The syntax error is in Popup 1 on Card 1, where you have the function MiddleFN in the IF .. THEN,   Note, HN compiler does not accept functions in the IF ... THEN part:


Your code:-

Code: [Select]
Local RX,ask,valid,polita

Put 'I' into ask
Put PopupItemFN(1) into polita
Append polita onto ask
Append '000000000000000' onto ask
SerialWrite(1,ask)
SerialTxWait(1)
Put SerialReadAllFN(1) into RX
    If MiddleFN(RX,1,1)='J' Then    *** error here ***
            Put MiddleFN(RX,2,17) in RX
            Else
            Call TimeStamp
            Append 'Nu s-a putut citi polita!' onto field 6
            GotoLabel 1
    EndIf


My suggestion (4 changes) - it compiles:-

Code: [Select]
Local RX,ask,valid,polita
Local s1    *** change ***

Put 'I' into ask
Put PopupItemFN(1) into polita
Append polita onto ask
Append '000000000000000' onto ask
SerialWrite(1,ask)
SerialTxWait(1)
Put SerialReadAllFN(1) into RX
Put MiddleFN(RX,1,1) into s1   *** change etc ***
    If s1='J' Then
            Put MiddleFN(RX,2,17) into RX
            Else
            Call TimeStamp
            Append 'Nu s-a putut citi polita!' onto field 6
            GotoLabel 1
    EndIf


Also, a bug below your IF - THEN

should be:-

Code: [Select]
Put MiddleFN(RX,2,17) into RX
Title: Re: Connect to USB Serial Port
Post by: minius on August 23, 2016, 09:17:01 PM
Thank you for the help. Here is a quick up-date of my progress.

I have now the program working fine for sending the information, but there is an issue with reading the received information (the reply). I need to get a reply from the Arduino board as a string of numbers. The problem is that the string is not starting from the beginning, but it has other characters from the previous replies. Sometimes the answer is missing altogether for a few messages sent from HN only to appear as a longer string on the following replies.

I have worked around this issue by making a read from the serial buffer and clearing the variable used to store SerialReadAllFN at the procedure start, just before sending the message i need a reply for. Also having a For... EndFor loop to make 9 trials of communication helped get some results. There are still a lot of incomplete readings of the replies.
Title: Re: Connect to USB Serial Port
Post by: Malkom on August 24, 2016, 06:58:06 AM
Thank you for the detailed info here.

My first guess with garbled messages is some kind of data corruption - perhaps the baud rate is too high.

What about if you lower the baud rate and/or put a delay between sending each character from the Arduino?

I've done a quick web search for arduino message garbled and it gives  a lot of results - many people have a similar problem to you.

http://stackoverflow.com/questions/17893792/arduino-garbled-serial-output (http://stackoverflow.com/questions/17893792/arduino-garbled-serial-output)

I've also had this problem before using HyperNext to communicate with a serial device but it wasn't just the baud rate it was the start/stop bit setting causing the problems. HN has settings for start/stop/parity bits.


 

Title: Re: Connect to USB Serial Port
Post by: minius on August 29, 2016, 08:40:07 AM
Thank you for the information. I got quite good reading now with a 300 ms delay for reading and no other changes in settings.
Title: Re: Connect to USB Serial Port
Post by: Malkom on August 29, 2016, 10:42:27 AM
Thank you for the information. I got quite good reading now with a 300 ms delay for reading and no other changes in settings.

Thats great news you got it working and thank you for letting us know.

300mS - seems a huge delay - not what I was expecting but then again I've never used Arduino - at least it works :)
Title: Re: Connect to USB Serial Port
Post by: minius on September 20, 2016, 06:42:20 PM
I have reduced the delay to 30ms and it still works on it's 4th attempt. Maybe the delay needed is due to the fact that HN 4 is slower in windows (or maybe not).
Title: Re: Connect to USB Serial Port
Post by: Malkom on September 21, 2016, 09:46:42 AM
I have reduced the delay to 30ms and it still works on it's 4th attempt. Maybe the delay needed is due to the fact that HN 4 is slower in windows (or maybe not).

Not sure if setting Windows process priority from Normal to High might make a difference to serial comms speed.

Perhaps increasing the priority of HyperNext Creator for testing, and then your built app might help but probably only if your and your  customer's computer are busy and not idling.


To test on raising HN Creator 4 priority, make a batch file, called HNC4.bat

and enter the following command similar to match your circumstances:

Code: [Select]
START "HyperNext Creator" /HIGH "C:\Program Files (x86)\HyperNext Studio 4\HyperNextCreator4.exe"
Then run the batch file and HN4 should start up - and the Task Manager should show it as running at HIGH priority.







Title: Re: Connect to USB Serial Port
Post by: Malkom on September 21, 2016, 02:22:50 PM
I have reduced the delay to 30ms and it still works on it's 4th attempt. Maybe the delay needed is due to the fact that HN 4 is slower in windows (or maybe not).

Hi Minius

I've just looked at your project and noticed that it isn't reading from the serial port inside the Serial Port Event - available via the Editor - Specials button.

Perhaps you are now using the Serial Port Event, if so thats great, so please disregard the following.

If you don't use the event and instead try to force read data then it would be unreliable.
Using the event means you only get data when it arrives and so should allow greater data throughput.
It means your app could sit idling until data arrives at the port.

The command to use inside the serial port event is something like:-

Code: [Select]
@ used inside SerialMotor project
Put SerialReadAllFN(1) after field 3

Actually the above code is not suitable for higher speed comms because writing to fields is slow and better to write to a variable.


So sorry the docs don't give an example, and although the Serial Motor project shows how to do this,  it again unfortunately it isn't documented.

Malkom